Skip to content

Commit 472875b

Browse files
committed
Rework the module hierarchy
1 parent 7c4fc0c commit 472875b

17 files changed

+824
-824
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
are no longer required to perform validation on _every_ call
88
- Remove the `nightly` feature flag and the `feature(min_const_generics)` attribute,
99
raising the minimum supported compiler version to 1.51.
10+
- Rework the module hierarchy, introducing the `collections` module
1011
- Rename `Array{Vec, Deque, Heap}` to `Inline*` for consistency with `InlineObject`
1112
- Redefine `ArenaStorage` as a struct for compatibility with non-array-like layouts
1213
- Remove `HeapStorage` type alias and add `AllocStorage` struct (similar to `ArenaStorage`)

examples/huffman.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use coca::Arena;
1+
use coca::arena::{Arena, Box};
22

33
use std::mem::{size_of, MaybeUninit};
44

@@ -9,7 +9,7 @@ fn calc_huff_encoded_size(input: &[u8]) -> usize {
99
const MEM_FOR_TREES: usize = 512 * size_of::<CodeTree>();
1010
const ARENA_SIZE: usize = MEM_FOR_FREQS_AND_CODELEN + MEM_FOR_TREES;
1111

12-
let mut backing = Box::new([MaybeUninit::<u8>::uninit(); ARENA_SIZE]);
12+
let mut backing = std::boxed::Box::new([MaybeUninit::<u8>::uninit(); ARENA_SIZE]);
1313
let mut arena = Arena::from(&mut backing[..]);
1414

1515
let mut freqs = arena.array(0isize, 256);
@@ -23,12 +23,12 @@ fn calc_huff_encoded_size(input: &[u8]) -> usize {
2323
sym: u8,
2424
},
2525
Composite {
26-
left: coca::Box<'a, CodeTree<'a>>,
27-
right: coca::Box<'a, CodeTree<'a>>,
26+
left: Box<'a, CodeTree<'a>>,
27+
right: Box<'a, CodeTree<'a>>,
2828
},
2929
}
3030

