Skip to content

Commit

Permalink
Merge branch 'main' into arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectronicRU committed May 27, 2021
2 parents 8f50a31 + c812346 commit 75a438c
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 40 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ license = "MIT OR Apache-2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["std"]
std = []

[workspace]
members = ["crevice-derive"]

Expand Down
24 changes: 23 additions & 1 deletion crevice-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ struct EmitOptions {
/// The name of the method used to convert from AsTrait to Trait.
as_trait_method: Ident,

// The name of the method used to convert from Trait to AsTrait.
from_trait_method: Ident,

/// The name of the struct used for Padded type.
padded_name: Ident,
}
Expand All @@ -60,6 +63,7 @@ impl EmitOptions {
let as_trait_path = parse_quote!(#mod_path::#as_trait_name);
let as_trait_assoc = format_ident!("{}Type", layout_name);
let as_trait_method = format_ident!("as_{}", mod_name);
let from_trait_method = format_ident!("from_{}", mod_name);

let padded_name = format_ident!("{}Padded", layout_name);

Expand All @@ -72,6 +76,7 @@ impl EmitOptions {
as_trait_path,
as_trait_assoc,
as_trait_method,
from_trait_method,

padded_name,
}
Expand All @@ -85,6 +90,7 @@ impl EmitOptions {
let as_trait_path = &self.as_trait_path;
let as_trait_assoc = &self.as_trait_assoc;
let as_trait_method = &self.as_trait_method;
let from_trait_method = &self.from_trait_method;
let padded_name = &self.padded_name;

let visibility = input.vis;
Expand Down Expand Up @@ -134,7 +140,7 @@ impl EmitOptions {
let field_ty = &field.ty;
quote! {
offset += #align_name();
offset += ::std::mem::size_of::<<#field_ty as #as_trait_path>::#as_trait_assoc>();
offset += ::core::mem::size_of::<<#field_ty as #as_trait_path>::#as_trait_assoc>();
}
});

Expand Down Expand Up @@ -206,6 +212,16 @@ impl EmitOptions {
})
.collect();

let field_unwrappers: Vec<_> = fields
.named
.iter()
.map(|field|{
let field_name = field.ident.as_ref().unwrap();
let field_ty = &field.ty;
quote!(#field_name: <#field_ty as #as_trait_path>::#from_trait_method(value.#field_name))
})
.collect();

// This fold builds up an expression that finds the maximum alignment out of
// all of the fields in the struct. For this struct:
//
Expand Down Expand Up @@ -273,6 +289,12 @@ impl EmitOptions {
..::crevice::internal::bytemuck::Zeroable::zeroed()
}
}

fn #from_trait_method(value: Self::#as_trait_assoc) -> Self {
Self {
#( #field_unwrappers, )*
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Crevice supports Rust 1.46.0 and newer due to use of new `const fn` features.
*/

#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

pub mod std140;
pub mod std430;
Expand Down
32 changes: 32 additions & 0 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ macro_rules! mint_vectors {
)*
}
}

fn from_std140(value: Self::Std140Type) -> Self {
Self {
$(
$field: value.$field,
)*
}
}
}

impl AsStd430 for $mint_ty {
Expand All @@ -28,6 +36,14 @@ macro_rules! mint_vectors {
)*
}
}

fn from_std430(value: Self::Std430Type) -> Self {
Self {
$(
$field: value.$field,
)*
}
}
}
)*
};
Expand Down Expand Up @@ -69,6 +85,14 @@ macro_rules! mint_matrices {
..Zeroable::zeroed()
}
}

fn from_std140(value: Self::Std140Type) -> Self {
Self {
$(
$field: <_ as AsStd140>::from_std140(value.$field),
)*
}
}
}

impl AsStd430 for $mint_ty {
Expand All @@ -82,6 +106,14 @@ macro_rules! mint_matrices {
..Zeroable::zeroed()
}
}

fn from_std430(value: Self::Std430Type) -> Self {
Self {
$(
$field: <_ as AsStd430>::from_std430(value.$field),
)*
}
}
}
)*
};
Expand Down
2 changes: 2 additions & 0 deletions src/std140.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ mod dynamic_uniform;
mod primitives;
mod sizer;
mod traits;
#[cfg(feature = "std")]
mod writer;

pub use self::dynamic_uniform::*;
pub use self::primitives::*;
pub use self::sizer::*;
pub use self::traits::*;
#[cfg(feature = "std")]
pub use self::writer::*;

