Skip to content
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

impl Fn/FnMut/FnOnce for Arc/Rc #49224

Closed
wants to merge 5 commits into from
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
66 changes: 66 additions & 0 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,72 @@ impl<T> From<Vec<T>> for Arc<[T]> {
}
}

mod fn_impls {
use super::Arc;

#[stable(feature = "shared_fn_impls", since = "1.26.0")]
impl<A, F: ?Sized + Fn<A>> Fn<A> for Arc<F> {
extern "rust-call" fn call(&self, args: A) -> F::Output {
(**self).call(args)
}
}

#[stable(feature = "shared_fn_impls", since = "1.26.0")]
impl<A, F: ?Sized + Fn<A>> FnMut<A> for Arc<F> {
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
(**self).call(args)
}
}

#[stable(feature = "shared_fn_impls", since = "1.26.0")]
impl<A, F: ?Sized + Fn<A>> FnOnce<A> for Arc<F> {
type Output = F::Output;

extern "rust-call" fn call_once(self, args: A) -> F::Output {
(&*self).call(args)
}
}

#[cfg(test)]
mod tests {
use super::Arc;

#[test]
fn is_fn() {
use_fn(Arc::new(|x| x + 1));
}

#[test]
fn is_fn_mut() {
use_fn_mut(Arc::new(|x| x + 1));
}

#[test]
fn is_fn_once() {
use_fn_once(Arc::new(|x| x + 1));
}

#[test]
fn can_dyn_dispatch() {
let dyn_dispatch: Arc<Fn(u8) -> u8> = Arc::new(|x| x + 1);
use_fn(dyn_dispatch);
}

fn use_fn_once<F: FnOnce(u8) -> u8>(fun: F) {
assert_eq!(2, fun(1));
}

fn use_fn_mut<F: FnMut(u8) -> u8>(mut fun: F) {
assert_eq!(2, fun(1));
assert_eq!(3, fun(2));
}

fn use_fn<F: Fn(u8) -> u8>(fun: F) {
assert_eq!(2, fun(1));
}
}
}

#[cfg(test)]
mod tests {
use std::boxed::Box;
Expand Down
4 changes: 3 additions & 1 deletion src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@
#![feature(exact_chunks)]
#![feature(pointer_methods)]
#![feature(inclusive_range_fields)]
#![feature(fn_traits)]

#![cfg_attr(stage0, feature(generic_param_attrs))]

#![cfg_attr(not(test), feature(fn_traits, swap_with_slice, i128))]
#![cfg_attr(not(test), feature(swap_with_slice, i128))]
#![cfg_attr(test, feature(test))]

// Allow testing this library
Expand Down
66 changes: 66 additions & 0 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,72 @@ impl<T: ?Sized> RcBoxPtr<T> for Weak<T> {
}
}

mod fn_impls {
use super::Rc;

#[stable(feature = "shared_fn_impls", since = "1.26.0")]
impl<A, F: ?Sized + Fn<A>> Fn<A> for Rc<F> {
extern "rust-call" fn call(&self, args: A) -> F::Output {
(**self).call(args)
}
}

#[stable(feature = "shared_fn_impls", since = "1.26.0")]
impl<A, F: ?Sized + Fn<A>> FnMut<A> for Rc<F> {
extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
(**self).call(args)
}
}

#[stable(feature = "shared_fn_impls", since = "1.26.0")]
impl<A, F: ?Sized + Fn<A>> FnOnce<A> for Rc<F> {
type Output = F::Output;

extern "rust-call" fn call_once(self, args: A) -> F::Output {
(&*self).call(args)
}
}

#[cfg(test)]
mod tests {
use super::Rc;

#[test]
fn is_fn() {
use_fn(Rc::new(|x| x + 1));
}

#[test]
fn is_fn_mut() {
use_fn_mut(Rc::new(|x| x + 1));
}

#[test]
fn is_fn_once() {
use_fn_once(Rc::new(|x| x + 1));
}

#[test]
fn can_dyn_dispatch() {
let dyn_dispatch: Rc<Fn(u8) -> u8> = Rc::new(|x| x + 1);
use_fn(dyn_dispatch);
}

fn use_fn_once<F: FnOnce(u8) -> u8>(fun: F) {
assert_eq!(2, fun(1));
}

fn use_fn_mut<F: FnMut(u8) -> u8>(mut fun: F) {
assert_eq!(2, fun(1));
assert_eq!(3, fun(2));
}

fn use_fn<F: Fn(u8) -> u8>(fun: F) {
assert_eq!(2, fun(1));
}
}
}

#[cfg(test)]
mod tests {
use super::{Rc, Weak};
Expand Down