Skip to content

Commit d495b84

Browse files
PinCoerceUnsized trait into core
1 parent 83dcdb3 commit d495b84

File tree

16 files changed

+218
-4
lines changed

16 files changed

+218
-4
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ symbols! {
276276
Path,
277277
PathBuf,
278278
Pending,
279+
PinCoerceUnsized,
279280
Pointer,
280281
Poll,
281282
ProcMacro,

library/alloc/src/boxed.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ use core::ops::{
200200
AsyncFn, AsyncFnMut, AsyncFnOnce, CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut,
201201
DerefPure, DispatchFromDyn, Receiver,
202202
};
203-
use core::pin::Pin;
203+
use core::pin::{Pin, PinCoerceUnsized};
204204
use core::ptr::{self, addr_of_mut, NonNull, Unique};
205205
use core::task::{Context, Poll};
206206
use core::{borrow, fmt, slice};
@@ -2724,3 +2724,6 @@ impl<T: core::error::Error> core::error::Error for Box<T> {
27242724
core::error::Error::provide(&**self, request);
27252725
}
27262726
}
2727+
2728+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2729+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Box<T, A> {}

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
#![feature(maybe_uninit_uninit_array_transpose)]
139139
#![feature(panic_internals)]
140140
#![feature(pattern)]
141+
#![feature(pin_coerce_unsized_trait)]
141142
#![feature(ptr_internals)]
142143
#![feature(ptr_metadata)]
143144
#![feature(ptr_sub_ptr)]

library/alloc/src/rc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Rece
256256
use core::panic::{RefUnwindSafe, UnwindSafe};
257257
#[cfg(not(no_global_oom_handling))]
258258
use core::pin::Pin;
259+
use core::pin::PinCoerceUnsized;
259260
use core::ptr::{self, drop_in_place, NonNull};
260261
#[cfg(not(no_global_oom_handling))]
261262
use core::slice::from_raw_parts_mut;
@@ -2176,6 +2177,12 @@ impl<T: ?Sized, A: Allocator> Deref for Rc<T, A> {
21762177
}
21772178
}
21782179

2180+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2181+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Rc<T, A> {}
2182+
2183+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2184+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Weak<T, A> {}
2185+
21792186
#[unstable(feature = "deref_pure_trait", issue = "87121")]
21802187
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Rc<T, A> {}
21812188

@@ -3689,6 +3696,9 @@ impl<T: ?Sized, A: Allocator> Deref for UniqueRc<T, A> {
36893696
}
36903697
}
36913698

3699+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
3700+
unsafe impl<T: ?Sized> PinCoerceUnsized for UniqueRc<T> {}
3701+
36923702
#[unstable(feature = "unique_rc_arc", issue = "112566")]
36933703
impl<T: ?Sized, A: Allocator> DerefMut for UniqueRc<T, A> {
36943704
fn deref_mut(&mut self) -> &mut T {

library/alloc/src/sync.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use core::marker::{PhantomData, Unsize};
2020
use core::mem::{self, align_of_val_raw, ManuallyDrop};
2121
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, Receiver};
2222
use core::panic::{RefUnwindSafe, UnwindSafe};
23-
use core::pin::Pin;
23+
use core::pin::{Pin, PinCoerceUnsized};
2424
use core::ptr::{self, NonNull};
2525
#[cfg(not(no_global_oom_handling))]
2626
use core::slice::from_raw_parts_mut;
@@ -2142,6 +2142,12 @@ impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
21422142
}
21432143
}
21442144

2145+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2146+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Arc<T, A> {}
2147+
2148+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2149+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Weak<T, A> {}
2150+
21452151
#[unstable(feature = "deref_pure_trait", issue = "87121")]
21462152
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Arc<T, A> {}
21472153

library/alloc/tests/arc.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,17 @@ fn make_mut_unsized() {
227227
assert_eq!(*data, [11, 21, 31]);
228228
assert_eq!(*other_data, [110, 20, 30]);
229229
}
230+
231+
#[allow(unused)]
232+
mod pin_coerce_unsized {
233+
use alloc::sync::Arc;
234+
use core::pin::Pin;
235+
236+
pub trait MyTrait {}
237+
impl MyTrait for String {}
238+
239+
// Pin coercion should work for Arc
240+
pub fn pin_arc(arg: Pin<Arc<String>>) -> Pin<Arc<dyn MyTrait>> {
241+
arg
242+
}
243+
}

