logo
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
109
110
111
112
113
114
115
116
117
118
119

#[ cfg( feature = "compiletime_assertions" ) ]
pub( crate ) mod private
{

  ///
  /// Compile-time assertion that two types have the same size.
  ///

  // #[ cfg( feature = "compiletime_assertions" ) ]
  #[ macro_export ]
  macro_rules! cta_type_same_size
  {
    ( $Type1:ty, $Type2:ty $(,)? ) =>
    {{
      const _ : fn() = ||
      {
        let _ : [ () ; core::mem::size_of::< $Type1 >() ] = [ () ; core::mem::size_of::< $Type2 >() ];
      };
      // let _ = core::mem::transmute::< $Type1, $Type2 >;
      true
    }}
  }

  ///
  /// Compile-time assertion of having the same align.
  ///

  // #[ cfg( feature = "compiletime_assertions" ) ]
  #[ macro_export ]
  macro_rules! cta_type_same_align
  {
    ( $Type1:ty, $Type2:ty $(,)? ) =>
    {{
      const _ : fn() = ||
      {
        let _ : [ () ; core::mem::align_of::< $Type1 >() ] = [ () ; core::mem::align_of::< $Type2 >() ];
      };
      true
    }};
  }

  ///
  /// Compile-time assertion that memory behind two references have the same size.
  ///

  // #[ cfg( feature = "compiletime_assertions" ) ]
  #[ macro_export ]
  macro_rules! cta_ptr_same_size
  {
    ( $Ins1:expr, $Ins2:expr $(,)? ) =>
    {{
      #[ allow( unsafe_code, unknown_lints, forget_copy, useless_transmute ) ]
      let _ = || unsafe
      {
        let mut ins1 = core::ptr::read( $Ins1 );
        core::ptr::write( &mut ins1, core::mem::transmute( core::ptr::read( $Ins2 ) ) );
        core::mem::forget( ins1 );
      };
      true
    }}
  }

  ///
  /// Compile-time assertion that two values have the same size.
  ///
  /// Does not consume values.
  ///

  // #[ cfg( feature = "compiletime_assertions" ) ]
  #[ macro_export ]
  macro_rules! cta_mem_same_size
  {
    ( $Ins1:expr, $Ins2:expr $(,)? ) =>
    {{
      $crate::cta_ptr_same_size!( &$Ins1, &$Ins2 )
    }}
  }

  pub use cta_type_same_size;
  pub use cta_type_same_align;

  pub use cta_ptr_same_size;
  pub use cta_mem_same_size;
}

/// Protected namespace of the module.
pub mod protected
{
  pub use super::orphan::*;
}

#[ doc( inline ) ]
pub use protected::*;

/// Orphan namespace of the module.
pub mod orphan
{
  pub use super::exposed::*;
}

/// Exposed namespace of the module.
pub mod exposed
{
  pub use super::prelude::*;
}

/// Prelude to use essentials: `use my_module::prelude::*`.
pub mod prelude
{
  #[ cfg( feature = "compiletime_assertions" ) ]
  pub use super::private::
  {
    cta_type_same_size,
    cta_type_same_align,
    cta_ptr_same_size,
    cta_mem_same_size,
  };
}