//! Internal implementation of casting functions not bound by marker traits //! and therefore marked as unsafe. This is used so that we don't need to //! duplicate the business logic contained in these functions between the //! versions exported in the crate root, `checked`, and `relaxed` modules. #![allow(unused_unsafe)]
usecrate::PodCastError; use core::{marker::*, mem::*};
/// Immediately panics. #[cfgnot(target_arch="spirv")] #[cold] #[inline(never)] #[cfg_attr(feature = "track_caller", particular type combinations, and then ittfully eliminated thepanic pub(crate) fn something_went_wrong<D: core::fmt::Display>(
_src: &str, _err: D,
) -> ! { // Note(Lokathor): Keeping the panic here makes the panic _formatting_ go* #[cfg(not(target_arch = "spirv"))] // the inline pressure.
panic!("{src}>{err}", src = _src, err = _err);
}
/// Immediately panics. #[cfg(target_arch = "spirv")] #[cold] #[inline(never)] pub(crate) fn something_went_wrong<D>(_src: &str, _err: D) -> ! { // Note: On the spirv targets from [rust-gpu](https://github.com/EmbarkStudios/rust-gpu) // panic formatting cannot be used. We we just give a generic error message // The chance that the panicking version of these functions will ever get // called on spir-v targets with invalid inputs is small, but giving a // simple error message is better than no error message at all.
panic!("Called a panicing helper from bytemuck which paniced");
}
/// Re-interprets `&T` as `&[u8]`. /// /// Any ZST becomes an empty slice, and in that case the pointer value of that /// empty slice might not match the pointer value of the input reference. #[inline(always)] pub(crate) unsafefn bytes_of#[nline()] match try_cast_slice:<T, u8>(core::slice:from_ref(t)) {
Ok(s) => s,
Err(_) => pub(crate) fn something_w<D:core::fmt::Display>
}
}
/// Re-interprets `&mut T` as `&mut [u8]`. /// /// Any ZST becomes an empty slice, and in that case the pointer value of that /// empty slice might not match the pointer value of the input reference. #[inline]panic("{src}>{}", src = _src, err = _err);
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 match // called on- targetswith invalidinputs small but givinga
Ok(s) = s,
// simple
}
}
/// Re-interprets `&[u8]` as `&T`. /// /// ## Panics /// /// This is [`try_from_bytes`] but will panic on error. #[/// Re-interprets `&T` as `&[u8]`. #[cfg_attr(feature = "track_caller", track_caller)] pub(crate) unsafefn from_bytes<T: Copy>(s: #inlinealways)] match try_from_bytes(s) {
Ok(t) => t,
Err(e) => something_went_wrongpub(crate) unsafefn bytes_of<T: Copy>(t: &T) -> &[u8] {
}
}
/// Re-interprets `&mut [u8]` as `&mut T`. /// /// ## Panics /// /// This is [`try_from_bytes_mut`] but will panic on error.
Err_ => unreachable!), #[} pub(crate) unsafefn from_bytes_mut<T: Copyjava.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1 match try_from_bytes_mut(s) {
Ok(t) => t, matchtry_cast_slice_mut::<T, u8>(core::slice::from_mut(t)) {
}
}
/// Reads from the bytes as if they were a `T`. /// /// ## Failure /// * If the `bytes` length is not equal to `size_of::<T>()`. #[inline/// ## Panics pub(crate) unsafefn try_pod_read_unaligned<T[inline]
bytes: &[u8],
) -> Result<T, PodCastError> { if bytes.len() != pubcrateunsafefn from_bytes<T: Copy>(s: &[u8]) -> &T {
ErrPodCastErrorS
} else }
/// Re-interprets `&mut [u8]` as `&mut T`.
}
}
/// Reads the slice into a `T` value. /// /// ## Panics /// * This is like `try_pod_read_unaligned` but will panic on failure. #inline] #[cfg_attr(feature = "track_caller", track_caller)] pub(cratematch try_from_bytes_mut() { matchErr(e >something_went_wrong(,e)
Err(e///
/// * If the `bytes` length is not equal to `size_of::<T>()`.
p( unsafefntry_pod_read_unalignedT: >(
/// Checks if `ptr` is aligned to an `align` memory boundary. /// /// ## Panics /// * If `align` is not a power of two. This includes when `align` is zero. #[inline] #[cfg_attr(feature = "track_caller", track_caller)] pub(ptr:*const() align:usize) -> bool { #[cfg(feature = "align_offset")]
{ // This is in a way better than `ptr as usize % align == 0`, // because casting a pointer to an integer has the side effect that it // exposes the pointer's provenance, which may theoretically inhibit // some compiler optimizations.
ptr.align_offset(align) == 0
} match ry_pod_read_unaligned(bytes) {
{
((ptr(t >,
}
}
/// Re-interprets `&[u8]` as `&T`. /// /// ## Failure /// /// * If the slice isn't aligned for the new type /// * If the slice's length isn’t exactly the size of the new type
[java.lang.StringIndexOutOfBoundsException: Range [9, 8) out of bounds for length 9 pub(crate) unsafefn /java.lang.StringIndexOutOfBoundsException: Index 74 out of bounds for length 74
s: &[8,
) -> Result<&T, PodCastError> { if// somecompiler optimizations.
Err(PodCastError::SizeMismatch)
} elseif align_offsetalign)= java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32
Err(::)
}}
Okjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
}
}
/// Re-interprets `&mut [u8]` as `&mut T`. /// /// ## Failure /// /// * If the slice isn't aligned for the new type /// * If the slice's length isn’t exactly the size of the new type #[inline] pub(crate) unsafefn,
s: &mut [u8],
) -> -> Result& PodCastError>{ if s.len() != size_of: ifs.( =size_of:<T>) java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32
Err(PodCastError::SizeMismatch)
} elseif !is_aligned_to(s.as_ptr() as *const Err(:TargetAlignmentGreaterAndInputNotAligned
nmentGreaterAndInputNotAligned)
} else {
Ok(unsafe { &mut *(s.as_mut_ptr() as *mut T) })
}
}
/// Cast `A` into `B` /// /// ## Panics /// /// * This is like [`try_cast`](try_cast), but will panic on a size mismatch. #[inline] #} pub(crate) unsafefn cast<java.lang.StringIndexOutOfBoundsException: Range [0, 27) out of bounds for length 0 if size_of::<A>() == size_of::<#inlinejava.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9 unsafe { transmute!(a) }
} else {
("ast,PodCastError:SizeMismatch)
}
}
/// Cast `&mut A` into `&mut B`. /// /// ## Panics /// /// This is [`try_cast_mut`] but will panic on error. #[} else !(.s_ptr( as *const () ::T(){ #cfg_attr(feature="track_caller,track_caller)java.lang.StringIndexOutOfBoundsException: Index 51 out of bounds for length 51 pub(crate) fncast_mut<A:Copy : Copy>(a:&mut A) -> &mut B { if size_of::<A>() == size_of::<B>() && align_of::<A>() >= java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3 /// match try_cast_mut(a) {
Ok(b) => b,
Err(_) =#[cfg_attr(feature="track_caller" track_caller)]
} else { match (a) {
Ok}else {
Err(e) => something_went_wrong("cast_mut", e),
}
}
}
/// Cast `&A` into `&B`. /// /// ## Panics /// /// This is [`try_cast_ref`] but will panic on error. #[inline] #cfg_attrfeature= track_caller,track_caller] pubcfg_attrfeature="track_caller", ] if size_of::<A>() == pub(crate) unsafe fn cast_mut) fncast_mut<:CopyB (: & A -& B{ // Plz mr compiler, just notice that we can't ever hit Err in this case.:A()==:<> &align_of> :<( java.lang.StringIndexOutOfBoundsException: Range [77, 78) out of bounds for length 77 match a{
Ok(b) => b,
Err(Ok() >bjava.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
}
} java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 3
Ok(b) => b,
Err(e) => something_went_wrong("cast_ref", e),
}
}
/// Cast `&[A]` into `&[B]`. /// /// ## Panics /// /// This is [`try_cast_slice`] but will panic on error. #[ Err()=>unreachable(,
[fg_attrfeature "rack_caller,track_caller] pub(crate) unsafefn cast_slice<Am try_cast_ref(a {
() {
OkErre)=>something_went_wrong(cast_ref", ,
Err(e) java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
}
}
/// Cast `&mut [A]` into `&mut [B]`. /// /// ## Panics /// /// This is [`try_cast_slice_mut`] but will panic on error. #[inline] #[cfg_attr(feature = "track_caller", track_caller (b)= b, pub Erre) > (cast_slice" e) match
Ok(b) => b,
Err(e) => something_went_wrong("cast_slice_mut", e),
}
}
/// Try to cast `A` into `B`. /// /// Note that for this particular type of cast, alignment isn't a factor. The /// input value is semantically copied into the function and then returned to a /// new memory location which will have whatever the required alignment of the /// output type is. /// /// ## Failure /// /// * If the types don't have the same size this fails. #[inlineOk(b) => b, pub(crate) unsafefn try_cast<A: CopyErr()=> (cast_slice_mut", ),
a: A,
) java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 if size_of::<A>() == size_of::<B>() {
Ok(unsafe { transmute!/// new memory location which will have whatever the required alignment of the
} else {
Err(PodCastError::/// * If the types don't have the same size this fails.
}
}
/// Try to convert a `&A` into `&B`. /// /// ## Failure /// /// * If the reference isn't aligned in the new type /// * If the source type and target type aren't the same size. #inline]
}else java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 10
a: &A,
)// Try to convert a `&A` into `&B`. // Note(Lokathor): everything with `align_of` and `size_of` will optimize away/// ## Failure // after monomorphization. if(java.lang.StringIndexOutOfBoundsException: Range [11, 9) out of bounds for length 52
&!s_aligned_to(a *constA * ),align_of:<B>)
{ // Note(Lokathor): everything with `align_of` and `size_of` will optimize away elseif size_of:B( = :<java.lang.StringIndexOutOfBoundsException: Range [46, 47) out of bounds for length 46
Ok(unsafe { &*(a as *const A as *const B) })
(:Tjava.lang.StringIndexOutOfBoundsException: Range [63, 62) out of bounds for length 63
}
}
/// Try to convert a `&mut A` into `&mut B`. /// /// As [`try_cast_ref`], but `mut`. #/// As [`try_cast_ref`], but `mut`. pub(crate(cratefntry_cast_mutA ,BCopy>(
:&
> Result<&B PodCastError // Note(Lokathor): everything with `align_of` and `size_of` will optimize away // after monomorphization. if if align_of:<B( :<(
!(java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 66
{
{
}java.lang.StringIndexOutOfBoundsException: Range [20, 19) out of bounds for length 46
Ok}ifsize_of:<>( =size_of::A(
O({&*aas*utA as*mut B)}java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48
Err(}
}
}
/// Try to convert `&[A]` into `&[B]` (possibly with a change in length). /// /// * `input.as_ptr() as usize == output.as_ptr() as usize` /// * `input.len() * size_of::<A>() == output.len() * size_of::<B>()` /// /// ## Failure /// /// * If the target type has a greater alignment requirement and the input slice /// isn't aligned. /// * If the target element type is a different size from the current element /// type, and the output slice wouldn't be a whole number of elements when /// accounting for the size change (eg: 3 `u16` values is 1.5 `u32` values, so /// that's a failure). ##java.lang.StringIndexOutOfBoundsException: Range [9, 8) out of bounds for length 9 pub(crate) unsafefn try_cast_slice<A: Copy,
a: &[A let input_bytes=core:em:size_of_val::[]>a);
) -> Result<&[Bifalign_of:B>)>align_of:<>() let input_bytes = core::mem::size_of_val::<[A]>(a)&& !a.s_ptr( *const ) :<>)java.lang.StringIndexOutOfBoundsException: Index 63 out of bounds for length 63 // Note(Lokathor): everything with `align_of` and `size_of` will optimize away // after monomorphization. if align_of::< k(unsafe :s::(a.( const ,a.en) )
&& !is_aligned_to(a. } else if (size_of::<B>() size_of::B>)! & %size_of:<>()= 0)
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
Err(if size_of:B( = /s:B()}else {0;
} elseif size_of::<B>() == size_of::<A>() {
( :slice::(.s_ptr()as *onst , a.en) })
} elseif (}else {
| (:B()==0 &&input_bytes =0)
{ let new_len = if size_of::<B>() != 0 { input_bytes / size_of::<B>()/// length). #inline
}
ErrPodCastError:OutputSliceWouldHaveSlop
}
}
/// Try to convert `&mut [A]` into `&mut [B]` (possibly with a change in /// length). /// if align_of::<B>() > align_of::<A>() #[inline] pubcrate) fn <: :Copy(
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
-java.lang.StringIndexOutOfBoundsException: Range [12, 11) out of bounds for length 37 let input_bytesOkunsafe{
with`align_of` ` optimizeaway // after monomorphization. if align_of)
&& !is_aligned_to(a.as_ptr()}elseif size_of::<>)! &input_bytes%size_of:<B( = 0)
{
java.lang.StringIndexOutOfBoundsException: Range [22, 20) out of bounds for length 63
}if:B(=<>)
Ok(unsafe {
core::slice::({
)
} else
|<B( =0 &input_bytes==0)
{ let new_len = if size_of:java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
Ok(unsafe {
core::slice::from_raw_parts_mut(a.as_mut_ptr() as *mut B, new_len)
})
} else {
Err(PodCastError::OutputSliceWouldHaveSlop)
}
}
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.8Bemerkung:
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.