library/alloc/tests/boxed.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,40 @@ unsafe impl Allocator for ConstAllocator {
179179
self
180180
}
181181
}
182+
183+
#[allow(unused)]
184+
mod pin_coerce_unsized {
185+
use alloc::boxed::Box;
186+
use core::pin::Pin;
187+
188+
trait MyTrait {
189+
fn action(&self) -> &str;
190+
}
191+
impl MyTrait for String {
192+
fn action(&self) -> &str {
193+
&*self
194+
}
195+
}
196+
struct MyStruct;
197+
impl MyTrait for MyStruct {
198+
fn action(&self) -> &str {
199+
"MyStruct"
200+
}
201+
}
202+
203+
// Pin coercion should work for Box
204+
fn pin_box<T: MyTrait + 'static>(arg: Pin<Box<T>>) -> Pin<Box<dyn MyTrait>> {
205+
arg
206+
}
207+
208+
#[test]
209+
fn pin_coerce_unsized_box() {
210+
let my_string = "my string";
211+
let a_string = Box::pin(String::from(my_string));
212+
let pin_box_str = pin_box(a_string);
213+
assert_eq!(pin_box_str.as_ref().action(), my_string);
214+
let a_struct = Box::pin(MyStruct);
215+
let pin_box_struct = pin_box(a_struct);
216+
assert_eq!(pin_box_struct.as_ref().action(), "MyStruct");
217+
}
218+
}

library/alloc/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#![feature(drain_keep_rest)]
4141
#![feature(local_waker)]
4242
#![feature(vec_pop_if)]
43+
#![feature(unique_rc_arc)]
4344
#![allow(internal_features)]
4445
#![deny(fuzzy_provenance_casts)]
4546
#![deny(unsafe_op_in_unsafe_fn)]

library/alloc/tests/rc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,20 @@ fn weak_may_dangle() {
205205
// `val` dropped here while still borrowed
206206
// borrow might be used here, when `val` is dropped and runs the `Drop` code for type `std::rc::Weak`
207207
}
208+
209+
#[allow(unused)]
210+
mod pin_coerce_unsized {
211+
use alloc::rc::{Rc, UniqueRc};
212+
use core::pin::Pin;
213+
214+
pub trait MyTrait {}
215+
impl MyTrait for String {}
216+
217+
// Pin coercion should work for Rc
218+
pub fn pin_rc(arg: Pin<Rc<String>>) -> Pin<Rc<dyn MyTrait>> {
219+
arg
220+
}
221+
pub fn pin_unique_rc(arg: Pin<UniqueRc<String>>) -> Pin<UniqueRc<dyn MyTrait>> {
222+
arg
223+
}
224+
}

library/core/src/cell.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ use crate::fmt::{self, Debug, Display};
255255
use crate::marker::{PhantomData, Unsize};
256256
use crate::mem;
257257
use crate::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn};
258+
use crate::pin::PinCoerceUnsized;
258259
use crate::ptr::{self, NonNull};
259260

260261
mod lazy;
@@ -2396,3 +2397,21 @@ fn assert_coerce_unsized(
23962397
let _: Cell<&dyn Send> = c;
23972398
let _: RefCell<&dyn Send> = d;
23982399
}
2400+
2401+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2402+
unsafe impl<T: ?Sized> PinCoerceUnsized for UnsafeCell<T> {}
2403+
2404+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2405+
unsafe impl<T: ?Sized> PinCoerceUnsized for SyncUnsafeCell<T> {}
2406+
2407+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2408+
unsafe impl<T: ?Sized> PinCoerceUnsized for Cell<T> {}
2409+
2410+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2411+
unsafe impl<T: ?Sized> PinCoerceUnsized for RefCell<T> {}
2412+
2413+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2414+
unsafe impl<'b, T: ?Sized> PinCoerceUnsized for Ref<'b, T> {}
2415+
2416+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2417+
unsafe impl<'b, T: ?Sized> PinCoerceUnsized for RefMut<'b, T> {}

0 commit comments

Comments
 (0)