Skip to content

Commit ea678b7

Browse files
PinCoerceUnsized trait into core
1 parent d3dd34a commit ea678b7

File tree

12 files changed

+170
-4
lines changed

12 files changed

+170
-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
@@ -203,7 +203,7 @@ use core::ops::{AsyncFn, AsyncFnMut, AsyncFnOnce};
203203
use core::ops::{
204204
CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver,
205205
};
206-
use core::pin::Pin;
206+
use core::pin::{Pin, PinCoerceUnsized};
207207
use core::ptr::{self, addr_of_mut, NonNull, Unique};
208208
use core::slice;
209209
use core::task::{Context, Poll};
@@ -2646,3 +2646,6 @@ impl<T: core::error::Error> core::error::Error for Box<T> {
26462646
core::error::Error::provide(&**self, request);
26472647
}
26482648
}
2649+
2650+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2651+
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Rece
264264
use core::panic::{RefUnwindSafe, UnwindSafe};
265265
#[cfg(not(no_global_oom_handling))]
266266
use core::pin::Pin;
267+
use core::pin::PinCoerceUnsized;
267268
use core::ptr::{self, drop_in_place, NonNull};
268269
#[cfg(not(no_global_oom_handling))]
269270
use core::slice::from_raw_parts_mut;
@@ -2181,6 +2182,9 @@ impl<T: ?Sized, A: Allocator> Deref for Rc<T, A> {
21812182
}
21822183
}
21832184

2185+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2186+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Rc<T, A> {}
2187+
21842188
#[unstable(feature = "deref_pure_trait", issue = "87121")]
21852189
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Rc<T, A> {}
21862190

@@ -3696,6 +3700,9 @@ impl<T: ?Sized, A: Allocator> Deref for UniqueRc<T, A> {
36963700
}
36973701
}
36983702

3703+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
3704+
unsafe impl<T: ?Sized> PinCoerceUnsized for UniqueRc<T> {}
3705+
36993706
#[unstable(feature = "unique_rc_arc", issue = "112566")]
37003707
impl<T: ?Sized, A: Allocator> DerefMut for UniqueRc<T, A> {
37013708
fn deref_mut(&mut self) -> &mut T {

library/alloc/src/sync.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use core::marker::{PhantomData, Unsize};
2323
use core::mem::{self, align_of_val_raw};
2424
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, Receiver};
2525
use core::panic::{RefUnwindSafe, UnwindSafe};
26-
use core::pin::Pin;
26+
use core::pin::{Pin, PinCoerceUnsized};
2727
use core::ptr::{self, NonNull};
2828
#[cfg(not(no_global_oom_handling))]
2929
use core::slice::from_raw_parts_mut;
@@ -2147,6 +2147,9 @@ impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
21472147
}
21482148
}
21492149

2150+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2151+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Arc<T, A> {}
2152+
21502153
#[unstable(feature = "deref_pure_trait", issue = "87121")]
21512154
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Arc<T, A> {}
21522155

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: 10 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;
@@ -2394,3 +2395,12 @@ fn assert_coerce_unsized(
23942395
let _: Cell<&dyn Send> = c;
23952396
let _: RefCell<&dyn Send> = d;
23962397
}
2398+
2399+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2400+
unsafe impl<T: ?Sized> PinCoerceUnsized for UnsafeCell<T> {}
2401+
2402+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2403+
unsafe impl<T: ?Sized> PinCoerceUnsized for Cell<T> {}
2404+
2405+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2406+
unsafe impl<T: ?Sized> PinCoerceUnsized for RefCell<T> {}

0 commit comments

Comments
 (0)