mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 01:05:39 +03:00
1e4c1d6f75
Also, delete impls on non-Deref types. Pin doesn't do anything useful for non-Deref types, so PinCoerceUnsized on such types makes no sense. This is a breaking change, since stable code can observe the deleted `PinCoerceUnsized` impls by uselessly coercing between such types inside a `Pin`. There is still some strange behavior, such as `Pin<&mut i32>` being able to coerce to `Pin<&dyn Send>`, but not being able to coerce to `Pin<&i32>`. However, I don't think it's possible to fix this. Fixes https://github.com/rust-lang/rust/issues/145081
59 lines
1.7 KiB
Rust
59 lines
1.7 KiB
Rust
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();
|
|
unsafe {
|
|
let _ = 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 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 nesting_pins(arg: Pin<Pin<&String>>) -> Pin<Pin<&dyn MyTrait>> {
|
|
arg
|
|
}
|
|
}
|