Skip to content

Commit

Permalink
std: Implement Clone and DeepClone for extern "Rust" fn
Browse files Browse the repository at this point in the history
Implement Clone and DeepClone for functions with 0 to 8 arguments.
  • Loading branch information
blake2-ppc committed Jul 29, 2013
1 parent 27812ea commit 11aad20
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/libstd/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ clone_impl!(())
clone_impl!(bool)
clone_impl!(char)

macro_rules! extern_fn_clone(
($($A:ident),*) => (
impl<$($A,)* ReturnType> Clone for extern "Rust" fn($($A),*) -> ReturnType {
/// Return a copy of a function pointer
#[inline]
fn clone(&self) -> extern "Rust" fn($($A),*) -> ReturnType { *self }
}
)
)

extern_fn_clone!()
extern_fn_clone!(A)
extern_fn_clone!(A, B)
extern_fn_clone!(A, B, C)
extern_fn_clone!(A, B, C, D)
extern_fn_clone!(A, B, C, D, E)
extern_fn_clone!(A, B, C, D, E, F)
extern_fn_clone!(A, B, C, D, E, F, G)
extern_fn_clone!(A, B, C, D, E, F, G, H)

/// A trait distinct from `Clone` which represents "deep copies" of things like
/// managed boxes which would otherwise not be copied.
pub trait DeepClone {
Expand Down Expand Up @@ -157,6 +177,26 @@ deep_clone_impl!(())
deep_clone_impl!(bool)
deep_clone_impl!(char)

macro_rules! extern_fn_deep_clone(
($($A:ident),*) => (
impl<$($A,)* ReturnType> DeepClone for extern "Rust" fn($($A),*) -> ReturnType {
/// Return a copy of a function pointer
#[inline]
fn deep_clone(&self) -> extern "Rust" fn($($A),*) -> ReturnType { *self }
}
)
)

extern_fn_deep_clone!()
extern_fn_deep_clone!(A)
extern_fn_deep_clone!(A, B)
extern_fn_deep_clone!(A, B, C)
extern_fn_deep_clone!(A, B, C, D)
extern_fn_deep_clone!(A, B, C, D, E)
extern_fn_deep_clone!(A, B, C, D, E, F)
extern_fn_deep_clone!(A, B, C, D, E, F, G)
extern_fn_deep_clone!(A, B, C, D, E, F, G, H)

#[test]
fn test_owned_clone() {
let a = ~5i;
Expand Down Expand Up @@ -195,3 +235,21 @@ fn test_borrowed_clone() {
let z: &int = (&y).clone();
assert_eq!(*z, 5);
}

#[test]
fn test_extern_fn_clone() {
trait Empty {}
impl Empty for int {}

fn test_fn_a() -> float { 1.0 }
fn test_fn_b<T: Empty>(x: T) -> T { x }
fn test_fn_c(_: int, _: float, _: ~[int], _: int, _: int, _: int) {}

let _ = test_fn_a.clone();
let _ = test_fn_b::<int>.clone();
let _ = test_fn_c.clone();

let _ = test_fn_a.deep_clone();
let _ = test_fn_b::<int>.deep_clone();
let _ = test_fn_c.deep_clone();
}

5 comments on commit 11aad20

@bors
Copy link
Contributor

@bors bors commented on 11aad20 Jul 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 11aad20 Jul 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging blake2-ppc/rust/extern-fn-clone = 11aad20 into auto

@bors
Copy link
Contributor

@bors bors commented on 11aad20 Jul 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blake2-ppc/rust/extern-fn-clone = 11aad20 merged ok, testing candidate = d34016d

@bors
Copy link
Contributor

@bors bors commented on 11aad20 Jul 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d34016d

Please sign in to comment.