Struct Tailed

Source
#[repr(C)]
pub struct Tailed<Head, Tail: ?Sized> { pub head: Head, pub tail: Tail, }
Expand description

A custom DST type.

One of the main usages is Tailed<H, [T]>, where the type has a flexible array member.

Fields§

§head: Head

Leading, sized data part of the type.

§tail: Tail

Trailing, unsized data part of the type.

Implementations§

Source§

impl<H, T> Tailed<H, [T]>

Source

pub fn from_raw_parts(data: *const H, count: usize) -> *const Self

Constructs a fat pointer from a pointer to the header and the count of elements in the trailing slice.

§Safety

It’s important to specify the count of the trailing elements rather than the size of the allocated data.


let slice = &[1, 2, 3, 4, 5];
let (ptr, len) = (slice as *const i32, slice.len());
let pointed = Tailed::<_, [i32]>::from_raw_parts(ptr, 4);
let pointed = unsafe { &*pointed };

let other = Tailed { head: 1i32, tail: [2, 3, 4, 5] };
assert_eq!(pointed, &other as &Tailed::<_, [i32]>);
assert_eq!(std::mem::size_of_val(pointed), std::mem::size_of_val(&other));
Source

pub fn from_raw_parts_mut(data: *mut H, count: usize) -> *mut Self

Constructs a fat pointer from a pointer to the header and the count of elements in the trailing slice.

§Safety

The pointer to header must be correctly aligned.

use std::boxed::Box;

let slice = vec![1, 2, 3, 4, 5].into_boxed_slice();
let (len, ptr) = (slice.len(), Box::into_raw(slice));
let pointed = Tailed::<_, [i32]>::from_raw_parts_mut(ptr as *mut i32, 4);
let pointed = unsafe { &mut *pointed };

let other = Tailed { head: 1i32, tail: [2, 3, 4, 5] };
assert_eq!(pointed, &other as &Tailed::<_, [i32]>);
assert_eq!(std::mem::size_of_val(pointed), std::mem::size_of_val(&other));
Source§

impl<H> Tailed<MaybeUninit<H>, [u8]>

Source

pub fn from_bytes(data: &[u8]) -> &Self

Creates a Tailed view into a slice of bytes.

#![feature(maybe_uninit_extra)]
use core::mem::MaybeUninit;

#[derive(PartialEq, Debug)]
#[repr(C)]
struct Header { a: u16, b: u8 };

let bytes: Box<[u8]> = vec![0x01, 0x01, 0x02, 0xFF, 0x03, 0x04].into();

let tailed = Tailed::<MaybeUninit<Header>, _>::from_bytes(&bytes);
assert_eq!(unsafe { tailed.head.read() }, Header {a: 0x0101, b: 0x02 });
assert_eq!(&tailed.tail, &[0x03, 0x04]);
§Safety

The passed reference MUST be aligned in a compatible fashion, as if it was an reference to dynamically-sized Self.

The data buffer also MUST be at least mem::size_of::<H>() bytes long and be properly aligned (e.g. to account for trailing padding).

Moreover, since we can’t guarantee that the raw bytes correspond to a correctly initialized value of type H, it is the caller’s responsibility to assert that.

§Panics

When any of the above safety preconditions are violated.

Source

pub fn from_bytes_with_length(data: &[u8], len: usize) -> &Self

Creates a Tailed view into a slice of bytes with specifically len trailing bytes (any bytes after that are simply treated as padding).

Valid trailing byte count range is [0; mem::size_of_val(data) - mem::size_of::<H>()]. Specify usize::MAX to include as many bytes in the tail byte slice as possible.

#![feature(maybe_uninit_extra)]
use core::mem::MaybeUninit;

#[derive(PartialEq, Debug)]
#[repr(C)]
struct Header { a: u16, b: u8 };

let bytes: Box<[u8]> = vec![0x01, 0x01, 0x02, 0xFF, 0x03, 0xFF].into();

let tailed = Tailed::<MaybeUninit<Header>, _>::from_bytes_with_length(&bytes, 1);
assert_eq!(unsafe { tailed.head.read() }, Header {a: 0x0101, b: 0x02 });
assert_eq!(&tailed.tail, &[0x03]);
// Header is 4 bytes and there are 2 trailing bytes, despite tail having 1.
assert_eq!(core::mem::size_of_val(tailed), 6);
let tailed = Tailed::<MaybeUninit<Header>, _>::from_bytes_with_length(&bytes, 0);
assert_eq!(&tailed.tail, &[]);
let tailed = Tailed::<MaybeUninit<Header>, _>::from_bytes_with_length(&bytes, core::usize::MAX);
assert_eq!(&tailed.tail, &[0x03, 0xFF]);
§Safety

In addition to upholding safety preconditions outlined in from_bytes, the passed len must be not bigger than the trailing byte count after header H type (including its padding).

§Panics

When any of the above preconditions are violated.

Source

pub unsafe fn from_bytes_unchecked_with_length(data: &[u8], len: usize) -> &Self

Unchecked version of from_bytes that can also specify the tail byte length manually.

Source

pub unsafe fn assume_init(&self) -> &Tailed<H, [u8]>

Assumes that the header part of the DST is correctly initialized, yielding a read-only typed view into underlying storage.

This does not invoke any drop glue for the inner type and only provides read access to the underlying value.

use core::mem::MaybeUninit;

#[derive(PartialEq, Debug)]
#[repr(C)]
struct Header { a: u16, b: u8 };

let bytes: Box<[u8]> = vec![0x01, 0x01, 0x02, 0xFF, 0x03, 0x04].into();

let tailed = Tailed::<MaybeUninit<Header>, _>::from_bytes(&bytes);
let init: &Tailed<Header, _> = unsafe { Tailed::from_bytes(&bytes).assume_init() };
assert_eq!(&init.head, &Header { a: 0x0101, b: 0x02});
assert_eq!(&init.tail, &[0x03, 0x04]);

Trait Implementations§

Source§

impl<Head: Debug, Tail: Debug + ?Sized> Debug for Tailed<Head, Tail>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Head: PartialEq, Tail: PartialEq + ?Sized> PartialEq for Tailed<Head, Tail>

Source§

fn eq(&self, other: &Tailed<Head, Tail>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Head, Tail: ?Sized> StructuralPartialEq for Tailed<Head, Tail>

Auto Trait Implementations§

§

impl<Head, Tail> Freeze for Tailed<Head, Tail>
where Head: Freeze, Tail: Freeze + ?Sized,

§

impl<Head, Tail> RefUnwindSafe for Tailed<Head, Tail>
where Head: RefUnwindSafe, Tail: RefUnwindSafe + ?Sized,

§

impl<Head, Tail> Send for Tailed<Head, Tail>
where Head: Send, Tail: Send + ?Sized,

§

impl<Head, Tail> Sync for Tailed<Head, Tail>
where Head: Sync, Tail: Sync + ?Sized,

§

impl<Head, Tail> Unpin for Tailed<Head, Tail>
where Head: Unpin, Tail: Unpin + ?Sized,

§

impl<Head, Tail> UnwindSafe for Tailed<Head, Tail>
where Head: UnwindSafe, Tail: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.