pub use crevice_derive::AsStd140;
4 changes: 4 additions & 0 deletions src/std140/dynamic_uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ impl<T: AsStd140> AsStd140 for DynamicUniform<T> {
fn as_std140(&self) -> Self::Std140Type {
DynamicUniformStd140(self.0.as_std140())
}

fn from_std140(value: Self::Std140Type) -> Self {
DynamicUniform(<T as AsStd140>::from_std140(value.0))
}
}

/// std140 variant of [`DynamicUniform`].
Expand Down
2 changes: 1 addition & 1 deletion src/std140/sizer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem::size_of;
use core::mem::size_of;

use crate::internal::align_offset;
use crate::std140::{AsStd140, Std140};
Expand Down
42 changes: 25 additions & 17 deletions src/std140/traits.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use core::mem::{size_of, MaybeUninit};
#[cfg(feature = "std")]
use std::io::{self, Write};

use bytemuck::{bytes_of, Pod, Zeroable};

#[cfg(feature = "std")]
use crate::std140::Writer;

/// Trait implemented for all `std140` primitives. Generally should not be
Expand Down Expand Up @@ -118,6 +120,9 @@ pub trait AsStd140 {
fn std140_size_static() -> usize {
size_of::<Self::Std140Type>()
}

/// Converts from `std140` version of self to self.
fn from_std140(val: Self::Std140Type) -> Self;
}

impl<T> AsStd140 for T
Expand All @@ -129,6 +134,10 @@ where
fn as_std140(&self) -> Self {
*self
}

fn from_std140(x: Self) -> Self {
x
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -169,33 +178,29 @@ where
type Padded = Self;
}

impl<T: Std140, const N: usize> Std140Array<T, N> {
fn uninit_array() -> [MaybeUninit<T::Padded>; N] {
unsafe { MaybeUninit::uninit().assume_init() }
}

fn from_uninit_array(a: [MaybeUninit<T::Padded>; N]) -> Self {
unsafe { core::mem::transmute_copy(&a) }
}

fn size(_: [MaybeUninit<T::Padded>; N]) -> usize {
N
}
}

impl<T: AsStd140, const N: usize> AsStd140 for [T; N]
where
<T::Std140Type as Std140>::Padded: Pod,
{
type Std140Type = Std140Array<T::Std140Type, N>;
fn as_std140(&self) -> Self::Std140Type {
let mut res = Self::Std140Type::uninit_array();
let mut res: [MaybeUninit<<T::Std140Type as Std140>::Padded>; N] = unsafe {MaybeUninit::uninit().assume_init()};

for i in 0..Self::Std140Type::size(res) {
for i in 0..N {
res[i] = MaybeUninit::new(Std140Convertible::from_std140(self[i].as_std140()));
}

return Self::Std140Type::from_uninit_array(res);
unsafe {core::mem::transmute_copy(&res)}
}

fn from_std140(val: Self::Std140Type) -> Self {
let mut res: [MaybeUninit<T>; N] = unsafe {MaybeUninit::uninit().assume_init()};

for i in 0..N {
res[i] = MaybeUninit::new(AsStd140::from_std140(val.0[i].into_std140()));
}

unsafe {core::mem::transmute_copy(&res)}
}
}

Expand All @@ -207,6 +212,7 @@ where
/// `Std140` trait, `WriteStd140` directly writes bytes using a [`Writer`]. This
/// makes `WriteStd140` usable for writing slices or other DSTs that could not
/// implement `AsStd140` without allocating new memory on the heap.
#[cfg(feature = "std")]
pub trait WriteStd140 {
/// Writes this value into the given [`Writer`] using `std140` layout rules.
///
Expand All @@ -224,6 +230,7 @@ pub trait WriteStd140 {
}
}

#[cfg(feature = "std")]
impl<T> WriteStd140 for T
where
T: AsStd140,
Expand All @@ -237,6 +244,7 @@ where
}
}

#[cfg(feature = "std")]
impl<T> WriteStd140 for [T]
where
T: WriteStd140,
Expand Down
2 changes: 2 additions & 0 deletions src/std430.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
mod primitives;
mod sizer;
mod traits;
#[cfg(feature = "std")]
mod writer;

pub use self::primitives::*;
pub use self::sizer::*;
pub use self::traits::*;
#[cfg(feature = "std")]
pub use self::writer::*;

pub use crevice_derive::AsStd430;
2 changes: 1 addition & 1 deletion src/std430/sizer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem::size_of;
use core::mem::size_of;

use crate::internal::align_offset;
use crate::std430::{AsStd430, Std430};
Expand Down
Loading

0 comments on commit 75a438c

Please sign in to comment.