File: pointee-normalize-equate.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 893,176 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (56 lines) | stat: -rw-r--r-- 1,373 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//@ check-pass
//@ revisions: old next
//@[next] compile-flags: -Znext-solver

#![feature(ptr_metadata)]

use std::ptr::{self, Pointee};

fn cast_same_meta<T: ?Sized, U: ?Sized>(ptr: *const T) -> *const U
where
    T: Pointee<Metadata = <U as Pointee>::Metadata>,
{
    let (thin, meta) = ptr.to_raw_parts();
    ptr::from_raw_parts(thin, meta)
}

struct Wrapper<T: ?Sized>(T);

// normalize `Wrapper<T>::Metadata` -> `T::Metadata`
fn wrapper_to_tail<T: ?Sized>(ptr: *const T) -> *const Wrapper<T> {
    cast_same_meta(ptr)
}

// normalize `Wrapper<T>::Metadata` -> `T::Metadata` -> `()`
fn wrapper_to_unit<T>(ptr: *const ()) -> *const Wrapper<T> {
    cast_same_meta(ptr)
}

trait Project {
    type Assoc: ?Sized;
}

struct WrapperProject<T: ?Sized + Project>(T::Assoc);

// normalize `WrapperProject<T>::Metadata` -> `T::Assoc::Metadata`
fn wrapper_project_tail<T: ?Sized + Project>(ptr: *const T::Assoc) -> *const WrapperProject<T> {
    cast_same_meta(ptr)
}

// normalize `WrapperProject<T>::Metadata` -> `T::Assoc::Metadata` -> `()`
fn wrapper_project_unit<T: ?Sized + Project>(ptr: *const ()) -> *const WrapperProject<T>
where
    T::Assoc: Sized,
{
    cast_same_meta(ptr)
}

// normalize `<[T] as Pointee>::Metadata` -> `usize`, even if `[T]: Sized`
fn sized_slice<T>(ptr: *const [T]) -> *const str
where
    [T]: Sized,
{
    cast_same_meta(ptr)
}

fn main() {}