File: pin.rs

package info (click to toggle)
rustc-web 1.85.0%2Bdfsg3-1~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,759,988 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,056; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (81 lines) | stat: -rw-r--r-- 2,568 bytes parent folder | download | duplicates (3)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use core::pin::Pin;

#[test]
fn pin_const() {
    // test that the methods of `Pin` are usable in a const context

    const POINTER: &'static usize = &2;

    const PINNED: Pin<&'static usize> = Pin::new(POINTER);
    const PINNED_UNCHECKED: Pin<&'static usize> = unsafe { Pin::new_unchecked(POINTER) };
    assert_eq!(PINNED_UNCHECKED, PINNED);

    const INNER: &'static usize = Pin::into_inner(PINNED);
    assert_eq!(INNER, POINTER);

    const INNER_UNCHECKED: &'static usize = unsafe { Pin::into_inner_unchecked(PINNED) };
    assert_eq!(INNER_UNCHECKED, POINTER);

    const REF: &'static usize = PINNED.get_ref();
    assert_eq!(REF, POINTER);

    const INT: u8 = 42;
    const STATIC_REF: Pin<&'static u8> = Pin::static_ref(&INT);
    assert_eq!(*STATIC_REF, INT);

    // Note: `pin_mut_const` tests that the methods of `Pin<&mut T>` are usable in a const context.
    // A const fn is used because `&mut` is not (yet) usable in constants.
    const fn pin_mut_const() {
        let _ = Pin::new(&mut 2).into_ref();
        let _ = Pin::new(&mut 2).get_mut();
        let _ = unsafe { Pin::new(&mut 2).get_unchecked_mut() };
    }

    pin_mut_const();
}

#[allow(unused)]
mod pin_coerce_unsized {
    use core::cell::{Cell, RefCell, UnsafeCell};
    use core::pin::Pin;
    use core::ptr::NonNull;

    pub trait MyTrait {}
    impl MyTrait for String {}

    // These Pins should continue to compile.
    // Do note that these instances of Pin types cannot be used
    // meaningfully because all methods require a Deref/DerefMut
    // bounds on the pointer type and Cell, RefCell and UnsafeCell
    // do not implement Deref/DerefMut.

    pub fn cell(arg: Pin<Cell<Box<String>>>) -> Pin<Cell<Box<dyn MyTrait>>> {
        arg
    }
    pub fn ref_cell(arg: Pin<RefCell<Box<String>>>) -> Pin<RefCell<Box<dyn MyTrait>>> {
        arg
    }
    pub fn unsafe_cell(arg: Pin<UnsafeCell<Box<String>>>) -> Pin<UnsafeCell<Box<dyn MyTrait>>> {
        arg
    }

    // These sensible Pin coercions are possible.
    pub fn pin_mut_ref(arg: Pin<&mut String>) -> Pin<&mut dyn MyTrait> {
        arg
    }
    pub fn pin_ref(arg: Pin<&String>) -> Pin<&dyn MyTrait> {
        arg
    }
    pub fn pin_ptr(arg: Pin<*const String>) -> Pin<*const dyn MyTrait> {
        arg
    }
    pub fn pin_ptr_mut(arg: Pin<*mut String>) -> Pin<*mut dyn MyTrait> {
        arg
    }
    pub fn pin_non_null(arg: Pin<NonNull<String>>) -> Pin<NonNull<dyn MyTrait>> {
        arg
    }
    pub fn nesting_pins(arg: Pin<Pin<&String>>) -> Pin<Pin<&dyn MyTrait>> {
        arg
    }
}