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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
//! Contains marker traits representing a variety of guarantees provided by the implementors.
//!
//!
//!
use std_::marker::PhantomData;
use std_::mem::ManuallyDrop;
#[allow(dead_code)]
union __Uninit<T: Copy> {
uninit: (),
value: T,
}
/**
Represents a zero-sized marker type .
Types implementing this trait are zero-sized and can safely be stored in any
`#[repr(C)]` type without changing their layout.
# Safety
Implementors of this trait must ensure:
- That the type is zero-sized,
- That it has an alignment of 1.
- That the type is trivially constructible, eg: by implementing [`Default`].
The easiest way to enforce the requirements of being zero-sized and
having an alignment of 1 is to have structs composed entirely of MarkerType fields
(ie: `PhantomData`).
# Built-in impls
This trait is not implemented for arrays because it's not yet clear
what the behavior of `#[repr(C)]` types will be with zero-sized arrays.
The ["repr(C) is unsound on MSVC targets" issue](https://github.com/rust-lang/rust/issues/81996)
could possibly require zero-length arrays(or `#[repr(C)]` structs)
in `#[repr(C)]` types not being zero sized on MSVC.
[`Default`]: https://doc.rust-lang.org/std/default/trait.Default.html
*/
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "marker_type")))]
pub unsafe trait MarkerType: Copy + Sized {
/// The value of Self.
#[allow(const_err)]
const MTVAL: Self = {
// this is safe since implementing MarkerType guarantees that
// this type is a Zero Sized Type ,which can't be uninitialized.
unsafe { (__Uninit::<Self> { uninit: () }).value }
};
#[inline(always)]
#[allow(const_err)]
/// Constructs a reference to Self.
fn markertype_ref<'a>() -> &'a Self
where
Self: 'a,
{
unsafe {
&*std_::ptr::NonNull::<Self>::dangling().as_ptr()
}
}
/// Constructs Self,this is possible because Self implements MarkerType.
#[inline(always)]
fn markertype_val() -> Self {
Self::MTVAL
}
}
#[doc(hidden)]
#[inline(always)]
pub fn assert_markertype<T: MarkerType>(){}
unsafe impl<T: ?Sized> MarkerType for PhantomData<T> {}
unsafe impl<T> MarkerType for ManuallyDrop<T>
where
T: MarkerType
{}
/*
// Uncomment once the rules around zero sized types in `#[repr(C)]` types are figured out,
// and it treats these types as zero sized.
// Then uncomment all `Uncomment` comments
//
// https://github.com/rust-lang/rust/issues/81996
unsafe impl MarkerType for () {}
////////////////////////////////
#[cfg(feature = "rust_1_51")]
macro_rules! impl_zero_sized_array {
()=>{
/// When the "const_params" feature is disabled,
/// the MarkerType trait is implemented for arrays up to 32 elements long.
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "const_params")))]
unsafe impl<T, const N: usize> MarkerType for [T; N]
where T: MarkerType
{}
}
}
#[cfg(feature = "rust_1_51")]
impl_zero_sized_array!{}
///////////////////////////////////
#[cfg(not(feature = "rust_1_51"))]
macro_rules! impl_zero_sized_array {
($($size:expr),*)=>{
$(
unsafe impl<T> MarkerType for [T;$size]
where T:MarkerType
{}
)*
}
}
#[cfg(not(feature = "rust_1_51"))]
impl_zero_sized_array! {
00,01,02,03,04,05,06,07,08,09,
10,11,12,13,14,15,16,17,18,19,
20,21,22,23,24,25,26,27,28,29,
30,31,32
}
////////////////////////////////
macro_rules! impl_zero_sized_tuple {
($($ty:ident),+) => (
unsafe impl<$($ty),*> MarkerType for ($($ty,)*)
where $($ty:MarkerType,)*
{}
)
}
impl_zero_sized_tuple! {A}
impl_zero_sized_tuple! {A,B}
impl_zero_sized_tuple! {A,B,C}
impl_zero_sized_tuple! {A,B,C,D}
impl_zero_sized_tuple! {A,B,C,D,E}
impl_zero_sized_tuple! {A,B,C,D,E,F}
impl_zero_sized_tuple! {A,B,C,D,E,F,G}
impl_zero_sized_tuple! {A,B,C,D,E,F,G,H}
impl_zero_sized_tuple! {A,B,C,D,E,F,G,H,I}
impl_zero_sized_tuple! {A,B,C,D,E,F,G,H,I,J}
impl_zero_sized_tuple! {A,B,C,D,E,F,G,H,I,J,K}
impl_zero_sized_tuple! {A,B,C,D,E,F,G,H,I,J,K,L}
impl_zero_sized_tuple! {A,B,C,D,E,F,G,H,I,J,K,L,M}
impl_zero_sized_tuple! {A,B,C,D,E,F,G,H,I,J,K,L,M,N}
impl_zero_sized_tuple! {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O}
impl_zero_sized_tuple! {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P}
*/
#[cfg(test)]
mod tests {
use std_::marker::PhantomData;
use std_::mem::{ManuallyDrop, align_of, size_of};
macro_rules! assert_size_align {
($ty:ty) => {
assert_eq!(size_of::<$ty>(), 0);
assert_eq!(align_of::<$ty>(), 1);
assert_eq!(size_of::<ManuallyDrop<$ty>>(), 0);
assert_eq!(align_of::<ManuallyDrop<$ty>>(), 1);
};
}
type PD = PhantomData<u64>;
#[test]
fn test_manuallydrop(){
assert_size_align!(ManuallyDrop<PD>);
// Uncomment at the same time as the impls
// assert_size_align!(ManuallyDrop<(PD,PD)>);
}
#[test]
fn test_alignment_size() {
assert_size_align!(());
assert_size_align!(PhantomData<()>);
assert_size_align!(PhantomData<u64>);
/*
// Uncomment at the same time as the impls
assert_size_align!((PD,));
assert_size_align!((PD, PD,));
assert_size_align!((PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,));
assert_size_align!((
PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,
));
assert_size_align!((
PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,
));
assert_size_align!((
PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,
));
assert_size_align!((
PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,
));
assert_size_align!((
PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,
));
assert_size_align!((
PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,
PD,
));
assert_size_align!((
PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD, PD,
PD, PD,
));
assert_size_align!([PD; 0]);
assert_size_align!([PD; 1]);
assert_size_align!([PD; 2]);
assert_size_align!([PD; 3]);
assert_size_align!([PD; 4]);
assert_size_align!([PD; 5]);
assert_size_align!([PD; 6]);
assert_size_align!([PD; 7]);
assert_size_align!([PD; 8]);
assert_size_align!([PD; 9]);
assert_size_align!([PD; 10]);
assert_size_align!([PD; 11]);
assert_size_align!([PD; 12]);
assert_size_align!([PD; 13]);
assert_size_align!([PD; 14]);
assert_size_align!([PD; 15]);
assert_size_align!([PD; 16]);
assert_size_align!([PD; 17]);
assert_size_align!([PD; 18]);
assert_size_align!([PD; 19]);
assert_size_align!([PD; 20]);
assert_size_align!([PD; 21]);
assert_size_align!([PD; 22]);
assert_size_align!([PD; 23]);
assert_size_align!([PD; 24]);
assert_size_align!([PD; 25]);
assert_size_align!([PD; 26]);
assert_size_align!([PD; 27]);
assert_size_align!([PD; 28]);
assert_size_align!([PD; 29]);
assert_size_align!([PD; 30]);
assert_size_align!([PD; 31]);
assert_size_align!([PD; 32]);
#[cfg(feature = "rust_1_51")]
assert_size_align!([PD; 63]);
*/
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////