Skip to content

Implement CoerceUnsized for rc::Weak #26727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ pub struct Weak<T: ?Sized> {
unsafe impl<T: ?Sized + Sync + Send> Send for Weak<T> { }
unsafe impl<T: ?Sized + Sync + Send> Sync for Weak<T> { }

impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
2 changes: 2 additions & 0 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,8 @@ pub struct Weak<T: ?Sized> {
impl<T: ?Sized> !marker::Send for Weak<T> {}
impl<T: ?Sized> !marker::Sync for Weak<T> {}

impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}

#[unstable(feature = "rc_weak",
reason = "Weak pointers may not belong in this module.")]
impl<T: ?Sized> Weak<T> {
Expand Down
8 changes: 6 additions & 2 deletions src/test/run-pass/dst-coerce-rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

// Test a very simple custom DST coercion.

#![feature(core)]
#![feature(core, rc_weak)]

use std::cell::RefCell;
use std::rc::Rc;
use std::rc::{Rc, Weak};

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

let c: Weak<i32> = a.downgrade();
let d: Weak<Baz> = c.clone();

let _c = b.clone();

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