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>
impl<V, const N: usize, F> VertexLayout<V, N, F>
Source§impl<V, const N: usize, F: Fn(&V) -> &[f32; N]> VertexLayout<V, N, F>
impl<V, const N: usize, F: Fn(&V) -> &[f32; N]> VertexLayout<V, N, F>
Sourcepub fn new(offset: usize, field: F) -> Result<Self, VertexLayoutError>
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.
Sourcepub fn copy_from(
&self,
vertices: &[V],
) -> Result<VertexFieldSnapshot<N>, VertexLayoutError>
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§
impl<V, const N: usize, F: Copy> Copy for VertexLayout<V, N, F>
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>
impl<V, const N: usize, F> Move for VertexLayout<V, N, F>
impl<V, const N: usize, F> RefUnwindSafe for VertexLayout<V, N, F>
impl<V, const N: usize, F> Send for VertexLayout<V, N, F>
impl<V, const N: usize, F> Sync for VertexLayout<V, N, F>
impl<V, const N: usize, F> Unpin for VertexLayout<V, N, F>
impl<V, const N: usize, F> UnsafeUnpin for VertexLayout<V, N, F>
impl<V, const N: usize, F> UnwindSafe for VertexLayout<V, N, F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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