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: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ description = "A Vec-like structure-of-arrays container"
homepage = "https://github.com/tim-harding/soa-rs"
repository = "https://github.com/tim-harding/soa-rs"
readme = "README.md"
categories = ["data-structures", "no-std"]

[profile.bench]
# For profiling
debug = true
debug = true

[workspace]
members = ["soa-rs-derive", "soa-rs-testing"]
Expand All @@ -22,6 +23,7 @@ path = "soa-rs-derive"
[dependencies.serde]
version = "1.0.199"
optional = true
default-features = false

[features]
default = []
Expand Down
68 changes: 34 additions & 34 deletions soa-rs-derive/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn fields_struct(
impl #deref {
#(
#vis_all const fn #slice_getters_ref(&self) -> &[#ty_all] {
let slice = ::std::ptr::NonNull::slice_from_raw_parts(
let slice = ::core::ptr::NonNull::slice_from_raw_parts(
self.0.raw().#ident_all,
self.0.len(),
);
Expand All @@ -142,7 +142,7 @@ pub fn fields_struct(
}

#vis_all const fn #slice_getters_mut(&mut self) -> &mut [#ty_all] {
let mut slice = ::std::ptr::NonNull::slice_from_raw_parts(
let mut slice = ::core::ptr::NonNull::slice_from_raw_parts(
self.0.raw().#ident_all,
self.0.len(),
);
Expand Down Expand Up @@ -228,8 +228,8 @@ pub fn fields_struct(
#[automatically_derived]
impl<const N: usize> #array<N> {
#vis const fn from_array(array: [#ident; N]) -> Self {
let array = ::std::mem::ManuallyDrop::new(array);
let array = ::std::ptr::from_ref::<::std::mem::ManuallyDrop<[#ident; N]>>(&array);
let array = ::core::mem::ManuallyDrop::new(array);
let array = ::core::ptr::from_ref::<::core::mem::ManuallyDrop<[#ident; N]>>(&array);
let array = array.cast::<[#ident; N]>();
// SAFETY: Getting a slice this way is okay
// because the memory comes from an array,
Expand All @@ -239,19 +239,19 @@ pub fn fields_struct(
Self {
#(
#ident_all: {
let mut uninit = [const { ::std::mem::MaybeUninit::uninit() }; N];
let mut uninit = [const { ::core::mem::MaybeUninit::uninit() }; N];
let mut i = 0;
while i < N {
let src = ::std::ptr::from_ref(&array[i].#ident_all);
let src = ::core::ptr::from_ref(&array[i].#ident_all);
// SAFETY: This pointer is safe to read
// because it comes from a reference.
let read = unsafe { src.read() };
uninit[i] = ::std::mem::MaybeUninit::new(read);
uninit[i] = ::core::mem::MaybeUninit::new(read);
i += 1;
}
// TODO: Prefer MaybeUninit::transpose when stabilized
// SAFETY: MaybeUninit<[T; N]> is repr(transparent) of [T; N]
unsafe { ::std::mem::transmute_copy(&uninit) }
unsafe { ::core::mem::transmute_copy(&uninit) }
},
)*
}
Expand All @@ -265,7 +265,7 @@ pub fn fields_struct(
fn as_slice(&self) -> ::soa_rs::SliceRef<'_, Self::Item> {
let raw = #raw {
#(
#ident_all: ::std::ptr::NonNull::from(
#ident_all: ::core::ptr::NonNull::from(
self.#ident_all.as_slice()
).cast(),
)*
Expand All @@ -283,7 +283,7 @@ pub fn fields_struct(
fn as_mut_slice(&mut self) -> ::soa_rs::SliceMut<'_, Self::Item> {
let raw = #raw {
#(
#ident_all: ::std::ptr::NonNull::from(
#ident_all: ::core::ptr::NonNull::from(
self.#ident_all.as_mut_slice()
).cast(),
)*
Expand All @@ -298,9 +298,9 @@ pub fn fields_struct(
});
}

let indices = std::iter::repeat(()).enumerate().map(|(i, ())| i);
let indices = core::iter::repeat(()).enumerate().map(|(i, ())| i);
let offsets_len = fields_len - 1;
let raw_body = define(&|ty| quote! { ::std::ptr::NonNull<#ty> });
let raw_body = define(&|ty| quote! { ::core::ptr::NonNull<#ty> });

let layout_and_offsets_body = |checked: bool| {
let check_head = if checked {
Expand Down Expand Up @@ -337,13 +337,13 @@ pub fn fields_struct(

let indices = indices.clone();
quote! {
let array = #check_head ::std::alloc::Layout::array::<#ty_head>(cap) #check_tail;
let array = #check_head ::core::alloc::Layout::array::<#ty_head>(cap) #check_tail;
#raise_align_head
let layout = array;
let mut offsets = [0usize; #offsets_len];

#(
let array = #check_head ::std::alloc::Layout::array::<#ty_tail>(cap) #check_tail;
let array = #check_head ::core::alloc::Layout::array::<#ty_tail>(cap) #check_tail;
#raise_align_tail
let (layout, offset) = #check_head layout.extend(array) #check_tail;
offsets[#indices] = offset;
Expand Down Expand Up @@ -374,7 +374,7 @@ pub fn fields_struct(
impl #raw {
#[inline]
const fn layout_and_offsets(cap: usize)
-> Result<(::std::alloc::Layout, [usize; #offsets_len]), ::std::alloc::LayoutError>
-> Result<(::core::alloc::Layout, [usize; #offsets_len]), ::core::alloc::LayoutError>
{
#layout_and_offsets_checked_body
Ok((layout, offsets))
Expand All @@ -383,15 +383,15 @@ pub fn fields_struct(
// TODO: Make this const if Option::unwrap_unchecked is const stabilized
#[inline]
unsafe fn layout_and_offsets_unchecked(cap: usize)
-> (::std::alloc::Layout, [usize; #offsets_len])
-> (::core::alloc::Layout, [usize; #offsets_len])
{
#layout_and_offsets_unchecked_body
(layout, offsets)
}

#[inline]
const unsafe fn with_offsets(
ptr: ::std::ptr::NonNull<u8>,
ptr: ::core::ptr::NonNull<u8>,
offsets: [usize; #offsets_len],
) -> Self
{
Expand All @@ -417,19 +417,19 @@ pub fn fields_struct(
#[inline]
fn dangling() -> Self {
Self {
#(#ident_all: ::std::ptr::NonNull::dangling(),)*
#(#ident_all: ::core::ptr::NonNull::dangling(),)*
}
}

#[inline]
unsafe fn from_parts(ptr: ::std::ptr::NonNull<u8>, capacity: usize) -> Self {
unsafe fn from_parts(ptr: ::core::ptr::NonNull<u8>, capacity: usize) -> Self {
// SAFETY: Caller ensures ptr and capacity are from a previous allocation
let (_, offsets) = Self::layout_and_offsets_unchecked(capacity);
Self::with_offsets(ptr, offsets)
}

#[inline]
fn into_parts(self) -> ::std::ptr::NonNull<u8> {
fn into_parts(self) -> ::core::ptr::NonNull<u8> {
self.#ident_head.cast()
}

Expand All @@ -439,9 +439,9 @@ pub fn fields_struct(
.expect("capacity overflow");

// SAFETY: Caller ensures that Self is not zero-sized
let ptr = ::std::alloc::alloc(new_layout);
let Some(ptr) = ::std::ptr::NonNull::new(ptr) else {
::std::alloc::handle_alloc_error(new_layout);
let ptr = ::soa_rs::__alloc::alloc::alloc(new_layout);
let Some(ptr) = ::core::ptr::NonNull::new(ptr) else {
::soa_rs::__alloc::alloc::handle_alloc_error(new_layout);
};

Self::with_offsets(ptr, new_offsets)
Expand Down Expand Up @@ -475,9 +475,9 @@ pub fn fields_struct(
// - new_capacity is nonzero
// - old_layout matches the previous layout because old_capacity
// matches the previously allocated capacity
let ptr = ::std::alloc::realloc(ptr, old_layout, new_layout.size());
let Some(ptr) = ::std::ptr::NonNull::new(ptr) else {
::std::alloc::handle_alloc_error(new_layout);
let ptr = ::soa_rs::__alloc::alloc::realloc(ptr, old_layout, new_layout.size());
let Some(ptr) = ::core::ptr::NonNull::new(ptr) else {
::soa_rs::__alloc::alloc::handle_alloc_error(new_layout);
};

// Pointer may have moved, can't reuse self
Expand Down Expand Up @@ -529,9 +529,9 @@ pub fn fields_struct(
// - new_capacity is nonzero
// - old_layout matches the previous layout because old_capacity
// matches the previously allocated capacity
let ptr = ::std::alloc::realloc(ptr.as_ptr(), old_layout, new_layout.size());
let Some(ptr) = ::std::ptr::NonNull::new(ptr) else {
::std::alloc::handle_alloc_error(new_layout);
let ptr = ::soa_rs::__alloc::alloc::realloc(ptr.as_ptr(), old_layout, new_layout.size());
let Some(ptr) = ::core::ptr::NonNull::new(ptr) else {
::soa_rs::__alloc::alloc::handle_alloc_error(new_layout);
};

// Pointer may have moved, can't reuse dst
Expand All @@ -546,7 +546,7 @@ pub fn fields_struct(
// - This soa was previously allocated
// - layout is the previously used layout because old_capacity
// is the previously allocated capacity
::std::alloc::dealloc(self.#ident_head.as_ptr().cast(), layout);
::soa_rs::__alloc::alloc::dealloc(self.#ident_head.as_ptr().cast(), layout);
}

#[inline]
Expand Down Expand Up @@ -614,7 +614,7 @@ pub fn fields_struct(
unsafe fn slices<'a>(self, len: usize) -> #slices<'a> {
#slices {
#(
#ident_all: ::std::ptr::NonNull::slice_from_raw_parts(
#ident_all: ::core::ptr::NonNull::slice_from_raw_parts(
self.#ident_all,
len,
)
Expand All @@ -629,7 +629,7 @@ pub fn fields_struct(
unsafe fn slices_mut<'a>(self, len: usize) -> #slices_mut<'a> {
#slices_mut {
#(
#ident_all: ::std::ptr::NonNull::slice_from_raw_parts(
#ident_all: ::core::ptr::NonNull::slice_from_raw_parts(
self.#ident_all,
len,
)
Expand Down Expand Up @@ -682,8 +682,8 @@ impl ToTokens for FieldIdent {
}
}

impl std::fmt::Display for FieldIdent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for FieldIdent {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
FieldIdent::Named(ident) => write!(f, "{ident}"),
FieldIdent::Unnamed(i) => write!(f, "{i}"),
Expand Down
8 changes: 4 additions & 4 deletions soa-rs-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
mod fields;
mod zst;

use core::{
error::Error,
fmt::{self, Display, Formatter},
};
use fields::{FieldKind, fields_struct};
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use quote::{quote, quote_spanned};
use std::{
error::Error,
fmt::{self, Display, Formatter},
};
use syn::{Attribute, Data, DeriveInput, Fields, parse_macro_input};
use zst::{ZstKind, zst_struct};

Expand Down
10 changes: 5 additions & 5 deletions soa-rs-derive/src/zst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ pub fn zst_struct(ident: Ident, vis: Visibility, kind: ZstKind) -> TokenStream {
fn from_slice(slice: &::soa_rs::Slice<Self::Item>) -> &Self {
// SAFETY: Self is `repr(transparent)` of Slice
#[allow(clippy::transmute_ptr_to_ptr)]
unsafe { ::std::mem::transmute(slice) }
unsafe { ::core::mem::transmute(slice) }
}

fn from_slice_mut(slice: &mut ::soa_rs::Slice<Self::Item>) -> &mut Self {
// SAFETY: Self is `repr(transparent)` of Slice
#[allow(clippy::transmute_ptr_to_ptr)]
unsafe { ::std::mem::transmute(slice) }
unsafe { ::core::mem::transmute(slice) }
}
}

Expand All @@ -96,11 +96,11 @@ pub fn zst_struct(ident: Ident, vis: Visibility, kind: ZstKind) -> TokenStream {
fn dangling() -> Self { Self }

#[inline]
unsafe fn from_parts(ptr: ::std::ptr::NonNull<u8>, capacity: usize) -> Self { Self }
unsafe fn from_parts(ptr: ::core::ptr::NonNull<u8>, capacity: usize) -> Self { Self }

#[inline]
fn into_parts(self) -> ::std::ptr::NonNull<u8> {
::std::ptr::NonNull::dangling()
fn into_parts(self) -> ::core::ptr::NonNull<u8> {
::core::ptr::NonNull::dangling()
}

#[inline]
Expand Down
6 changes: 3 additions & 3 deletions soa-rs-testing/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Rng {
where
T: FromIterator<Vec4>,
{
std::iter::repeat_with(|| Vec4::new_rng(self))
core::iter::repeat_with(|| Vec4::new_rng(self))
.take(count)
.collect()
}
Expand Down Expand Up @@ -83,7 +83,7 @@ fn criterion_benchmark(c: &mut Criterion) {
soa1.chunks_exact(8)
.zip(soa2.chunks_exact(8))
.fold([0.; 8], |acc, (a, b)| {
std::array::from_fn(|i| acc[i] + a.idx(i).dot(&b.idx(i)))
core::array::from_fn(|i| acc[i] + a.idx(i).dot(&b.idx(i)))
})
.into_iter()
.sum::<f32>()
Expand All @@ -96,7 +96,7 @@ fn criterion_benchmark(c: &mut Criterion) {
vec1.chunks_exact(8).zip(vec2.chunks_exact(8)).fold(
[0.; 8],
|acc, (a, b)| {
std::array::from_fn(|i| {
core::array::from_fn(|i| {
acc[i] + a[i].dot(&b[i])
})
},
Expand Down
4 changes: 2 additions & 2 deletions soa-rs-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub fn clone() {

#[test]
pub fn clone_from() {
let mut dst: Soa<_> = std::iter::repeat(Tuple(100, 100, 100)).take(7).collect();
let mut dst: Soa<_> = core::iter::repeat(Tuple(100, 100, 100)).take(7).collect();
let src: Soa<_> = [Tuple(1, 2, 3), Tuple(4, 5, 6), Tuple(7, 8, 9)].into();
dst.clone_from(&src);
assert_eq!(dst, src);
Expand Down Expand Up @@ -335,8 +335,8 @@ pub fn ordering() {

#[test]
pub fn hashing() {
use core::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

let mut expected = DefaultHasher::new();
ABCDE.hash(&mut expected);
Expand Down
6 changes: 3 additions & 3 deletions src/borrow_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mod multiple_mutable_borrows {
mod swap_slices_by_mut_ref {
/// ```compile_fail,E0277
/// use soa_rs::{Soa, Soars};
/// use std::ops::DerefMut; // Added
/// use core::ops::DerefMut; // Added
///
/// #[derive(Soars)]
/// #[soa_derive(Debug, PartialEq)]
Expand All @@ -72,7 +72,7 @@ mod swap_slices_by_mut_ref {
/// let mut x = Soa::<Foo>::new();
/// x.push(Foo(0));
/// let mut y = Soa::<Foo>::new();
/// std::mem::swap(x.deref_mut(), y.deref_mut()); // Added
/// core::mem::swap(x.deref_mut(), y.deref_mut()); // Added
/// ```
mod deref_mut {}

Expand All @@ -86,7 +86,7 @@ mod swap_slices_by_mut_ref {
/// let mut x = Soa::<Foo>::new();
/// x.push(Foo(0));
/// let mut y = Soa::<Foo>::new();
/// std::mem::swap::<Slice<_>>(x.as_mut(), y.as_mut()); // Added
/// core::mem::swap::<Slice<_>>(x.as_mut(), y.as_mut()); // Added
/// ```
mod as_mut {}
}
2 changes: 1 addition & 1 deletion src/chunks_exact.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Slice, SliceRef, SoaRaw, Soars};
use std::marker::PhantomData;
use core::marker::PhantomData;

/// An iterator over a [`Slice`] in (non-overlapping) chunks of `chunk_size`
/// elements.
Expand Down
2 changes: 1 addition & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Slice, SliceMut, SliceRef, SoaRaw, Soars};
use std::{
use core::{
marker::PhantomData,
ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive},
};
Expand Down
Loading