1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/// Internal namespace.
pub( crate ) mod private
{
// use crate::protected::*;
///
/// Are two pointers points on the same data.
///
/// Does not require arguments to have the same type.
///
pub fn same_data< T1 : ?Sized, T2 : ?Sized >( src1 : &T1, src2 : &T2 ) -> bool
{
extern "C" { fn memcmp( s1 : *const u8, s2 : *const u8, n : usize ) -> i32; }
let mem1 = src1 as *const _ as *const u8;
let mem2 = src2 as *const _ as *const u8;
if !same_size( src1, src2 )
{
return false;
}
// Unsafe block is required because we're calling a foreign function (memcmp)
// and manually managing memory addresses.
#[ allow( unsafe_code ) ]
unsafe { memcmp( mem1, mem2, core::mem::size_of_val( src1 ) ) == 0 }
}
/* zzz : qqq : implement mem::same_data, comparing data. discuss */
///
/// Are two pointers are the same, not taking into accoint type.
///
/// Unlike `std::ptr::eq()` does not require arguments to have the same type.
///
pub fn same_ptr< T1 : ?Sized, T2 : ?Sized >( src1 : &T1, src2 : &T2 ) -> bool
{
let mem1 = src1 as *const _ as *const ();
let mem2 = src2 as *const _ as *const ();
mem1 == mem2
}
///
/// Are two pointers points on data of the same size.
///
pub fn same_size< T1 : ?Sized, T2 : ?Sized >( _src1 : &T1, _src2 : &T2 ) -> bool
{
core::mem::size_of_val( _src1 ) == core::mem::size_of_val( _src2 )
}
///
/// Are two pointers points on the same region, ie same size and same pointer.
///
/// Does not require arguments to have the same type.
///
pub fn same_region< T1 : ?Sized, T2 : ?Sized >( src1 : &T1, src2 : &T2 ) -> bool
{
same_ptr( src1, src2 ) && same_size( src1, src2 )
}
}
/// Protected namespace of the module.
pub mod protected
{
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use super::
{
orphan::*,
};
}
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use protected::*;
/// Orphan namespace of the module.
pub mod orphan
{
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use super::
{
exposed::*,
private::same_data,
private::same_ptr,
private::same_size,
private::same_region,
};
}
/// Exposed namespace of the module.
pub mod exposed
{
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use super::prelude::*;
}
/// Prelude to use essentials: `use my_module::prelude::*`.
pub mod prelude
{
}