@@ -20,6 +20,7 @@ use clone::Clone;
2020use intrinsics;
2121use ops:: Deref ;
2222use fmt;
23+ use hash;
2324use option:: Option :: { self , Some , None } ;
2425use marker:: { PhantomData , Send , Sized , Sync } ;
2526use 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" ) ]
347391impl < T : ?Sized > Ord for * const T {
0 commit comments