#![no_std]
#![allow(warnings)]
#[doc(hidden)]
pub mod docs {
pub mod prelude {
pub use crate::stability::StableABI;
pub use crate::transmute::{unsafe_transmute, StableTransmuteInto};
use crate::typic;
pub use core::mem;
pub use core::num::NonZeroU8;
#[typic::repr(C)]
#[derive(Default, StableABI)]
pub struct Padded(pub u8, pub u16, pub u8);
#[typic::repr(C)]
#[derive(Default, StableABI)]
pub struct Packed(pub u16, pub u16, pub u16);
#[typic::repr(C)]
#[derive(Default, StableABI)]
pub struct Constrained {
wizz: i8,
bang: u8,
}
impl Constrained {
pub fn new(wizz: i8, bang: u8) -> Self {
assert!((wizz as i16) / (bang as i16) >= 0);
Constrained { wizz, bang }
}
pub fn something_dangerous(&self) {
unsafe {
}
}
}
#[typic::repr(C)]
#[derive(Default, StableABI)]
pub struct Unconstrained {
pub wizz: u8,
pub bang: i8,
}
}
}
#[doc(hidden)]
#[deprecated(note = "TODO")]
pub enum TODO {}
#[doc(hidden)]
pub mod private {
pub mod bytelevel;
pub mod highlevel;
pub mod layout;
pub mod num;
pub mod stability;
pub mod target;
pub mod transmute;
}
#[doc(hidden)]
pub use private::highlevel as internal;
#[doc(inline)]
pub use typic_derive::repr;
#[doc(inline)]
pub use private::stability;
pub mod transmute;
mod typic {
pub use super::*;
}
pub mod layout {
use crate::internal::{Private, Public};
use crate::private::{layout, num};
use generic_array::ArrayLength;
pub trait Layout: layout::Layout<Public> {
type Size: num::Unsigned + ArrayLength<u8>;
type Align: num::Unsigned;
}
impl<T> Layout for T
where
T: layout::Layout<Public>,
{
type Size = <T as layout::Layout<Public>>::Size;
type Align = <T as layout::Layout<Public>>::Align;
}
pub type SizeOf<T> = <T as Layout>::Size;
pub type AlignOf<T> = <T as Layout>::Align;
}
pub mod extras {
pub mod zerocopy {
use crate::layout::*;
use crate::transmute::*;
use typenum::U1;
use generic_array::{ArrayLength as Length, GenericArray as Array};
pub unsafe trait FromBytes<O: TransmuteOptions = ()>
{}
unsafe impl<T, O: TransmuteOptions> FromBytes<O> for T
where
T: Layout + TransmuteFrom<Array<u8, SizeOf<T>>, O>
{}
pub unsafe trait AsBytes<O: TransmuteOptions = ()> {}
unsafe impl<T, O: TransmuteOptions> AsBytes<O> for T
where
T: Layout + TransmuteInto<Array<u8, SizeOf<T>>, O>
{}
pub trait Unaligned {}
impl<T> Unaligned for T
where
T: Layout<Align=U1>,
{}
}
pub mod bytemuck {
use crate::transmute::*;
use core::mem::{align_of, size_of, size_of_val};
pub fn try_cast_ref<'t, 'u, T, U>(src: &'t T) -> Option<&'u U>
where
&'t T: UnsafeTransmuteInto<&'u U, neglect::Alignment>,
{
if align_of::<U>() > align_of::<T>() && (src as *const T as usize) % align_of::<U>() != 0 {
None
} else {
Some(unsafe { src.unsafe_transmute_into() })
}
}
use core::slice;
use generic_array::{ArrayLength as Length, GenericArray as Array};
use crate::layout::*;
pub fn try_cast_slice<'t, 'u, T, U>(src: &'t [T]) -> Option<&'u [U]>
where
&'t Array<T, SizeOf<U>>: TransmuteInto<&'u Array<U, SizeOf<T>>>,
T: Layout,
U: Layout,
SizeOf<T>: 'u + Length<U>,
SizeOf<U>: 't + Length<T>,
{
if align_of::<U>() > align_of::<T>() && (src.as_ptr() as usize) % align_of::<U>() != 0 {
None
} else {
let len = size_of_val(src).checked_div(size_of::<U>()).unwrap_or(0);
Some(unsafe {
slice::from_raw_parts(src.as_ptr() as *const U, len)
})
}
}
}
}