Skip to main content

AsBytes

Trait AsBytes 

Source
pub trait AsBytes {
    // Required methods
    fn as_bytes(&self) -> &[u8] ;
    fn len(&self) -> usize;

    // Provided methods
    fn to_bytes_vec(&self) -> Vec<u8>  { ... }
    fn byte_size(&self) -> usize { ... }
    fn is_empty(&self) -> bool { ... }
}
Expand description

Trait for borrowing data as byte slices. This trait abstracts the conversion of types that implement Pod (or collections thereof) into their raw byte representation as a slice (&[ u8]).

Required Methods§

Source

fn as_bytes(&self) -> &[u8]

Returns the underlying byte slice of the data.

Source

fn len(&self) -> usize

Returns the count of elements contained in the data. For single-element tuples (T,), this is 1. For collections (Vec< T >, &[ T ], [ T; N ]), this is the number of T items.

Provided Methods§

Source

fn to_bytes_vec(&self) -> Vec<u8>

Returns an owned vector containing a copy of the bytes of the data. The default implementation clones the bytes from as_bytes().

Source

fn byte_size(&self) -> usize

Returns the size in bytes of the data.

Source

fn is_empty(&self) -> bool

Returns true if the data contains no elements.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: Pod, const N: usize> AsBytes for [T; N]

Implementation for [T; N] where T is POD.

Source§

fn as_bytes(&self) -> &[u8]

Source§

fn byte_size(&self) -> usize

Source§

fn len(&self) -> usize

Source§

impl<T: Pod> AsBytes for (T,)

Implementation for single POD types wrapped in a tuple ( T, ).

Source§

fn as_bytes(&self) -> &[u8]

Source§

fn byte_size(&self) -> usize

Source§

fn len(&self) -> usize

Source§

impl<T: Pod> AsBytes for Vec<T>

Implementation for Vec< T > where T is POD.

Source§

fn as_bytes(&self) -> &[u8]

Source§

fn byte_size(&self) -> usize

Source§

fn len(&self) -> usize

Source§

impl<T: Pod> AsBytes for [T]

Implementation for [T] where T is POD.

Source§

fn as_bytes(&self) -> &[u8]

Source§

fn byte_size(&self) -> usize

Source§

fn len(&self) -> usize

Implementors§