Skip to main content

VertexLayout

Struct VertexLayout 

Source
pub struct VertexLayout<V, const N: usize, F> { /* private fields */ }
Expand description

Checked float-array field layout, not in C++ meshoptimizer.

V need not be plain old data: padding and other fields are never read. Use #[repr(C)] for a stable layout. The selector enforces the f32 element type and array size at compile time. Its address is checked against offset for every vertex when copying; an empty slice has no field address to check.

use core::mem::{offset_of, size_of};
use meshoptimizer_rs::{VertexFieldSnapshot, VertexLayout};

#[repr(C)]
struct Vertex {
    position: [f32; 3],
    normal: [f32; 3],
    uv: [f32; 2],
}
let vertices = [Vertex {
    position: [1.0, 2.0, 3.0], normal: [0.0, 0.0, 1.0], uv: [0.5, 0.5],
}];
let layout = VertexLayout::new(offset_of!(Vertex, position), |v: &Vertex| &v.position)?;
assert_eq!(layout.stride(), size_of::<Vertex>());
let positions = layout.copy_from(&vertices)?;
assert_eq!(positions.positions()?.get(0), Some([1.0, 2.0, 3.0]));
let normals = VertexFieldSnapshot::from_vertices(
    &vertices, offset_of!(Vertex, normal), |v| &v.normal,
)?;
assert_eq!(normals.attributes()?.components(), 3);
let uv = VertexFieldSnapshot::from_vertices(&vertices, offset_of!(Vertex, uv), |v| &v.uv)?;
assert_eq!(uv.attributes()?.components(), 2);

Non-float fields cannot be selected:

ⓘ
use meshoptimizer_rs::VertexLayout;
#[repr(C)]
struct Vertex { position: [u32; 3] }
let layout = VertexLayout::new(0, |v: &Vertex| &v.position);

The selector may be any Fn(&V) -> &[f32; N], including a capturing closure; the layout is Clone/Copy when the selector is.

Implementations§

Source§

impl<V, const N: usize, F> VertexLayout<V, N, F>

Source

pub const fn stride(&self) -> usize

Byte stride, equal to size_of::<V>().

Source

pub const fn offset(&self) -> usize

Byte offset of the selected field.

Source§

impl<V, const N: usize, F: Fn(&V) -> &[f32; N]> VertexLayout<V, N, F>

Source

pub fn new(offset: usize, field: F) -> Result<Self, VertexLayoutError>

Check a byte offset and typed field selector using V’s size/alignment.

§Errors

Returns InvalidStride, MisalignedField or InvalidFieldSize for an unsupported layout. A selector/offset mismatch is checked by copy_from.

Source

pub fn copy_from( &self, vertices: &[V], ) -> Result<VertexFieldSnapshot<N>, VertexLayoutError>

Copy the selected field into an initialized snapshot with the same layout.

Unselected bytes are zero, including padding. Float bit patterns are preserved, including non-finite values; geometric operations validate them. Allocation is caller-owned and outside Workspace limits.

§Errors

Returns SizeOverflow or AllocationFailed if the snapshot cannot be allocated; FieldOffsetMismatch if any selector result differs from the declared field address. Selector panics propagate to the caller.

Trait Implementations§

Source§

impl<V, const N: usize, F: Clone> Clone for VertexLayout<V, N, F>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V, const N: usize, F: Copy> Copy for VertexLayout<V, N, F>

Source§

impl<V, const N: usize, F> Debug for VertexLayout<V, N, F>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<V, const N: usize, F> Forget for VertexLayout<V, N, F>

§

impl<V, const N: usize, F> Freeze for VertexLayout<V, N, F>
where F: Freeze, PhantomData<fn(&V) -> &[f32; N]>: Freeze,

§

impl<V, const N: usize, F> RefUnwindSafe for VertexLayout<V, N, F>

§

impl<V, const N: usize, F> Send for VertexLayout<V, N, F>
where F: Send, PhantomData<fn(&V) -> &[f32; N]>: Send,

§

impl<V, const N: usize, F> Sync for VertexLayout<V, N, F>
where F: Sync, PhantomData<fn(&V) -> &[f32; N]>: Sync,

§

impl<V, const N: usize, F> Unpin for VertexLayout<V, N, F>
where F: Unpin, PhantomData<fn(&V) -> &[f32; N]>: Unpin,

§

impl<V, const N: usize, F> UnsafeUnpin for VertexLayout<V, N, F>

§

impl<V, const N: usize, F> UnwindSafe for VertexLayout<V, N, F>
where F: UnwindSafe, PhantomData<fn(&V) -> &[f32; N]>: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.