Skip to content
Closed
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
18 changes: 18 additions & 0 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ impl<T> Clone for Weak<T> {
}
}

impl<T: fmt::Show> fmt::Show for Weak<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.upgrade() {
Some(rc) => rc.fmt(f),
_ => "<stale weak pointer>".fmt(f)
}
}
}

#[doc(hidden)]
trait RcBoxPtr<T> {
fn inner<'a>(&'a self) -> &'a RcBox<T>;
Expand Down Expand Up @@ -399,4 +408,13 @@ mod tests {
assert!(cow1_weak.upgrade().is_none());
}

#[test]
fn test_stale_weak_pointer() {
let strong = Rc::new(75u);
let weak = strong.downgrade();
assert_eq!(format!("{}", weak), format!("75"));
drop(strong);
assert_eq!(format!("{}", weak), format!("<stale weak pointer>"));
}

}