diff --git a/Cargo.lock b/Cargo.lock index 120289c..8729cdc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "autocfg" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "duplicate" @@ -20,7 +20,7 @@ dependencies = [ [[package]] name = "eastl-rs" -version = "0.14.1" +version = "0.15.0" dependencies = [ "duplicate", "itertools", @@ -31,9 +31,9 @@ dependencies = [ [[package]] name = "either" -version = "1.10.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "heck" @@ -91,18 +91,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.79" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -131,6 +131,6 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "version_check" -version = "0.9.4" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" diff --git a/Cargo.toml b/Cargo.toml index c19a8b1..5dda92b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/allocator.rs b/src/allocator.rs index c699e74..24c590f 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -46,7 +46,7 @@ pub unsafe trait Allocator { /// `p` must be a valid pointer to an array with size `n`. unsafe fn deallocate(&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::(), std::mem::align_of::(), ) @@ -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::(n).unwrap().align_to(align).unwrap(), ) } diff --git a/src/fixed_list/mod.rs b/src/fixed_list/mod.rs index aae4e4e..26bc7f0 100644 --- a/src/fixed_list/mod.rs +++ b/src/fixed_list/mod.rs @@ -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. @@ -64,94 +65,6 @@ impl )); }) } - - /// 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 { - 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 { - 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)] @@ -175,6 +88,24 @@ impl fmt:: } } +impl Deref + for FixedList +{ + type Target = List, OverflowAllocator>>; + + fn deref(&self) -> &Self::Target { + &self.base_list + } +} + +impl DerefMut + for FixedList +{ + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base_list + } +} + #[cfg(test)] mod test { use crate::allocator::DefaultAllocator; diff --git a/src/fixed_map/mod.rs b/src/fixed_map/mod.rs index f189be8..9ced45c 100644 --- a/src/fixed_map/mod.rs +++ b/src/fixed_map/mod.rs @@ -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. @@ -82,73 +79,30 @@ impl< } } -impl> - FixedMapImpl +impl< + K: PartialEq, + V, + const NODE_COUNT: usize, + A: PoolAllocator + Default, + C: Compare + Default, + > Deref for FixedMapImpl { - /// 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; - /// 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 { - 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 + Default, + > DerefMut for FixedMapImpl +{ + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base_map } } diff --git a/src/fixed_vector/mod.rs b/src/fixed_vector/mod.rs index 3e9936b..7dc0538 100644 --- a/src/fixed_vector/mod.rs +++ b/src/fixed_vector/mod.rs @@ -6,7 +6,7 @@ use moveit::{new, MoveNew, MoveRef}; use std::ffi::c_void; use std::fmt::Debug; use std::mem::{size_of, MaybeUninit}; -use std::ops::Deref; +use std::ops::{Deref, DerefMut}; use std::pin::Pin; use std::ptr::null_mut; use std::{mem, ptr}; @@ -91,68 +91,15 @@ unsafe impl MoveNew } impl FixedVector { - /// Returns the vector as raw bytes - pub fn as_slice(&self) -> &[T] { - self.base_vec.as_slice() - } - - pub fn as_slice_mut(&mut self) -> &mut [T] { - self.base_vec.as_slice_mut() - } - /// Returns the max fixed size, which is the user-supplied NodeCount parameter pub fn max_size(&self) -> usize { NODE_COUNT } - /// Returns true if the vector is empty - pub fn is_empty(&self) -> bool { - self.base_vec.is_empty() - } - - /// Returns the length of the vector - pub fn len(&self) -> usize { - self.base_vec.len() - } - - /// Returns true if the fixed space has been fully allocated. Note that if overflow is enabled, the container size can be greater than nodeCount but full() could return true because the fixed space may have a recently freed slot. - pub fn is_full(&self) -> bool { - // If len >= capacity (NodeCount), then we are definitely full. - // Also, if our size is smaller but we've switched away from self.buffer due to a previous overflow, then we are considered full. - self.base_vec.len() >= NODE_COUNT - || self.base_vec.begin_ptr.cast_const() != self.buffer[0].as_ptr() - } - /// Returns true if the allocations spilled over into the overflow allocator. Meaningful only if overflow is enabled. pub fn has_overflowed(&self) -> bool { !ptr::eq(self.base_vec.begin_ptr, self.buffer[0].as_ptr()) } - - /// Pushes a new element into the vector - /// - /// # Arguments - /// - /// `elem`: The new element - pub fn push(&mut self, elem: T) { - self.base_vec.push(elem) - } - - /// Pops an element off of the back of the array - pub fn pop(&mut self) -> Option { - self.base_vec.pop() - } - - /// Inserts an element into the array at an index. - /// `index` must be less than or equal to `size` - /// - /// # Arguments - /// - /// `index`: The index to insert the element - /// - /// `elem`: The element to add to the array - pub fn insert(&mut self, index: usize, elem: T) { - self.base_vec.insert(index, elem) - } } impl AsRef<[T]> for FixedVector { @@ -172,10 +119,18 @@ impl Debug impl Deref for FixedVector { - type Target = [T]; + type Target = Vector>; fn deref(&self) -> &Self::Target { - self.as_slice() + &self.base_vec + } +} + +impl DerefMut + for FixedVector +{ + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.base_vec } } @@ -213,7 +168,6 @@ mod test { } assert_eq!(v.len(), 12); assert!(v.has_overflowed()); - assert!(v.is_full()); assert_eq!(v.as_slice()[11], 11); } @@ -257,7 +211,6 @@ mod test { let target = unsafe { target.assume_init_ref() }; assert_eq!(target.len(), 12); assert!(target.has_overflowed()); - assert!(target.is_full()); assert_eq!(target.as_slice()[11], 11); } } diff --git a/src/internal/hash_table/mod.rs b/src/internal/hash_table/mod.rs index a9957c4..f18c54a 100644 --- a/src/internal/hash_table/mod.rs +++ b/src/internal/hash_table/mod.rs @@ -167,7 +167,9 @@ impl, E: Equals> HashTable Self { Self { _pad: 0, - bucket_array: unsafe { std::mem::transmute(EMPTY_BUCKET_ARR.as_ptr()) }, + bucket_array: unsafe { + std::mem::transmute::<*const usize, *mut *mut Node>(EMPTY_BUCKET_ARR.as_ptr()) + }, bucket_count: 1, element_count: 0, rehash_policy: PrimeRehashPolicy::default(), diff --git a/src/list/mod.rs b/src/list/mod.rs index 7490ce2..3bae954 100644 --- a/src/list/mod.rs +++ b/src/list/mod.rs @@ -129,6 +129,16 @@ impl List { IterMut::new(&self.node, self.size()) } + /// Returns true if the list contains no elements. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Returns the length of the list, in elements. + pub fn len(&self) -> u32 { + self.size + } + /// Removes the last element in the list, returning its value /// /// # Return diff --git a/src/list/node.rs b/src/list/node.rs index 93067d9..6aff4b0 100644 --- a/src/list/node.rs +++ b/src/list/node.rs @@ -33,7 +33,7 @@ impl ListNodeBase { } #[repr(C)] -pub(crate) struct ListNode { +pub struct ListNode { pub(crate) base: ListNodeBase, pub(crate) value: T, } diff --git a/src/vector.rs b/src/vector.rs index 7221df8..a27d287 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -67,7 +67,7 @@ impl Vector { if let Some(begin_ptr) = unsafe { self.begin_ptr.as_ref() } { unsafe { std::slice::from_raw_parts(begin_ptr, self.len()) } } else { - &mut [] + &[] } }