Skip to content

Commit c4b4f07

Browse files
committed
Auto merge of #26727 - remram44:coerceunsized-weak, r=eddyb
This is a simple addition, shouldn't change behavior. Fixes #26704 I don't know if the coercion for `Rc` is tested, if it is this probably needs the same test with `Weak`.
2 parents f635b2f + 3278e79 commit c4b4f07

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/liballoc/arc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ pub struct Weak<T: ?Sized> {
145145
unsafe impl<T: ?Sized + Sync + Send> Send for Weak<T> { }
146146
unsafe impl<T: ?Sized + Sync + Send> Sync for Weak<T> { }
147147

148+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
149+
148150
#[stable(feature = "rust1", since = "1.0.0")]
149151
impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {
150152
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

src/liballoc/rc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ pub struct Weak<T: ?Sized> {
734734
impl<T: ?Sized> !marker::Send for Weak<T> {}
735735
impl<T: ?Sized> !marker::Sync for Weak<T> {}
736736

737+
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
738+
737739
#[unstable(feature = "rc_weak",
738740
reason = "Weak pointers may not belong in this module.")]
739741
impl<T: ?Sized> Weak<T> {

src/test/run-pass/dst-coerce-rc.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
// Test a very simple custom DST coercion.
1212

13-
#![feature(core)]
13+
#![feature(core, rc_weak)]
1414

1515
use std::cell::RefCell;
16-
use std::rc::Rc;
16+
use std::rc::{Rc, Weak};
1717

1818
trait Baz {
1919
fn get(&self) -> i32;
@@ -36,9 +36,13 @@ fn main() {
3636
let b: Rc<Baz> = a.clone();
3737
assert_eq!(b.get(), 42);
3838

39+
let c: Weak<i32> = a.downgrade();
40+
let d: Weak<Baz> = c.clone();
41+
3942
let _c = b.clone();
4043

4144
let a: Rc<RefCell<i32>> = Rc::new(RefCell::new(42));
4245
let b: Rc<RefCell<Baz>> = a.clone();
4346
assert_eq!(b.borrow().get(), 42);
47+
let c: Weak<RefCell<Baz>> = a.downgrade();
4448
}

0 commit comments

Comments
 (0)