Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ futures = { version = "0.3", features = ["executor"], default-features = false }
trybuild = { version = "1", default-features = false }
test_impl = { path = "./test_impl" }

[features]
default = ["alloc"]
alloc = []

[target.'cfg(target_family = "unix")'.dev-dependencies]
pprof = { version = "0.14", features = ["criterion", "flamegraph"], default-features = false }
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ assert_eq!(offsets, [0, 64, 192]);
Supports writing to uninitialized memory as well.

```rust
use std::mem::MaybeUninit;
use core::mem::MaybeUninit;
use encase::{ShaderType, DynamicStorageBuffer};

let mut uninit_buffer: Vec<MaybeUninit<u8>> = Vec::new();
Expand All @@ -157,7 +157,7 @@ let byte_buffer: Vec<u8> = unsafe {
)
};

std::mem::forget(uninit_buffer);
core::mem::forget(uninit_buffer);

// write byte_buffer to GPU

Expand Down
1 change: 1 addition & 0 deletions src/core/alignment_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl AlignmentValue {
#[cfg(test)]
mod test {
use super::AlignmentValue;
use alloc::format;

#[test]
fn new() {
Expand Down
31 changes: 25 additions & 6 deletions src/core/rw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::ShaderType;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::mem::MaybeUninit;
use thiserror::Error;

Expand Down Expand Up @@ -177,8 +179,9 @@ impl<B: BufferMut> Cursor<B> {
#[error("could not enlarge buffer")]
pub struct EnlargeError;

impl From<std::collections::TryReserveError> for EnlargeError {
fn from(_: std::collections::TryReserveError) -> Self {
#[cfg(feature = "alloc")]
impl From<alloc::collections::TryReserveError> for EnlargeError {
fn from(_: alloc::collections::TryReserveError) -> Self {
Self
}
}
Expand Down Expand Up @@ -243,6 +246,7 @@ impl<const LEN: usize> BufferRef for [u8; LEN] {
}
}

#[cfg(feature = "alloc")]
impl BufferRef for Vec<u8> {
#[inline]
fn len(&self) -> usize {
Expand Down Expand Up @@ -334,6 +338,7 @@ impl<const LEN: usize> BufferMut for [MaybeUninit<u8>; LEN] {
}
}

#[cfg(feature = "alloc")]
impl BufferMut for Vec<u8> {
#[inline]
fn capacity(&self) -> usize {
Expand All @@ -357,6 +362,7 @@ impl BufferMut for Vec<u8> {
}
}

#[cfg(feature = "alloc")]
impl BufferMut for Vec<MaybeUninit<u8>> {
#[inline]
fn capacity(&self) -> usize {
Expand Down Expand Up @@ -401,7 +407,13 @@ macro_rules! impl_buffer_ref_for_wrappers {
)*};
}

impl_buffer_ref_for_wrappers!(&T, &mut T, Box<T>, std::rc::Rc<T>, std::sync::Arc<T>);
impl_buffer_ref_for_wrappers!(&T, &mut T);

#[cfg(feature = "alloc")]
impl_buffer_ref_for_wrappers!(alloc::boxed::Box<T>, alloc::rc::Rc<T>);

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
impl_buffer_ref_for_wrappers!(alloc::sync::Arc<T>);

macro_rules! impl_buffer_mut_for_wrappers {
($($type:ty),*) => {$(
Expand Down Expand Up @@ -429,11 +441,15 @@ macro_rules! impl_buffer_mut_for_wrappers {
)*};
}

impl_buffer_mut_for_wrappers!(&mut T, Box<T>);
impl_buffer_mut_for_wrappers!(&mut T);

#[cfg(feature = "alloc")]
impl_buffer_mut_for_wrappers!(alloc::boxed::Box<T>);

#[cfg(test)]
mod buffer_ref {
use super::BufferRef;
use alloc::vec::Vec;

#[test]
fn array() {
Expand All @@ -456,6 +472,7 @@ mod buffer_ref {
mod buffer_mut {
use super::BufferMut;
use crate::core::EnlargeError;
use alloc::vec::Vec;

#[test]
fn array() {
Expand Down Expand Up @@ -493,6 +510,7 @@ mod buffer_mut {
#[cfg(test)]
mod error {
use super::Error;
use alloc::format;

#[test]
fn derived_traits() {
Expand All @@ -502,7 +520,7 @@ mod error {
};

{
use std::error::Error;
use core::error::Error;
assert!(err.source().is_none());
}

Expand All @@ -521,6 +539,7 @@ mod error {
#[cfg(test)]
mod enlarge_error {
use super::EnlargeError;
use alloc::{format, vec::Vec};

#[test]
fn derived_traits() {
Expand All @@ -531,7 +550,7 @@ mod enlarge_error {
};
let err = EnlargeError::from(try_reserve_error);

use std::error::Error;
use core::error::Error;
assert!(err.source().is_none());

assert_eq!(format!("{}", err.clone()), "could not enlarge buffer");
Expand Down
1 change: 1 addition & 0 deletions src/core/size_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl SizeValue {
#[cfg(test)]
mod test {
use super::SizeValue;
use alloc::format;

#[test]
fn new() {
Expand Down
2 changes: 1 addition & 1 deletion src/core/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::num::NonZeroU64;
use core::num::NonZeroU64;

use super::{AlignmentValue, BufferMut, BufferRef, Reader, SizeValue, Writer};

Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#![doc(
html_logo_url = "https://raw.githubusercontent.com/teoxoy/encase/3d6d2e4d7670863e97463a15ceeafac6d13ee73e/logo.svg"
)]
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;

/// Used to implement `ShaderType` for structs
///
Expand Down Expand Up @@ -102,6 +106,7 @@ pub use crate::core::{
CalculateSizeFor, DynamicStorageBuffer, DynamicUniformBuffer, ShaderSize, ShaderType,
StorageBuffer, UniformBuffer,
};

pub use types::runtime_sized_array::ArrayLength;

pub mod internal {
Expand All @@ -112,6 +117,7 @@ pub mod internal {
}

/// Module containing items necessary to implement `ShaderType` for runtime-sized arrays
#[cfg(feature = "alloc")]
pub mod rts_array {
#[doc(inline)]
pub use super::impl_rts_array;
Expand Down
17 changes: 14 additions & 3 deletions src/types/runtime_sized_array.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::collections::{LinkedList, VecDeque};
#[cfg(feature = "alloc")]
use alloc::{
collections::{LinkedList, VecDeque},
vec::Vec,
};

#[cfg(feature = "alloc")]
use crate::core::RuntimeSizedArray;
use crate::core::{
BufferMut, BufferRef, CreateFrom, Metadata, ReadFrom, Reader, RuntimeSizedArray, ShaderSize,
WriteInto, Writer,
BufferMut, BufferRef, CreateFrom, Metadata, ReadFrom, Reader, ShaderSize, WriteInto, Writer,
};
use crate::ShaderType;

Expand Down Expand Up @@ -248,10 +253,14 @@ macro_rules! impl_rts_array_inner {
}

impl_rts_array!([T]; using len);
#[cfg(feature = "alloc")]
impl_rts_array!(Vec<T>; using len truncate);
#[cfg(feature = "alloc")]
impl_rts_array!(VecDeque<T>; using len truncate);
#[cfg(feature = "alloc")]
impl_rts_array!(LinkedList<T>; using len);

#[cfg(feature = "alloc")]
impl<T> Truncate for LinkedList<T> {
fn truncate(&mut self, len: usize) {
if len < self.len() {
Expand All @@ -263,6 +272,8 @@ impl<T> Truncate for LinkedList<T> {
#[cfg(test)]
mod array_length {
use super::ArrayLength;
#[cfg(feature = "alloc")]
use alloc::format;

#[test]
fn derived_traits() {
Expand Down
2 changes: 1 addition & 1 deletion src/types/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ macro_rules! impl_traits_for_atomic {
impl WriteInto for $type {
#[inline]
fn write_into<B: BufferMut>(&self, writer: &mut Writer<B>) {
let value = self.load(std::sync::atomic::Ordering::Relaxed);
let value = self.load(::core::sync::atomic::Ordering::Relaxed);
WriteInto::write_into(&value, writer);
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/types/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ macro_rules! impl_wrapper_inner {

impl_wrapper!(&T; using Ref{});
impl_wrapper!(&mut T; using Ref{} Mut{});
impl_wrapper!(Box<T>; using Ref{} Mut{} From{ new });
impl_wrapper!(std::borrow::Cow<'_, T>; (T: ?Sized + ToOwned<Owned = T>); using Ref{} From{ Owned });
impl_wrapper!(std::rc::Rc<T>; using Ref{} From{ new });
impl_wrapper!(std::sync::Arc<T>; using Ref{} From{ new });
#[cfg(feature = "alloc")]
impl_wrapper!(alloc::boxed::Box<T>; using Ref{} Mut{} From{ new });
#[cfg(feature = "alloc")]
impl_wrapper!(alloc::borrow::Cow<'_, T>; (T: ?Sized + alloc::borrow::ToOwned<Owned = T>); using Ref{} From{ Owned });
#[cfg(feature = "alloc")]
impl_wrapper!(alloc::rc::Rc<T>; using Ref{} From{ new });
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
impl_wrapper!(alloc::sync::Arc<T>; using Ref{} From{ new });
impl_wrapper!(core::cell::Cell<T>; (T: Copy); using Ref{ .get() } Mut{ .get_mut() } From{ new });
14 changes: 11 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "alloc")]
use alloc::{collections::TryReserveError, vec::Vec};
#[cfg(feature = "alloc")]
use core::mem::MaybeUninit;

#[track_caller]
Expand Down Expand Up @@ -45,14 +48,16 @@ macro_rules! if_pod_and_little_endian {
}};
}

#[cfg(feature = "alloc")]
pub(crate) trait ByteVecExt {
/// Tries to extend `self` with `0`s up to `new_len`, using memset.
fn try_extend(&mut self, new_len: usize) -> Result<(), std::collections::TryReserveError>;
fn try_extend(&mut self, new_len: usize) -> Result<(), TryReserveError>;
}

#[cfg(feature = "alloc")]
impl ByteVecExt for Vec<u8> {
#[inline]
fn try_extend(&mut self, new_len: usize) -> Result<(), std::collections::TryReserveError> {
fn try_extend(&mut self, new_len: usize) -> Result<(), TryReserveError> {
let additional = new_len.saturating_sub(self.len());
if additional > 0 {
self.try_reserve(additional)?;
Expand All @@ -71,9 +76,10 @@ impl ByteVecExt for Vec<u8> {
}
}

#[cfg(feature = "alloc")]
impl<T> ByteVecExt for Vec<MaybeUninit<T>> {
#[inline]
fn try_extend(&mut self, new_len: usize) -> Result<(), std::collections::TryReserveError> {
fn try_extend(&mut self, new_len: usize) -> Result<(), TryReserveError> {
let additional = new_len.saturating_sub(self.len());
if additional > 0 {
self.try_reserve(additional)?;
Expand Down Expand Up @@ -136,6 +142,8 @@ impl<T> SliceExt<T> for [T] {
#[cfg(test)]
mod byte_vec_ext {
use crate::utils::ByteVecExt;
#[cfg(feature = "alloc")]
use alloc::{vec, vec::Vec};

#[test]
fn try_extend() {
Expand Down
5 changes: 4 additions & 1 deletion tests/hygiene.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![no_implicit_prelude]
#![allow(non_camel_case_types)]

#[cfg(feature = "alloc")]
extern crate alloc;

macro_rules! decl_primitives_as_traits {
($($primitive:ident),*) => {$(#[allow(dead_code)] trait $primitive {})*};
}
Expand Down Expand Up @@ -110,5 +113,5 @@ struct TestGeneric<
a: &'a mut Test,
b: &'a mut [T; N],
#[shader(align(16), size(runtime))]
c: &'a mut ::std::vec::Vec<[::test_impl::Vec3f; 2]>,
c: &'a mut ::alloc::vec::Vec<[::test_impl::Vec3f; 2]>,
}
5 changes: 4 additions & 1 deletion tests/pass/wrappers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[cfg(feature = "alloc")]
extern crate alloc;

use alloc::{borrow::Cow, rc::Rc, sync::Arc};
use core::cell::Cell;
use encase::ShaderType;
use std::{borrow::Cow, rc::Rc, sync::Arc};

fn main() {}

Expand Down
Loading