31-
struct Item<'a>(isize, coca::Box<'a, CodeTree<'a>>);
31+
struct Item<'a>(isize, Box<'a, CodeTree<'a>>);
3232
impl<'a> std::cmp::PartialEq for Item<'a> {
3333
fn eq(&self, rhs: &Self) -> bool {
3434
self.0.eq(&rhs.0)
@@ -46,7 +46,7 @@ fn calc_huff_encoded_size(input: &[u8]) -> usize {
4646
}
4747
}
4848

49-
let mut items = arena.vec(256usize);
49+
let mut items: coca::collections::ArenaVec<'_, Item> = arena.with_capacity(256usize);
5050
for (idx, freq) in freqs.iter().enumerate() {
5151
if *freq == 0 {
5252
continue;
@@ -55,7 +55,7 @@ fn calc_huff_encoded_size(input: &[u8]) -> usize {
5555
items.push(Item(*freq, leaf_node));
5656
}
5757

58-
let mut queue = coca::ArenaHeap::<Item<'_>, _>::from(items);
58+
let mut queue = coca::collections::ArenaHeap::<Item<'_>, _>::from(items);
5959
while queue.len() > 1 {
6060
let Item(freq_l, left) = queue.pop().unwrap();
6161
let Item(freq_r, right) = queue.pop().unwrap();

examples/icosphere.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
use std::mem::MaybeUninit;
44

5+
use coca::arena::Arena;
6+
use coca::collections::{ArenaDeque, ArenaVec};
57
use coca::storage::Capacity;
6-
use coca::{Arena, ArenaDeque, ArenaVec};
78

89
coca::index_type! { VertexID: u32 }
910

@@ -23,10 +24,10 @@ fn generate_icosphere<'a>(arena: &mut Arena<'a>, subdivision_frequency: u32) ->
2324

2425
let triangulation_number = subdivision_frequency.pow(2);
2526
let n_vertices = 10 * triangulation_number + 2;
26-
let mut vertices: ArenaVec<'_, Vertex, VertexID> = arena.vec(VertexID(n_vertices));
27+
let mut vertices: ArenaVec<'_, Vertex, VertexID> = arena.with_capacity(n_vertices as usize);
2728

2829
let n_faces = 20 * triangulation_number;
29-
let mut faces: ArenaDeque<'_, Face, u32> = arena.deque(n_faces);
30+
let mut faces: ArenaDeque<'_, Face, u32> = arena.with_capacity(n_faces as usize);
3031

3132
// each edge's midpoint is calculated twice, once for each adjacent face
3233
// so we can cache the calculated midpoint the first time,
@@ -38,7 +39,7 @@ fn generate_icosphere<'a>(arena: &mut Arena<'a>, subdivision_frequency: u32) ->
3839

3940
let mut tmp = arena.make_sub_arena();
4041
let mut edge_subdivision_cache: ArenaVec<(VertexID, VertexID, VertexID), u32> =
41-
tmp.vec(30 * (subdivision_frequency / 2).pow(2));
42+
tmp.with_capacity(30 * (subdivision_frequency / 2).pow(2) as usize);
4243

4344
fn subdivide_edge(
4445
a: VertexID,

src/arena.rs

+30-29
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//!
1414
//! ```compile_fail
1515
//! use core::mem::MaybeUninit;
16-
//! use coca::{Arena, Box};
16+
//! use coca::arena::{Arena, Box};
1717
//!
1818
//! let bad_array = {
1919
//! let mut backing_region = [MaybeUninit::uninit(); 256];
@@ -33,7 +33,7 @@
3333
//!
3434
//! ```no_run
3535
//! # use core::mem::MaybeUninit;
36-
//! # use coca::{Arena, Box};
36+
//! # use coca::arena::{Arena, Box};
3737
//! // callers must drop the return value before they can allocate from `arena` again!
3838
//! fn semi_bad_array<'a>(arena: &'a mut Arena) -> Box<'a, [i32]> {
3939
//! let mut sub = arena.make_sub_arena();
@@ -57,9 +57,9 @@
5757
//! your target platform's pointer size and the alignment of the passed buffer).
5858
//! This does not apply to creating sub-arenas.
5959
60-
use crate::cache::CacheTable;
60+
use crate::collections::cache::CacheTable;
61+
use crate::collections::ArenaString;
6162
use crate::storage::{ArenaStorage, ArrayLike, Capacity, LayoutSpec};
62-
use crate::string::ArenaString;
6363

6464
use core::alloc::Layout;
6565
use core::cmp::Ordering;
@@ -375,7 +375,7 @@ impl<'src> From<&'src mut [MaybeUninit<u8>]> for Arena<'src> {
375375
/// # Examples
376376
/// ```
377377
/// use core::mem::MaybeUninit;
378-
/// use coca::Arena;
378+
/// use coca::arena::Arena;
379379
///
380380
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
381381
/// let arena = Arena::from(&mut backing_region[..]);
@@ -402,7 +402,7 @@ impl Arena<'static> {
402402
/// # fn test() -> Option<()> {
403403
/// # let ptr;
404404
/// {
405-
/// let mut arena = coca::Arena::try_static_with_capacity(1024 * 1024)?;
405+
/// let mut arena = coca::arena::Arena::try_static_with_capacity(1024 * 1024)?;
406406
/// # ptr = arena.alloc(()).as_mut() as *mut () as *mut u8;
407407
/// let hello = coca::fmt!(arena, "{}, {}!", "Hello", "World")?;
408408
/// assert_eq!(hello.as_ref(), "Hello, World!");
@@ -500,7 +500,7 @@ impl<'src> Arena<'src> {
500500
/// # Examples
501501
/// ```
502502
/// use core::mem::MaybeUninit;
503-
/// use coca::Arena;
503+
/// use coca::arena::Arena;
504504
///
505505
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
506506
/// let mut arena = Arena::from(&mut backing_region[..]);
@@ -588,7 +588,7 @@ impl<'src> Arena<'src> {
588588
/// # Examples
589589
/// ```
590590
/// # use core::mem::MaybeUninit;
591-
/// # use coca::{arena::Arena, pool::direct::DirectPool};
591+
/// # use coca::{arena::Arena, collections::pool::direct::DirectPool};
592592
/// # fn test() -> Option<()> {
593593
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
594594
/// let mut arena = Arena::from(&mut backing_region[..]);
@@ -631,7 +631,7 @@ impl<'src> Arena<'src> {
631631
/// # Examples
632632
/// ```
633633
/// use core::mem::MaybeUninit;
634-
/// use coca::Arena;
634+
/// use coca::arena::Arena;
635635
///
636636
/// # fn test() -> Option<()> {
637637
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
@@ -675,7 +675,7 @@ impl<'src> Arena<'src> {
675675
/// # Examples
676676
/// ```
677677
/// use core::mem::MaybeUninit;
678-
/// use coca::Arena;
678+
/// use coca::arena::Arena;
679679
///
680680
/// # fn test() -> Option<()> {
681681
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
@@ -718,7 +718,7 @@ impl<'src> Arena<'src> {
718718
/// # Examples
719719
/// ```
720720
/// use core::mem::MaybeUninit;
721-
/// use coca::Arena;
721+
/// use coca::arena::Arena;
722722
///
723723
/// # fn test() -> Option<()> {
724724
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
@@ -767,7 +767,7 @@ impl<'src> Arena<'src> {
767767
/// # Examples
768768
/// ```
769769
/// use core::mem::MaybeUninit;
770-
/// use coca::Arena;
770+
/// use coca::arena::Arena;
771771
///
772772
/// # fn test() -> Option<()> {
773773
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
@@ -825,7 +825,7 @@ impl<'src> Arena<'src> {
825825
/// # Examples
826826
/// ```
827827
/// use core::mem::MaybeUninit;
828-
/// use coca::Arena;
828+
/// use coca::arena::Arena;
829829
///
830830
/// # fn test() -> Option<()> {
831831
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
@@ -896,7 +896,7 @@ impl<'src> Arena<'src> {
896896
/// # Examples
897897
/// ```
898898
/// use core::mem::MaybeUninit;
899-
/// use coca::Arena;
899+
/// use coca::arena::Arena;
900900
///
901901
/// # fn test() -> Option<()> {
902902
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
@@ -935,7 +935,7 @@ impl<'src> Arena<'src> {
935935
///
936936
/// # Examples
937937
/// ```
938-
/// use coca::{Arena, ArenaVec};
938+
/// use coca::{arena::Arena, collections::ArenaVec};
939939
/// use core::mem::MaybeUninit;
940940
///
941941
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
@@ -968,14 +968,14 @@ impl<'src> Arena<'src> {
968968
///
969969
/// # Examples
970970
/// ```
971-
/// use coca::Arena;
971+
/// use coca::arena::Arena;
972972
/// use core::mem::MaybeUninit;
973973
///
974974
/// # fn test() -> Option<()> {
975975
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
976976
/// let mut arena = Arena::from(&mut backing_region[..]);
977977
///
978-
/// let mut s: coca::ArenaString<'_, usize> = arena.try_string_from("Hello, World!")?;
978+
/// let mut s: coca::collections::ArenaString<'_, usize> = arena.try_string_from("Hello, World!")?;
979979
/// assert_eq!(s, "Hello, World!");
980980
/// assert_eq!(s.len(), s.capacity());
981981
/// # Some(()) }
@@ -1008,7 +1008,7 @@ impl<'src> Arena<'src> {
10081008
///
10091009
/// # Examples
10101010
/// ```
1011-
/// use coca::Arena;
1011+
/// use coca::arena::Arena;
10121012
/// use core::mem::MaybeUninit;
10131013
///
10141014
/// # fn test() -> Option<()> {
@@ -1035,7 +1035,7 @@ impl<'src> Arena<'src> {
10351035
///
10361036
/// Returns `None` if the remaining space in the arena is insufficient.
10371037
#[allow(clippy::type_complexity)]
1038-
pub fn try_cache_with_hasher<K: Eq + Hash, V, L: crate::cache::CacheLine<K, V>, H: BuildHasher>(&mut self, capacity: usize, hash_builder: H) -> Option<CacheTable<K, V, ArenaStorage<'src, ArrayLike<L>>, L, H>> {
1038+
pub fn try_cache_with_hasher<K: Eq + Hash, V, L: crate::collections::cache::CacheLine<K, V>, H: BuildHasher>(&mut self, capacity: usize, hash_builder: H) -> Option<CacheTable<K, V, ArenaStorage<'src, ArrayLike<L>>, L, H>> {
10391039
let capacity = (capacity + L::CAPACITY - 1) / L::CAPACITY;
10401040
let storage = self.try_storage_with_capacity(capacity)?;
10411041
Some(CacheTable::from_storage_and_hasher(storage, hash_builder))
@@ -1047,7 +1047,7 @@ impl<'src> Arena<'src> {
10471047
/// Panics if the remaining space in the arena is insufficient to exhaust
10481048
/// the iterator. See [`try_cache_with_hasher`](Arena::try_cache_with_hasher)
10491049
/// for a checked version that never panics.
1050-
pub fn cache_with_hasher<K: Eq + Hash, V, L: crate::cache::CacheLine<K, V>, H: BuildHasher>(&mut self, capacity: usize, hash_builder: H) -> CacheTable<K, V, ArenaStorage<'src, ArrayLike<L>>, L, H> {
1050+
pub fn cache_with_hasher<K: Eq + Hash, V, L: crate::collections::cache::CacheLine<K, V>, H: BuildHasher>(&mut self, capacity: usize, hash_builder: H) -> CacheTable<K, V, ArenaStorage<'src, ArrayLike<L>>, L, H> {
10511051
self.try_cache_with_hasher(capacity, hash_builder)
10521052
.expect("unexpected allocation failure in cache_with_hasher")
10531053
}
@@ -1056,8 +1056,8 @@ impl<'src> Arena<'src> {
10561056
///
10571057
/// # Panics
10581058
/// Panics if the remaining space in the arena is insufficient to exhaust
1059-
/// the iterator. See [`try_collect`](Arena::try_collect) for a checked
1060-
/// version that never panics.
1059+
/// the iterator. See [`try_collect_slice`](Arena::try_collect_slice)
1060+
/// for a checked version that never panics.
10611061
#[inline]
10621062
#[track_caller]
10631063
pub fn collect_slice<T, I>(&mut self, iter: I) -> Box<'src, [T]>
@@ -1077,7 +1077,7 @@ impl<'src> Arena<'src> {
10771077
/// # Examples
10781078
/// ```
10791079
/// use core::mem::{MaybeUninit, size_of, size_of_val};
1080-
/// use coca::Arena;
1080+
/// use coca::arena::Arena;
10811081
///
10821082
/// # fn test() -> Option<()> {
10831083
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
@@ -1197,14 +1197,14 @@ impl<'src> Arena<'src> {
11971197
/// # Examples
11981198
/// ```
11991199
/// use core::mem::{MaybeUninit, size_of, size_of_val};
1200-
/// use coca::Arena;
1200+
/// use coca::arena::Arena;
12011201
///
12021202
/// # fn test() -> Option<()> {
12031203
/// let mut backing_region = [MaybeUninit::uninit(); 1024];
12041204
/// let mut arena = Arena::from(&mut backing_region[..]);
12051205
///
12061206
/// let chars = ['a', 'b', 'c', 'd', 'e'];
1207-
/// let s: coca::ArenaString<'_, usize> = arena.try_collect_with_capacity(chars.iter(), 8)?;
1207+
/// let s: coca::collections::ArenaString<'_, usize> = arena.try_collect_with_capacity(chars.iter(), 8)?;
12081208
///
12091209
/// assert_eq!(s, "abcde");
12101210
/// # Some(())
@@ -1232,7 +1232,7 @@ impl<'src> Arena<'src> {
12321232
///
12331233
/// # Examples
12341234
/// ```
1235-
/// use coca::{Arena, Box};
1235+
/// use coca::arena::{Arena, Box};
12361236
/// use core::{fmt::Write, mem::MaybeUninit};
12371237
///
12381238
/// # fn main() -> Result<(), core::fmt::Error> {
@@ -1265,7 +1265,7 @@ impl Arena<'_> {
12651265
/// # Examples
12661266
/// ```
12671267
/// use core::mem::MaybeUninit;
1268-
/// use coca::Arena;
1268+
/// use coca::arena::Arena;
12691269
///
12701270
/// let mut backing_region = [MaybeUninit::uninit(); 256];
12711271
/// let mut arena = Arena::from(&mut backing_region[..]);
@@ -1387,7 +1387,7 @@ impl<'buf> From<Writer<'_, 'buf>> for Box<'buf, str> {
13871387
///
13881388
/// # Examples
13891389
/// ```
1390-
/// use coca::{Arena, fmt};
1390+
/// use coca::{arena::Arena, fmt};
13911391
/// use core::mem::MaybeUninit;
13921392
///
13931393
/// # fn test() -> Option<()> {
@@ -1402,11 +1402,12 @@ impl<'buf> From<Writer<'_, 'buf>> for Box<'buf, str> {
14021402
#[macro_export]
14031403
macro_rules! fmt {
14041404
($arena:expr, $($arg:tt)*) => {{
1405+
#[allow(unused_imports)]
14051406
use core::fmt::Write;
14061407
let mut writer = $arena.make_writer();
14071408
core::write!(writer, $($arg)*)
14081409
.ok()
1409-
.map(|_| $crate::Box::<'_, str>::from(writer))
1410+
.map(|_| $crate::arena::Box::<'_, str>::from(writer))
14101411
}}
14111412
}
14121413

0 commit comments

Comments
 (0)