Struct image_texel::Texel
source · [−]pub struct Texel<P: ?Sized>(_);
Expand description
Marker struct to denote a texel type.
Can be constructed only for types that have expected alignment and no byte invariants. It
always implements Copy
and Clone
, regardless of the underlying type and is zero-sized.
This is the central encapsulation of unsafety in this crate. It utilizes bytemuck
for a safe
interface but permits other types with an unsafe interface, and offers the cast operations
without a bound on the Pod
trait. Note that Pod
is a pure marker trait; its properties must
hold even if it is not explicitly mentioned. If all constructors (safely or unsafely) ensure
that its properties hold we can use Texel
as a witness type for the bound and subsequently
write interfaces to take an instance instead of having a static type bound. This achieves two
effects:
- Firstly, it makes the interface independent of the chosen transmutation crate. Potentially we
will have a method to construct the
Texel
via acore
trait. - Secondly, it allows creating texel of third-party types for which the bound can not be implemented. Crucially, this includes SIMD representations that would be a burden to support directly. And conversely you can also deal with arbitrary existing texel without a bound in your own interfaces!
Implementations
sourceimpl<P: Pod> Texel<P>
impl<P: Pod> Texel<P>
sourcepub const fn for_type() -> Option<Self>
pub const fn for_type() -> Option<Self>
Try to construct an instance of the marker.
If successful, you can freely use it to access the image buffers. This requires:
- The type must have an alignment of at most
MAX_ALIGN
. - The type must not be a ZST.
- The type must not have any Drop-glue (no drop, any contain not part that is Drop).
sourceimpl<P> Texel<P>
impl<P> Texel<P>
sourcepub const unsafe fn new_unchecked() -> Self
pub const unsafe fn new_unchecked() -> Self
Create a witness certifying P
as a texel without checks.
Safety
The type P
must not:
- have any validity invariants, i.e. is mustn’t contain any padding.
- have any safety invariants. This implies it can be copied.
- have an alignment larger than
MaxAligned
. - be a zero-size type.
Furthermore, tentatively, the type must not have any drop glue. That is its members are all simple types without Drop implementations. This requirement exists mainly to avoid code accidentally leaking instances, and ensures that copies created from their byte representation—which is safe according to the other invairants— do not cause unexpected effects.
sourcepub const fn size_nz(self) -> NonZeroUsize
pub const fn size_nz(self) -> NonZeroUsize
Publicly visible function to use the guarantee of non-ZST.
sourcepub const fn array<const N: usize>(self) -> Texel<[P; N]>
pub const fn array<const N: usize>(self) -> Texel<[P; N]>
Construct a texel as an array of no elements.
Panics
This function panics when called with N
equal to 0.
sourcepub const fn transparent_wrap<O>(
self,
_: IsTransparentWrapper<P, O>
) -> Texel<O>
pub const fn transparent_wrap<O>(
self,
_: IsTransparentWrapper<P, O>
) -> Texel<O>
Construct a texel by wrapping into a transparent wrapper.
TODO: a constructor for Texel
sourcepub const fn transparent_unwrap<O>(
self,
_: IsTransparentWrapper<O, P>
) -> Texel<O>
pub const fn transparent_unwrap<O>(
self,
_: IsTransparentWrapper<O, P>
) -> Texel<O>
Construct a texel by unwrapping a transparent wrapper.
sourcepub const fn num_wrapping(self) -> Texel<Wrapping<P>>
pub const fn num_wrapping(self) -> Texel<Wrapping<P>>
Construct a texel that contains a number in the standard Wrapping
type.
sourceimpl<T, const N: usize> Texel<[T; N]>
impl<T, const N: usize> Texel<[T; N]>
sourcepub const fn array_element(self) -> Texel<T>
pub const fn array_element(self) -> Texel<T>
Construct a texel, from an array of elements.
sourceimpl<P> Texel<P>
impl<P> Texel<P>
Operations that can be performed based on the evidence of Texel.
sourcepub fn copy_val(self, val: &P) -> P
pub fn copy_val(self, val: &P) -> P
Copy a texel.
Note that this does not require Copy
because that requirement was part of the
requirements of constructing this Texel
witness.
sourcepub fn to_slice<'buf>(self, buffer: &'buf [MaxAligned]) -> &'buf [P]
pub fn to_slice<'buf>(self, buffer: &'buf [MaxAligned]) -> &'buf [P]
Reinterpret a slice of aligned bytes as a slice of the texel.
Note that the size (in bytes) of the slice will be shortened if the size of P
is not a
divisor of the input slice’s size.
sourcepub fn to_mut_slice<'buf>(self, buffer: &'buf mut [MaxAligned]) -> &'buf mut [P]
pub fn to_mut_slice<'buf>(self, buffer: &'buf mut [MaxAligned]) -> &'buf mut [P]
Reinterpret a slice of aligned bytes as a mutable slice of the texel.
Note that the size (in bytes) of the slice will be shortened if the size of P
is not a
divisor of the input slice’s size.
sourcepub fn try_to_slice<'buf>(self, bytes: &'buf [u8]) -> Option<&'buf [P]>
pub fn try_to_slice<'buf>(self, bytes: &'buf [u8]) -> Option<&'buf [P]>
Try to reinterpret a slice of bytes as a slice of the texel.
This returns Some
if the buffer is suitably aligned, and None
otherwise.
sourcepub fn try_to_slice_mut<'buf>(
self,
bytes: &'buf mut [u8]
) -> Option<&'buf mut [P]>
pub fn try_to_slice_mut<'buf>(
self,
bytes: &'buf mut [u8]
) -> Option<&'buf mut [P]>
Try to reinterpret a slice of bytes as a slice of the texel.
This returns Some
if the buffer is suitably aligned, and None
otherwise.
sourcepub fn to_bytes<'buf>(self, texel: &'buf [P]) -> &'buf [u8]
pub fn to_bytes<'buf>(self, texel: &'buf [P]) -> &'buf [u8]
Reinterpret a slice of texel as memory.
Note that you can convert a reference to a single value by core::slice::from_ref
.
sourcepub fn to_mut_bytes<'buf>(self, texel: &'buf mut [P]) -> &'buf mut [u8]
pub fn to_mut_bytes<'buf>(self, texel: &'buf mut [P]) -> &'buf mut [u8]
Reinterpret a mutable slice of texel as memory.
Note that you can convert a reference to a single value by core::slice::from_mut
.
Trait Implementations
sourceimpl<P> From<Texel<P>> for TexelLayout
impl<P> From<Texel<P>> for TexelLayout
Convert a pixel to an element, discarding the exact type information.
sourceimpl<P> Ord for Texel<P>
impl<P> Ord for Texel<P>
sourceimpl<P> PartialOrd<Texel<P>> for Texel<P>
impl<P> PartialOrd<Texel<P>> for Texel<P>
sourcefn partial_cmp(&self, _: &Self) -> Option<Ordering>
fn partial_cmp(&self, _: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<P> TryMend<MatrixBytes> for Texel<P>
impl<P> TryMend<MatrixBytes> for Texel<P>
Try to use the matrix with a specific pixel type.
type Into = Matrix<P>
type Err = MismatchedPixelError
fn try_mend(self, matrix: &MatrixBytes) -> Result<Matrix<P>, Self::Err>
sourceimpl<P> TryMend<StridedBytes> for Texel<P>
impl<P> TryMend<StridedBytes> for Texel<P>
Try to use the matrix with a specific pixel type.
type Into = Strides<P>
type Err = MismatchedPixelError
fn try_mend(self, matrix: &StridedBytes) -> Result<Strides<P>, Self::Err>
impl<P> Copy for Texel<P>
This is a pure marker type.
impl<P> Eq for Texel<P>
Auto Trait Implementations
impl<P: ?Sized> RefUnwindSafe for Texel<P> where
P: RefUnwindSafe,
impl<P: ?Sized> Send for Texel<P> where
P: Send,
impl<P: ?Sized> Sync for Texel<P> where
P: Sync,
impl<P: ?Sized> Unpin for Texel<P> where
P: Unpin,
impl<P: ?Sized> UnwindSafe for Texel<P> where
P: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more