added optional no_std feature

master
Parker TenBroeck 2 years ago
parent be0bc7b612
commit dc421e002b

@ -15,3 +15,7 @@ lazy_static = "1.4.0"
[build-dependencies] [build-dependencies]
cc = "1.0" cc = "1.0"
rustc_version = "0.2" rustc_version = "0.2"
[features]
default = []
no_std = []

@ -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)->(); pub type CallbackRaw = unsafe extern "C" fn (ptr: *mut c_void, data: *mut c_void)->();

@ -85,13 +85,17 @@
//! # License //! # License
//! MIT licensed //! MIT licensed
#![cfg_attr(nightly, feature(test))]
#![cfg_attr(all(nightly, not(feature = "no_std")), feature(test))]
#![allow(dead_code)] #![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::{ mem::{
self, self,
MaybeUninit, MaybeUninit,
@ -106,7 +110,13 @@ use std::{
ptr, 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; mod ffi;
/// Allocate a runtime length uninitialised byte buffer on the stack, call `callback` with this buffer, and then deallocate the buffer. /// 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<u8>]) -> T
unsafe { unsafe {
let slice = slice::from_raw_parts_mut(allocad_ptr as *mut MaybeUninit<u8>, size); let slice = slice::from_raw_parts_mut(allocad_ptr as *mut MaybeUninit<u8>, size);
let callback = ManuallyDrop::take(&mut callback); 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,14 +195,20 @@ where F: FnOnce(&mut [MaybeUninit<u8>]) -> T
ffi::alloca_trampoline(size, create_trampoline(&callback), &mut callback as *mut _ as *mut c_void); ffi::alloca_trampoline(size, create_trampoline(&callback), &mut callback as *mut _ as *mut c_void);
rval.assume_init() rval.assume_init()
}; };
#[cfg(feature = "no_std")]
match rval
{ {
return rval
}
#[cfg(not(feature = "no_std"))]
{
match rval {
Ok(v) => v, Ok(v) => v,
Err(pan) => panic::resume_unwind(pan), Err(pan) => std::panic::resume_unwind(pan),
} }
} }
}
/// A module of helper functions for slice memory manipulation /// A module of helper functions for slice memory manipulation
/// ///
/// These are mostly re-implementations of unstable corelib functions in stable Rust. /// These are mostly re-implementations of unstable corelib functions in stable Rust.
@ -192,7 +216,7 @@ pub mod helpers {
use super::*; use super::*;
#[inline(always)] pub(crate) fn align_buffer_to<T>(ptr: *mut u8) -> *mut T #[inline(always)] pub(crate) fn align_buffer_to<T>(ptr: *mut u8) -> *mut T
{ {
use std::mem::align_of; use core::mem::align_of;
((ptr as usize) + align_of::<T>() - (ptr as usize) % align_of::<T>()) as *mut T ((ptr as usize) + align_of::<T>() - (ptr as usize) % align_of::<T>()) as *mut T
} }
@ -245,7 +269,7 @@ where F: FnOnce(&mut [u8]) -> T
#[inline] pub fn stackalloc_uninit<T, U, F>(size: usize, callback: F) -> U #[inline] pub fn stackalloc_uninit<T, U, F>(size: usize, callback: F) -> U
where F: FnOnce(&mut [MaybeUninit<T>]) -> U where F: FnOnce(&mut [MaybeUninit<T>]) -> U
{ {
let size_bytes = (std::mem::size_of::<T>() * size) + std::mem::align_of::<T>(); let size_bytes = (core::mem::size_of::<T>() * size) + core::mem::align_of::<T>();
alloca(size_bytes, move |buf| { alloca(size_bytes, move |buf| {
let abuf = align_buffer_to::<MaybeUninit<T>>(buf.as_mut_ptr() as *mut u8); let abuf = align_buffer_to::<MaybeUninit<T>>(buf.as_mut_ptr() as *mut u8);
debug_assert!(buf.as_ptr_range().contains(&(abuf as *const _ as *const MaybeUninit<u8>))); debug_assert!(buf.as_ptr_range().contains(&(abuf as *const _ as *const MaybeUninit<u8>)));
@ -379,5 +403,4 @@ where F: FnOnce(&mut [T]) -> U,
} }
#[cfg(test)]
mod tests;

Loading…
Cancel
Save