From dc421e002b191fe9b4dab1a31c46da00a52b6a80 Mon Sep 17 00:00:00 2001 From: Parker TenBroeck <51721964+ParkerTenBroeck@users.noreply.github.com> Date: Mon, 13 Jun 2022 13:44:40 -0400 Subject: [PATCH] added optional no_std feature --- Cargo.toml | 4 ++++ src/ffi.rs | 2 +- src/lib.rs | 49 ++++++++++++++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5ed1a15..e9a2a85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,7 @@ lazy_static = "1.4.0" [build-dependencies] cc = "1.0" rustc_version = "0.2" + +[features] +default = [] +no_std = [] diff --git a/src/ffi.rs b/src/ffi.rs index 0f6b49b..b417b56 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -1,4 +1,4 @@ -use std::ffi::c_void; +use core::ffi::c_void; pub type CallbackRaw = unsafe extern "C" fn (ptr: *mut c_void, data: *mut c_void)->(); diff --git a/src/lib.rs b/src/lib.rs index b35218c..ee9c8d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,13 +85,17 @@ //! # License //! MIT licensed -#![cfg_attr(nightly, feature(test))] + +#![cfg_attr(all(nightly, not(feature = "no_std")), feature(test))] #![allow(dead_code)] -#[cfg(nightly)] extern crate test; +#![cfg_attr(all(feature = "no_std", not(test)), no_std)] + +#[cfg(all(nightly, not(feature = "no_std"), not(test)))] extern crate test; -use std::{ +#[allow(unused)] +use core::{ mem::{ self, MaybeUninit, @@ -106,7 +110,13 @@ use std::{ ptr, }; -pub mod avec; pub use avec::AVec; +#[cfg(test)] +mod tests; + +#[cfg(not(feature = "no_std"))] +pub mod avec; +#[cfg(not(feature = "no_std"))] +pub use avec::AVec; mod ffi; /// Allocate a runtime length uninitialised byte buffer on the stack, call `callback` with this buffer, and then deallocate the buffer. @@ -157,7 +167,15 @@ where F: FnOnce(&mut [MaybeUninit]) -> T unsafe { let slice = slice::from_raw_parts_mut(allocad_ptr as *mut MaybeUninit, size); let callback = ManuallyDrop::take(&mut callback); - rval = MaybeUninit::new(panic::catch_unwind(AssertUnwindSafe(move || callback(slice)))); + + #[cfg(feature = "no_std")] + { + rval = MaybeUninit::new(callback(slice)); + } + #[cfg(not(feature = "no_std"))] + { + rval = MaybeUninit::new(std::panic::catch_unwind(AssertUnwindSafe(move || callback(slice)))); + } } }; @@ -177,12 +195,18 @@ where F: FnOnce(&mut [MaybeUninit]) -> T ffi::alloca_trampoline(size, create_trampoline(&callback), &mut callback as *mut _ as *mut c_void); rval.assume_init() }; - - match rval + #[cfg(feature = "no_std")] { - Ok(v) => v, - Err(pan) => panic::resume_unwind(pan), + return rval } + #[cfg(not(feature = "no_std"))] + { + match rval { + Ok(v) => v, + Err(pan) => std::panic::resume_unwind(pan), + } + } + } /// A module of helper functions for slice memory manipulation @@ -192,7 +216,7 @@ pub mod helpers { use super::*; #[inline(always)] pub(crate) fn align_buffer_to(ptr: *mut u8) -> *mut T { - use std::mem::align_of; + use core::mem::align_of; ((ptr as usize) + align_of::() - (ptr as usize) % align_of::()) as *mut T } @@ -245,7 +269,7 @@ where F: FnOnce(&mut [u8]) -> T #[inline] pub fn stackalloc_uninit(size: usize, callback: F) -> U where F: FnOnce(&mut [MaybeUninit]) -> U { - let size_bytes = (std::mem::size_of::() * size) + std::mem::align_of::(); + let size_bytes = (core::mem::size_of::() * size) + core::mem::align_of::(); alloca(size_bytes, move |buf| { let abuf = align_buffer_to::>(buf.as_mut_ptr() as *mut u8); debug_assert!(buf.as_ptr_range().contains(&(abuf as *const _ as *const MaybeUninit))); @@ -379,5 +403,4 @@ where F: FnOnce(&mut [T]) -> U, } -#[cfg(test)] -mod tests; +