Skip to content

Commit

Permalink
improve Array
Browse files Browse the repository at this point in the history
- refactor modules.
- remove `_phantom` field.
- new method `into_array`.
- improve `Debug`.
- fix no-alloc build.
  • Loading branch information
joseluis committed Jul 26, 2023
1 parent e5c61c2 commit e81da62
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 86 deletions.
18 changes: 3 additions & 15 deletions src/list/array/impls.rs → src/list/array/core_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
//!
//

use crate::all::{Array, Direct, Storage};
#[cfg(not(feature = "safe"))]
use core::mem::{self, MaybeUninit};

use core::{
fmt,
marker::PhantomData,
ops::{Deref, DerefMut},
};

use crate::all::{Array, Direct, Storage};

#[cfg(feature = "alloc")]
use {
crate::mem::Boxed,
Expand Down Expand Up @@ -43,7 +40,6 @@ where
fn clone(&self) -> Self {
Self {
array: self.array.clone(),
_phantom: PhantomData,
}
}
}
Expand Down Expand Up @@ -96,7 +92,6 @@ impl<T: Default, const LEN: usize> Default for Array<T, (), LEN> {

Array {
array: Direct::new(data),
_phantom: PhantomData,
}
}
}
Expand Down Expand Up @@ -143,10 +138,7 @@ impl<T: Default, const LEN: usize> Default for Array<T, Boxed, LEN> {
unsafe { Box::from_raw(raw_slice as *mut [T; LEN]) }
};

Array {
array: data,
_phantom: PhantomData,
}
Array { array: data }
}
}

Expand Down Expand Up @@ -207,7 +199,6 @@ where

Array {
array: Direct::new(data),
_phantom: PhantomData,
}
}
}
Expand Down Expand Up @@ -265,9 +256,6 @@ where
unsafe { Box::from_raw(raw_slice as *mut [T; LEN]) }
};

Array {
array: data,
_phantom: PhantomData,
}
Array { array: data }
}
}
31 changes: 20 additions & 11 deletions src/list/array/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#[cfg(not(feature = "safe"))]
use core::mem::{self, MaybeUninit};

use core::marker::PhantomData;

use crate::all::{Array, Direct, Storage};

#[allow(unused)]
Expand All @@ -19,10 +17,10 @@ use {

// ``
impl<T, S: Storage, const LEN: usize> Array<T, S, LEN> {
/// Returns an new `Array` from the given primitive `array`.
pub fn new(array: [T; LEN]) -> Self {
Self {
array: array.into(),
_phantom: PhantomData,
}
}
}
Expand Down Expand Up @@ -57,10 +55,7 @@ impl<T: Clone, const LEN: usize> Array<T, (), LEN> {
#[cfg(feature = "safe")]
let data = Direct::new(core::array::from_fn(|_| element.clone()));

Self {
array: data,
_phantom: PhantomData,
}
Self { array: data }
}
}

Expand Down Expand Up @@ -106,10 +101,7 @@ impl<T: Clone, const LEN: usize> Array<T, Boxed, LEN> {
unsafe { Box::from_raw(raw_slice as *mut [T; LEN]) }
};

Self {
array: data,
_phantom: PhantomData,
}
Self { array: data }
}
}

Expand Down Expand Up @@ -155,3 +147,20 @@ impl<T, S: Storage, const LEN: usize> Array<T, S, LEN> {
self.array.as_mut_slice()
}
}

// `S: Boxed`
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "alloc")))]
impl<T, const LEN: usize> Array<T, Boxed, LEN> {
/// Returns the inner boxed primitive array.
pub fn into_array(self) -> Box<[T; LEN]> {
self.array
}
}
// `S: ()`
impl<T, const LEN: usize> Array<T, (), LEN> {
/// Returns the inner boxed primitive array.
pub fn into_array(self) -> [T; LEN] {
self.array.0
}
}
64 changes: 4 additions & 60 deletions src/list/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,17 @@
//! homogeneous data structures.
//

use core::marker::PhantomData;

#[cfg(feature = "alloc")]
use crate::mem::Boxed;
use crate::mem::Storage;

use crate::{
all::DataCollection,
error::{LadataError as Error, LadataResult as Result},
mem::Storage,
};

mod impls;
mod core_impls;
mod methods;

