Skip to content

Commit

Permalink
Implement traits for variadic function pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jul 17, 2016
1 parent 103e5c9 commit 9c5039a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/libcore/ptr.rs
Expand Up @@ -571,12 +571,21 @@ macro_rules! fnptr_impls_safety_abi {
}

macro_rules! fnptr_impls_args {
($($Arg: ident),*) => {
($($Arg: ident),+) => {
fnptr_impls_safety_abi! { extern "Rust" fn($($Arg),*) -> Ret, $($Arg),* }
fnptr_impls_safety_abi! { extern "C" fn($($Arg),*) -> Ret, $($Arg),* }
fnptr_impls_safety_abi! { extern "C" fn($($Arg),* , ...) -> Ret, $($Arg),* }
fnptr_impls_safety_abi! { unsafe extern "Rust" fn($($Arg),*) -> Ret, $($Arg),* }
fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),*) -> Ret, $($Arg),* }
}
fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),* , ...) -> Ret, $($Arg),* }
};
() => {
// No variadic functions with 0 parameters
fnptr_impls_safety_abi! { extern "Rust" fn() -> Ret, }
fnptr_impls_safety_abi! { extern "C" fn() -> Ret, }
fnptr_impls_safety_abi! { unsafe extern "Rust" fn() -> Ret, }
fnptr_impls_safety_abi! { unsafe extern "C" fn() -> Ret, }
};
}

fnptr_impls_args! { }
Expand Down
14 changes: 14 additions & 0 deletions src/libcoretest/ptr.rs
Expand Up @@ -171,3 +171,17 @@ fn test_unsized_unique() {
let zs: &mut [i32] = &mut [1, 2, 3];
assert!(ys == zs);
}

#[test]
fn test_variadic_fnptr() {
use core::hash::{Hash, SipHasher};
extern "C" {
fn printf(_: *const u8, ...);
}
let p: unsafe extern "C" fn(*const u8, ...) = printf;
let q = p.clone();
assert_eq!(p, q);
assert!(!(p < q));
let mut s = SipHasher::new();
assert_eq!(p.hash(&mut s), q.hash(&mut s));
}

0 comments on commit 9c5039a

Please sign in to comment.