Skip to content
Closed
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
35 changes: 35 additions & 0 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,35 @@ impl<T: ?Sized + Hash> Hash for Arc<T> {
}
}

#[stable(feature = "fn_smart_ptr", since = "1.11.0")]
impl<I, F: ?Sized> Fn<I> for Arc<F>
where F: Fn<I>
{
extern "rust-call" fn call(&self, args: I) -> Self::Output {
(&**self).call(args)
}
}

#[stable(feature = "fn_smart_ptr", since = "1.11.0")]
impl<I, F: ?Sized> FnMut<I> for Arc<F>
where F: Fn<I>
{
extern "rust-call" fn call_mut(&mut self, args: I) -> Self::Output {
self.call(args)
}
}

#[stable(feature = "fn_smart_ptr", since = "1.11.0")]
impl<I, F: ?Sized> FnOnce<I> for Arc<F>
where F: Fn<I>
{
type Output = F::Output;
extern "rust-call" fn call_once(self, args: I) -> Self::Output {
self.call(args)
}
}


#[stable(feature = "from_for_ptrs", since = "1.6.0")]
impl<T> From<T> for Arc<T> {
fn from(t: T) -> Self {
Expand Down Expand Up @@ -1172,6 +1201,12 @@ mod tests {
assert_eq!(format!("{:?}", a), "5");
}

#[test]
fn test_fn() {
let f = Arc::new(|i: i32| -> i32 { i + 1 });
assert_eq!(Some(1).map(f), Some(2));
}

// Make sure deriving works with Arc<T>
#[derive(Eq, Ord, PartialEq, PartialOrd, Clone, Debug, Default)]
struct Foo {
Expand Down
3 changes: 2 additions & 1 deletion src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@
#![feature(unique)]
#![feature(unsafe_no_drop_flag, filling_drop)]
#![feature(unsize)]
#![feature(fn_traits)]

#![cfg_attr(not(test), feature(raw, fn_traits, placement_new_protocol))]
#![cfg_attr(not(test), feature(raw, placement_new_protocol))]
#![cfg_attr(test, feature(test, box_heap))]

// Allow testing this library
Expand Down
34 changes: 34 additions & 0 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,34 @@ impl<T: ?Sized> fmt::Pointer for Rc<T> {
}
}

#[stable(feature = "fn_smart_ptr", since = "1.11.0")]
impl<I, F: ?Sized> Fn<I> for Rc<F>
where F: Fn<I>
{
extern "rust-call" fn call(&self, args: I) -> Self::Output {
(&**self).call(args)
}
}

#[stable(feature = "fn_smart_ptr", since = "1.11.0")]
impl<I, F: ?Sized> FnMut<I> for Rc<F>
where F: Fn<I>
{
extern "rust-call" fn call_mut(&mut self, args: I) -> Self::Output {
self.call(args)
}
}

#[stable(feature = "fn_smart_ptr", since = "1.11.0")]
impl<I, F: ?Sized> FnOnce<I> for Rc<F>
where F: Fn<I>
{
type Output = F::Output;
extern "rust-call" fn call_once(self, args: I) -> Self::Output {
self.call(args)
}
}

#[stable(feature = "from_for_ptrs", since = "1.6.0")]
impl<T> From<T> for Rc<T> {
fn from(t: T) -> Self {
Expand Down Expand Up @@ -1137,6 +1165,12 @@ mod tests {
assert_eq!(format!("{:?}", foo), "75");
}

#[test]
fn test_fn() {
let f = Rc::new(|i: i32| -> i32 { i + 1 });
assert_eq!(Some(1).map(f), Some(2));
}

#[test]
fn test_unsized() {
let foo: Rc<[i32]> = Rc::new([1, 2, 3]);
Expand Down