devela::all

Trait ByteSized

Source
pub trait ByteSized: Sized {
    const BYTE_ALIGN: usize = _;
    const BYTE_SIZE: usize = _;

    // Provided methods
    fn byte_align(&self) -> usize  { ... }
    fn byte_size(&self) -> usize  { ... }
    fn ptr_size_ratio(&self) -> [usize; 2] { ... }
}
Expand description

Type size information in bytes.

This trait is automatically implemented for every Sized type.

Provided Associated Constants§

Source

const BYTE_ALIGN: usize = _

The alignment of this type in bytes.

Source

const BYTE_SIZE: usize = _

The size of this type in bytes.

Provided Methods§

Source

fn byte_align(&self) -> usize

Returns the alignment of this type in bytes.

Source

fn byte_size(&self) -> usize

Returns the size of this type in bytes.

Ignores any allocated resources in the heap.

Source

fn ptr_size_ratio(&self) -> [usize; 2]

Returns the size ratio between Ptr::BYTES and BYTE_SIZE.

For example: the ratio will be (1, 1) if both sizes are the same, (2, 1) if a pointer is double the byte size, and (1, 2) if a pointer is half the byte size.

§Example
use devela::ByteSized;

assert_eq![().ptr_size_ratio(), [1, 0]];
assert_eq![1_usize.ptr_size_ratio(), [1, 1]];
assert_eq!["slice".ptr_size_ratio(), [1, 2]];
assert_eq![String::from("hello").ptr_size_ratio(), [1, 3]];

#[cfg(target_pointer_width = "64")]
{
    assert_eq![0_u8.ptr_size_ratio(), [8, 1]];
    assert_eq![0_u16.ptr_size_ratio(), [4, 1]];
    assert_eq![0_u32.ptr_size_ratio(), [2, 1]];
    assert_eq![0_u64.ptr_size_ratio(), [1, 1]];
    assert_eq![0_u128.ptr_size_ratio(), [1, 2]];
    assert_eq!['c'.ptr_size_ratio(), [2, 1]];
    assert_eq!["slice".ptr_size_ratio(), [1, 2]];
}

For the const version see Ptr::size_ratio.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> ByteSized for T