diff --git a/Cargo.toml b/Cargo.toml index 06e6b31..a844747 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/README.md b/README.md index 15f3cb7..7fdd567 100644 --- a/README.md +++ b/README.md @@ -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> = Vec::new(); @@ -157,7 +157,7 @@ let byte_buffer: Vec = unsafe { ) }; -std::mem::forget(uninit_buffer); +core::mem::forget(uninit_buffer); // write byte_buffer to GPU diff --git a/src/core/alignment_value.rs b/src/core/alignment_value.rs index 18392c4..82da8c7 100644 --- a/src/core/alignment_value.rs +++ b/src/core/alignment_value.rs @@ -80,6 +80,7 @@ impl AlignmentValue { #[cfg(test)] mod test { use super::AlignmentValue; + use alloc::format; #[test] fn new() { diff --git a/src/core/rw.rs b/src/core/rw.rs index 69867a7..57a6a70 100644 --- a/src/core/rw.rs +++ b/src/core/rw.rs @@ -1,4 +1,6 @@ use super::ShaderType; +#[cfg(feature = "alloc")] +use alloc::vec::Vec; use core::mem::MaybeUninit; use thiserror::Error; @@ -177,8 +179,9 @@ impl Cursor { #[error("could not enlarge buffer")] pub struct EnlargeError; -impl From for EnlargeError { - fn from(_: std::collections::TryReserveError) -> Self { +#[cfg(feature = "alloc")] +impl From for EnlargeError { + fn from(_: alloc::collections::TryReserveError) -> Self { Self } } @@ -243,6 +246,7 @@ impl BufferRef for [u8; LEN] { } } +#[cfg(feature = "alloc")] impl BufferRef for Vec { #[inline] fn len(&self) -> usize { @@ -334,6 +338,7 @@ impl BufferMut for [MaybeUninit; LEN] { } } +#[cfg(feature = "alloc")] impl BufferMut for Vec { #[inline] fn capacity(&self) -> usize { @@ -357,6 +362,7 @@ impl BufferMut for Vec { } } +#[cfg(feature = "alloc")] impl BufferMut for Vec> { #[inline] fn capacity(&self) -> usize { @@ -401,7 +407,13 @@ macro_rules! impl_buffer_ref_for_wrappers { )*}; } -impl_buffer_ref_for_wrappers!(&T, &mut T, Box, std::rc::Rc, std::sync::Arc); +impl_buffer_ref_for_wrappers!(&T, &mut T); + +#[cfg(feature = "alloc")] +impl_buffer_ref_for_wrappers!(alloc::boxed::Box, alloc::rc::Rc); + +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +impl_buffer_ref_for_wrappers!(alloc::sync::Arc); macro_rules! impl_buffer_mut_for_wrappers { ($($type:ty),*) => {$( @@ -429,11 +441,15 @@ macro_rules! impl_buffer_mut_for_wrappers { )*}; } -impl_buffer_mut_for_wrappers!(&mut T, Box); +impl_buffer_mut_for_wrappers!(&mut T); + +#[cfg(feature = "alloc")] +impl_buffer_mut_for_wrappers!(alloc::boxed::Box); #[cfg(test)] mod buffer_ref { use super::BufferRef; + use alloc::vec::Vec; #[test] fn array() { @@ -456,6 +472,7 @@ mod buffer_ref { mod buffer_mut { use super::BufferMut; use crate::core::EnlargeError; + use alloc::vec::Vec; #[test] fn array() { @@ -493,6 +510,7 @@ mod buffer_mut { #[cfg(test)] mod error { use super::Error; + use alloc::format; #[test] fn derived_traits() { @@ -502,7 +520,7 @@ mod error { }; { - use std::error::Error; + use core::error::Error; assert!(err.source().is_none()); } @@ -521,6 +539,7 @@ mod error { #[cfg(test)] mod enlarge_error { use super::EnlargeError; + use alloc::{format, vec::Vec}; #[test] fn derived_traits() { @@ -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"); diff --git a/src/core/size_value.rs b/src/core/size_value.rs index 8ae0e03..5418392 100644 --- a/src/core/size_value.rs +++ b/src/core/size_value.rs @@ -41,6 +41,7 @@ impl SizeValue { #[cfg(test)] mod test { use super::SizeValue; + use alloc::format; #[test] fn new() { diff --git a/src/core/traits.rs b/src/core/traits.rs index bc30971..941da2d 100644 --- a/src/core/traits.rs +++ b/src/core/traits.rs @@ -1,4 +1,4 @@ -use std::num::NonZeroU64; +use core::num::NonZeroU64; use super::{AlignmentValue, BufferMut, BufferRef, Reader, SizeValue, Writer}; diff --git a/src/lib.rs b/src/lib.rs index 1277a67..cd291f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 /// @@ -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 { @@ -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; diff --git a/src/types/runtime_sized_array.rs b/src/types/runtime_sized_array.rs index 01fe964..32597a8 100644 --- a/src/types/runtime_sized_array.rs +++ b/src/types/runtime_sized_array.rs @@ -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; @@ -248,10 +253,14 @@ macro_rules! impl_rts_array_inner { } impl_rts_array!([T]; using len); +#[cfg(feature = "alloc")] impl_rts_array!(Vec; using len truncate); +#[cfg(feature = "alloc")] impl_rts_array!(VecDeque; using len truncate); +#[cfg(feature = "alloc")] impl_rts_array!(LinkedList; using len); +#[cfg(feature = "alloc")] impl Truncate for LinkedList { fn truncate(&mut self, len: usize) { if len < self.len() { @@ -263,6 +272,8 @@ impl Truncate for LinkedList { #[cfg(test)] mod array_length { use super::ArrayLength; + #[cfg(feature = "alloc")] + use alloc::format; #[test] fn derived_traits() { diff --git a/src/types/scalar.rs b/src/types/scalar.rs index 709162e..342c6a5 100644 --- a/src/types/scalar.rs +++ b/src/types/scalar.rs @@ -121,7 +121,7 @@ macro_rules! impl_traits_for_atomic { impl WriteInto for $type { #[inline] fn write_into(&self, writer: &mut Writer) { - let value = self.load(std::sync::atomic::Ordering::Relaxed); + let value = self.load(::core::sync::atomic::Ordering::Relaxed); WriteInto::write_into(&value, writer); } } diff --git a/src/types/wrapper.rs b/src/types/wrapper.rs index 9357d51..8e55020 100644 --- a/src/types/wrapper.rs +++ b/src/types/wrapper.rs @@ -113,8 +113,12 @@ macro_rules! impl_wrapper_inner { impl_wrapper!(&T; using Ref{}); impl_wrapper!(&mut T; using Ref{} Mut{}); -impl_wrapper!(Box; using Ref{} Mut{} From{ new }); -impl_wrapper!(std::borrow::Cow<'_, T>; (T: ?Sized + ToOwned); using Ref{} From{ Owned }); -impl_wrapper!(std::rc::Rc; using Ref{} From{ new }); -impl_wrapper!(std::sync::Arc; using Ref{} From{ new }); +#[cfg(feature = "alloc")] +impl_wrapper!(alloc::boxed::Box; using Ref{} Mut{} From{ new }); +#[cfg(feature = "alloc")] +impl_wrapper!(alloc::borrow::Cow<'_, T>; (T: ?Sized + alloc::borrow::ToOwned); using Ref{} From{ Owned }); +#[cfg(feature = "alloc")] +impl_wrapper!(alloc::rc::Rc; using Ref{} From{ new }); +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +impl_wrapper!(alloc::sync::Arc; using Ref{} From{ new }); impl_wrapper!(core::cell::Cell; (T: Copy); using Ref{ .get() } Mut{ .get_mut() } From{ new }); diff --git a/src/utils.rs b/src/utils.rs index 2d0c216..bd1972c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "alloc")] +use alloc::{collections::TryReserveError, vec::Vec}; +#[cfg(feature = "alloc")] use core::mem::MaybeUninit; #[track_caller] @@ -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 { #[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)?; @@ -71,9 +76,10 @@ impl ByteVecExt for Vec { } } +#[cfg(feature = "alloc")] impl ByteVecExt for Vec> { #[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)?; @@ -136,6 +142,8 @@ impl SliceExt for [T] { #[cfg(test)] mod byte_vec_ext { use crate::utils::ByteVecExt; + #[cfg(feature = "alloc")] + use alloc::{vec, vec::Vec}; #[test] fn try_extend() { diff --git a/tests/hygiene.rs b/tests/hygiene.rs index ff0dea3..04ad3a3 100644 --- a/tests/hygiene.rs +++ b/tests/hygiene.rs @@ -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 {})*}; } @@ -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]>, } diff --git a/tests/pass/wrappers.rs b/tests/pass/wrappers.rs index 3904bd6..61237df 100644 --- a/tests/pass/wrappers.rs +++ b/tests/pass/wrappers.rs @@ -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() {}