Skip to content

Commit bc7c430

Browse files
committed
Implement more traits for function pointers
1 parent d89a10b commit bc7c430

File tree

2 files changed

+71
-69
lines changed

2 files changed

+71
-69
lines changed

src/libcore/clone.rs

-42
Original file line numberDiff line numberDiff line change
@@ -86,45 +86,3 @@ clone_impl! { f64 }
8686
clone_impl! { () }
8787
clone_impl! { bool }
8888
clone_impl! { char }
89-
90-
macro_rules! extern_fn_clone {
91-
($($A:ident),*) => (
92-
#[stable(feature = "rust1", since = "1.0.0")]
93-
impl<$($A,)* ReturnType> Clone for extern "Rust" fn($($A),*) -> ReturnType {
94-
/// Returns a copy of a function pointer.
95-
#[inline]
96-
fn clone(&self) -> extern "Rust" fn($($A),*) -> ReturnType { *self }
97-
}
98-
99-
#[stable(feature = "rust1", since = "1.0.0")]
100-
impl<$($A,)* ReturnType> Clone for extern "C" fn($($A),*) -> ReturnType {
101-
/// Returns a copy of a function pointer.
102-
#[inline]
103-
fn clone(&self) -> extern "C" fn($($A),*) -> ReturnType { *self }
104-
}
105-
106-
#[stable(feature = "rust1", since = "1.0.0")]
107-
impl<$($A,)* ReturnType> Clone for unsafe extern "Rust" fn($($A),*) -> ReturnType {
108-
/// Returns a copy of a function pointer.
109-
#[inline]
110-
fn clone(&self) -> unsafe extern "Rust" fn($($A),*) -> ReturnType { *self }
111-
}
112-
113-
#[stable(feature = "rust1", since = "1.0.0")]
114-
impl<$($A,)* ReturnType> Clone for unsafe extern "C" fn($($A),*) -> ReturnType {
115-
/// Returns a copy of a function pointer.
116-
#[inline]
117-
fn clone(&self) -> unsafe extern "C" fn($($A),*) -> ReturnType { *self }
118-
}
119-
)
120-
}
121-
122-
extern_fn_clone! {}
123-
extern_fn_clone! { A }
124-
extern_fn_clone! { A, B }
125-
extern_fn_clone! { A, B, C }
126-
extern_fn_clone! { A, B, C, D }
127-
extern_fn_clone! { A, B, C, D, E }
128-
extern_fn_clone! { A, B, C, D, E, F }
129-
extern_fn_clone! { A, B, C, D, E, F, G }
130-
extern_fn_clone! { A, B, C, D, E, F, G, H }

src/libcore/ptr.rs

+71-27
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use clone::Clone;
2020
use intrinsics;
2121
use ops::Deref;
2222
use fmt;
23+
use hash;
2324
use option::Option::{self, Some, None};
2425
use marker::{PhantomData, Send, Sized, Sync};
2526
use mem;
@@ -308,40 +309,83 @@ impl<T: ?Sized> Clone for *mut T {
308309
}
309310
}
310311

311-
// Equality for extern "C" fn pointers
312-
mod externfnpointers {
313-
use cmp::PartialEq;
312+
// Impls for function pointers
313+
macro_rules! fnptr_impls_safety_abi {
314+
($FnTy: ty, $($Arg: ident),*) => {
315+
#[stable(feature = "rust1", since = "1.0.0")]
316+
impl<Ret, $($Arg),*> Clone for $FnTy {
317+
#[inline]
318+
fn clone(&self) -> Self {
319+
*self
320+
}
321+
}
314322

315-
#[stable(feature = "rust1", since = "1.0.0")]
316-
impl<_R> PartialEq for extern "C" fn() -> _R {
317-
#[inline]
318-
fn eq(&self, other: &extern "C" fn() -> _R) -> bool {
319-
let self_ = *self as usize;
320-
let other_ = *other as usize;
321-
self_ == other_
323+
#[stable(feature = "fnptr_impls", since = "1.4.0")]
324+
impl<Ret, $($Arg),*> PartialEq for $FnTy {
325+
#[inline]
326+
fn eq(&self, other: &Self) -> bool {
327+
*self as usize == *other as usize
328+
}
322329
}
323-
}
324-
macro_rules! fnptreq {
325-
($($p:ident),*) => {
326-
#[stable(feature = "rust1", since = "1.0.0")]
327-
impl<_R,$($p),*> PartialEq for extern "C" fn($($p),*) -> _R {
328-
#[inline]
329-
fn eq(&self, other: &extern "C" fn($($p),*) -> _R) -> bool {
330-
let self_ = *self as usize;
331-
332-
let other_ = *other as usize;
333-
self_ == other_
334-
}
330+
331+
#[stable(feature = "fnptr_impls", since = "1.4.0")]
332+
impl<Ret, $($Arg),*> Eq for $FnTy {}
333+
334+
#[stable(feature = "fnptr_impls", since = "1.4.0")]
335+
impl<Ret, $($Arg),*> PartialOrd for $FnTy {
336+
#[inline]
337+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
338+
(*self as usize).partial_cmp(&(*other as usize))
339+
}
340+
}
341+
342+
#[stable(feature = "fnptr_impls", since = "1.4.0")]
343+
impl<Ret, $($Arg),*> Ord for $FnTy {
344+
#[inline]
345+
fn cmp(&self, other: &Self) -> Ordering {
346+
(*self as usize).cmp(&(*other as usize))
347+
}
348+
}
349+
350+
#[stable(feature = "fnptr_impls", since = "1.4.0")]
351+
impl<Ret, $($Arg),*> hash::Hash for $FnTy {
352+
fn hash<HH: hash::Hasher>(&self, state: &mut HH) {
353+
state.write_usize(*self as usize)
354+
}
355+
}
356+
357+
#[stable(feature = "fnptr_impls", since = "1.4.0")]
358+
impl<Ret, $($Arg),*> fmt::Pointer for $FnTy {
359+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
360+
fmt::Pointer::fmt(&(*self as *const ()), f)
361+
}
362+
}
363+
364+
#[stable(feature = "fnptr_impls", since = "1.4.0")]
365+
impl<Ret, $($Arg),*> fmt::Debug for $FnTy {
366+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
367+
fmt::Pointer::fmt(&(*self as *const ()), f)
335368
}
336369
}
337370
}
338-
fnptreq! { A }
339-
fnptreq! { A,B }
340-
fnptreq! { A,B,C }
341-
fnptreq! { A,B,C,D }
342-
fnptreq! { A,B,C,D,E }
343371
}
344372

373+
macro_rules! fnptr_impls_args {
374+
($($Arg: ident),*) => {
375+
fnptr_impls_safety_abi! { extern "Rust" fn($($Arg),*) -> Ret, $($Arg),* }
376+
fnptr_impls_safety_abi! { extern "C" fn($($Arg),*) -> Ret, $($Arg),* }
377+
fnptr_impls_safety_abi! { unsafe extern "Rust" fn($($Arg),*) -> Ret, $($Arg),* }
378+
fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),*) -> Ret, $($Arg),* }
379+
}
380+
}
381+
382+
fnptr_impls_args! { }
383+
fnptr_impls_args! { A }
384+
fnptr_impls_args! { A, B }
385+
fnptr_impls_args! { A, B, C }
386+
fnptr_impls_args! { A, B, C, D }
387+
fnptr_impls_args! { A, B, C, D, E }
388+
345389
// Comparison for pointers
346390
#[stable(feature = "rust1", since = "1.0.0")]
347391
impl<T: ?Sized> Ord for *const T {

0 commit comments

Comments
 (0)