Skip to content

Commit

Permalink
Implement Fixed containers in terms of their base types, bump version…
Browse files Browse the repository at this point in the history
… to 0.15
  • Loading branch information
MrElectrify committed Aug 20, 2024
1 parent bef1c49 commit c96f378
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 232 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "eastl-rs"
authors = ["Andrew Buck"]
description = "EASTL binary-compatible Rust implementations"
documentation = "https://docs.rs/crate/eastl-rs"
version = "0.14.1"
version = "0.15.0"
edition = "2021"
license = "MIT"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub unsafe trait Allocator {
/// `p` must be a valid pointer to an array with size `n`.
unsafe fn deallocate<T>(&mut self, p: *mut T, n: usize) {
self.deallocate_raw_aligned(
std::mem::transmute(p),
std::mem::transmute::<*mut T, *mut ()>(p),
n * std::mem::size_of::<T>(),
std::mem::align_of::<T>(),
)
Expand Down Expand Up @@ -104,7 +104,7 @@ unsafe impl Allocator for DefaultAllocator {

unsafe fn deallocate_raw_aligned(&mut self, p: *mut (), n: usize, align: usize) {
alloc::dealloc(
std::mem::transmute(p),
std::mem::transmute::<*mut (), *mut u8>(p),
Layout::array::<u8>(n).unwrap().align_to(align).unwrap(),
)
}
Expand Down
107 changes: 19 additions & 88 deletions src/fixed_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::list::List;
use moveit::{new, New};
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
use std::{fmt, mem, slice};

/// A fixed list which uses the default allocator as an overflow.
Expand Down Expand Up @@ -64,94 +65,6 @@ impl<T, const NODE_COUNT: usize, OverflowAllocator: Allocator>
));
})
}

/// Get a reference to the last value, if any
///
/// # Return
/// A reference to the last value if present, `None` if the list is empty.
pub fn back(&self) -> Option<&T> {
self.base_list.back()
}

/// Get a mutable reference to the last value, if any
///
/// # Return
/// A mutable reference to the last value if present, `None` if the list is empty.
pub fn back_mut(&mut self) -> Option<&mut T> {
self.base_list.back_mut()
}

/// Remove all elements from this list
pub fn clear(&mut self) {
self.base_list.clear()
}

/// Returns the number of occupied elements in the list.
pub fn len(&self) -> usize {
self.size()
}

/// If the list is empty or not
pub fn is_empty(&self) -> bool {
self.base_list.empty()
}

/// Get a reference to the first value, if any
///
/// # Return
/// A reference to the first value if present, `None` if the list is empty.
pub fn front(&self) -> Option<&T> {
self.base_list.front()
}

/// Get a mutable reference to the first value, if any
///
/// # Return
/// A mutable reference to the first value if present, `None` if the list is empty.
pub fn front_mut(&mut self) -> Option<&mut T> {
self.base_list.front_mut()
}

/// Return a forward iterator for this list
pub fn iter(&self) -> crate::list::iter::Iter<'_, T> {
self.base_list.iter()
}

/// Return a mutable forward iterator for this list
pub fn iter_mut(&self) -> crate::list::iter::IterMut<'_, T> {
self.base_list.iter_mut()
}

/// Removes the last element in the list, returning its value
///
/// # Return
/// The last value if present, `None` if the list is empty.
pub fn pop_back(&mut self) -> Option<T> {
self.base_list.pop_back()
}

/// Removes the first element in the list, returning its value
///
/// # Return
/// The first value if present, `None` if the list is empty.
pub fn pop_front(&mut self) -> Option<T> {
self.base_list.pop_front()
}

/// Push a value to the back of the list
pub fn push_back(&mut self, value: T) {
self.base_list.push_back(value)
}

/// Push a value to the front of the list
pub fn push_front(&mut self, value: T) {
self.base_list.push_front(value)
}

/// Get the list's size
pub fn size(&self) -> usize {
self.base_list.size()
}
}

