Le 19/07/2013 10:17, Thiez a écrit :
I think something along these lines should do:

fn bitwise_compare<T>(a: &T, b: &T) -> bool {
   use std::{ptr,sys,cast,uint};
   let size = sys::size_of::<T>();
   unsafe {
     let a: &u8 = cast::transmute(a);
     let b: &u8 = cast::transmute(b);
     for uint::range(0,size) |n| {
       if *ptr::offset(a,n) != *ptr::offset(b,n){
         return false
       }
     }
   }
   true
}

Note that it won't work for strings and vectors. It could be slightly
more efficient by comparing in larger chunks than u8, but LLVM will
probably figure that out.

There’s code in libstd that uses libc::memcpm instead of an explicit loop:

https://github.com/mozilla/rust/blob/06fec5243b/src/libstd/vec.rs#L2091

Cheers,
--
Simon Sapin
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to