/// An abstract Array.
///
/// - <https://en.wikipedia.org/wiki/Array_(data_type)#Abstract_arrays>
pub trait DataArray: DataCollection {
///
fn array_get(&self, index: usize) -> Result<&<Self as DataCollection>::Element>;
///
fn array_set(&mut self, index: usize, element: <Self as DataCollection>::Element)
-> Result<()>;
}
mod traits;

/// An array, backed by the core primitive [`array`].
pub struct Array<T, S: Storage, const LEN: usize> {
array: S::Stored<[T; LEN]>,
_phantom: PhantomData<T>,
}

/// An [`Array`] stored in the heap.
Expand All @@ -50,43 +32,5 @@ pub(crate) mod all {
pub use super::BoxedArray;

#[doc(inline)]
pub use super::{Array, DataArray, DirectArray};
}

impl<T, S: Storage, const LEN: usize> DataCollection for Array<T, S, LEN> {
type Element = T;
fn collection_is_empty(&self) -> Option<bool> {
None
}
fn collection_is_full(&self) -> Option<bool> {
None
}
fn collection_capacity(&self) -> usize {
LEN
}
fn collection_len(&self) -> usize {
self.len()
}
}

impl<T, S: Storage, const LEN: usize> DataArray for Array<T, S, LEN> {
fn array_get(&self, index: usize) -> Result<&<Self as DataCollection>::Element> {
if let Some(e) = self.get(index) {
Ok(e)
} else {
Err(Error::IndexOutOfBounds(index))
}
}
fn array_set(
&mut self,
index: usize,
element: <Self as DataCollection>::Element,
) -> Result<()> {
if let Some(e) = self.get_mut(index) {
*e = element;
Ok(())
} else {
Err(Error::IndexOutOfBounds(index))
}
}
pub use super::{traits::DataArray, Array, DirectArray};
}
60 changes: 60 additions & 0 deletions src/list/array/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// ladata::list::array::traits
//
//!
//

use crate::{
error::{LadataError as Error, LadataResult as Result},
list::Array,
mem::Storage,
misc::DataCollection,
};

/// An abstract Array.
///
/// - <https://en.wikipedia.org/wiki/Array_(data_type)#Abstract_arrays>
pub trait DataArray: DataCollection {
///
fn array_get(&self, index: usize) -> Result<&<Self as DataCollection>::Element>;
///
fn array_set(&mut self, index: usize, element: <Self as DataCollection>::Element)
-> Result<()>;
}

impl<T, S: Storage, const LEN: usize> DataCollection for Array<T, S, LEN> {
type Element = T;
fn collection_is_empty(&self) -> Option<bool> {
None
}
fn collection_is_full(&self) -> Option<bool> {
None
}
fn collection_capacity(&self) -> usize {
LEN
}
fn collection_len(&self) -> usize {
self.len()
}
}

impl<T, S: Storage, const LEN: usize> DataArray for Array<T, S, LEN> {
fn array_get(&self, index: usize) -> Result<&<Self as DataCollection>::Element> {
if let Some(e) = self.get(index) {
Ok(e)
} else {
Err(Error::IndexOutOfBounds(index))
}
}
fn array_set(
&mut self,
index: usize,
element: <Self as DataCollection>::Element,
) -> Result<()> {
if let Some(e) = self.get_mut(index) {
*e = element;
Ok(())
} else {
Err(Error::IndexOutOfBounds(index))
}
}
}
10 changes: 10 additions & 0 deletions src/list/bit_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,24 @@ pub type BoxedBitArray<const BITLEN: usize, const BYTECAP: usize> =
BitArray<Boxed, BITLEN, BYTECAP>;

/// An array of 8 bits stored in the heap.
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "alloc")))]
pub type BoxedBitArray8 = BitArray<Boxed, 8, 1>;
/// An array of 16 bits stored in the heap.
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "alloc")))]
pub type BoxedBitArray16 = BitArray<Boxed, 16, 2>;
/// An array of 32 bits stored in the heap.
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "alloc")))]
pub type BoxedBitArray32 = BitArray<Boxed, 32, 4>;
/// An array of 64 bits stored in the heap.
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "alloc")))]
pub type BoxedBitArray64 = BitArray<Boxed, 64, 8>;
/// An array of 128 bits stored in the heap.
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "alloc")))]
pub type BoxedBitArray128 = BitArray<Boxed, 128, 16>;

pub use all::*;
Expand Down

0 comments on commit e81da62

Please sign in to comment.