#[allow(private_bounds)]
Expand All @@ -175,6 +88,24 @@ impl<T: fmt::Debug, const NODE_COUNT: usize, OverflowAllocator: Allocator> fmt::
}
}

impl<T, const NODE_COUNT: usize, OverflowAllocator: Allocator + Default> Deref
for FixedList<T, NODE_COUNT, OverflowAllocator>
{
type Target = List<T, FixedPoolWithOverflow<ListNode<T>, OverflowAllocator>>;

fn deref(&self) -> &Self::Target {
&self.base_list
}
}

impl<T, const NODE_COUNT: usize, OverflowAllocator: Allocator + Default> DerefMut
for FixedList<T, NODE_COUNT, OverflowAllocator>
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.base_list
}
}

#[cfg(test)]
mod test {
use crate::allocator::DefaultAllocator;
Expand Down
92 changes: 23 additions & 69 deletions src/fixed_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ use crate::allocator::{Allocator, DefaultAllocator};
use crate::compare::{Compare, Less};
use crate::fixed_pool::with_overflow::FixedPoolWithOverflow;
use crate::fixed_pool::{FixedPool, PoolAllocator};
use crate::internal::rb_tree::{
iter::{Iter, IterMut},
node::Node,
};
use crate::internal::rb_tree::node::Node;
use crate::map::Map;
use duplicate::duplicate_item;
use moveit::{new, New};
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
use std::{mem, slice};

/// A fixed map with overflow which uses the default allocator as an overflow.
Expand Down Expand Up @@ -82,73 +79,30 @@ impl<
}
}

impl<K: PartialEq, V, const NODE_COUNT: usize, A: Allocator, C: Compare<K>>
FixedMapImpl<K, V, NODE_COUNT, A, C>
impl<
K: PartialEq,
V,
const NODE_COUNT: usize,
A: PoolAllocator + Default,
C: Compare<K> + Default,
> Deref for FixedMapImpl<K, V, NODE_COUNT, A, C>
{
/// Clears the map, removing all key-value pairs
pub fn clear(&mut self) {
self.base_map.clear()
}

/// Returns true if the map contains a pair indexed
/// by the given key
///
/// # Arguments
///
/// `key`: The key to index the pair
pub fn contains_key(&self, key: &K) -> bool {
self.base_map.contains_key(key)
}

/// Fetches the value indexed by the key in the map
///
/// # Arguments
///
/// `key`: The key to index the pair
pub fn get(&self, key: &K) -> Option<&V> {
self.base_map.get(key)
}
type Target = Map<K, V, A, C>;

/// Fetches the value indexed by the key in the map
///
/// # Arguments
///
/// `key`: The key to index the pair
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.base_map.get_mut(key)
}

/// Returns true if the map contains no elements
pub fn is_empty(&self) -> bool {
self.base_map.is_empty()
}

/// Returns an iterator over the elements in the map.
///
/// # Safety
/// This iterator is not tested as trees are only partially implemented.
#[duplicate_item(
iter Self Iter;
[iter] [&Self] [Iter];
[iter_mut] [&mut Self] [IterMut];
)]
#[allow(clippy::needless_arbitrary_self_type)]
pub unsafe fn iter(self: Self) -> Iter<K, V> {
self.base_map.iter()
}

/// Returns the number of elements in the map
pub fn len(&self) -> usize {
self.base_map.len()
fn deref(&self) -> &Self::Target {
&self.base_map
}
}

/// Removes a key-value pair from the map,
/// returning the pair if it was found
///
/// # Arguments
///
/// `key`: The key to index the pair
pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)> {
self.base_map.remove_entry(key)
impl<
K: PartialEq,
V,
const NODE_COUNT: usize,
A: PoolAllocator + Default,
C: Compare<K> + Default,
> DerefMut for FixedMapImpl<K, V, NODE_COUNT, A, C>
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.base_map
}
}
Loading

0 comments on commit c96f378

Please sign in to comment.