Skip to content

Commit

Permalink
Get rid of generic-array
Browse files Browse the repository at this point in the history
  • Loading branch information
cmichi committed Nov 26, 2020
1 parent 527e0ac commit 41b6294
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 181 deletions.
1 change: 0 additions & 1 deletion crates/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ derive_more = { version = "0.99", default-features = false, features = ["from",
scale-info = { version = "0.4", default-features = false, features = ["derive"], optional = true }
cfg-if = "1.0"
array-init = "1.0"
generic-array = "0.14.1"

# Workaround: we actually just need criterion as a dev-dependency, but
# there is an issue with a doubly included std lib when executing
Expand Down
25 changes: 8 additions & 17 deletions crates/storage/src/collections/smallvec/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,25 @@ use super::{
SmallVec,
};
use crate::{
lazy::LazyArrayLength,
traits::PackedLayout,
};
use core::iter::{
Extend,
FromIterator,
};

impl<T, N> Drop for SmallVec<T, N>
impl<T, const N: usize> Drop for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn drop(&mut self) {
self.clear_cells()
}
}

impl<T, N> core::ops::Index<u32> for SmallVec<T, N>
impl<T, const N: usize> core::ops::Index<u32> for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
type Output = T;

Expand All @@ -56,10 +53,9 @@ where
}
}

impl<T, N> core::ops::IndexMut<u32> for SmallVec<T, N>
impl<T, const N: usize> core::ops::IndexMut<u32> for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn index_mut(&mut self, index: u32) -> &mut Self::Output {
let len = self.len();
Expand All @@ -75,10 +71,9 @@ where
}
}

impl<'a, T: 'a, N> IntoIterator for &'a SmallVec<T, N>
impl<'a, T: 'a, const N: usize> IntoIterator for &'a SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
type Item = &'a T;
type IntoIter = Iter<'a, T, N>;
Expand All @@ -88,10 +83,9 @@ where
}
}

impl<T, N> Extend<T> for SmallVec<T, N>
impl<T, const N: usize> Extend<T> for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn extend<I>(&mut self, iter: I)
where
Expand All @@ -103,10 +97,9 @@ where
}
}

impl<T, N> FromIterator<T> for SmallVec<T, N>
impl<T, const N: usize> FromIterator<T> for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn from_iter<I>(iter: I) -> Self
where
Expand All @@ -118,10 +111,9 @@ where
}
}

impl<T, N> core::cmp::PartialEq for SmallVec<T, N>
impl<T, const N: usize> core::cmp::PartialEq for SmallVec<T, N>
where
T: PartialEq + PackedLayout,
N: LazyArrayLength<T>,
{
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
Expand All @@ -131,9 +123,8 @@ where
}
}

impl<T, N> core::cmp::Eq for SmallVec<T, N>
impl<T, const N: usize> core::cmp::Eq for SmallVec<T, N>
where
T: Eq + PackedLayout,
N: LazyArrayLength<T>,
{
}
34 changes: 11 additions & 23 deletions crates/storage/src/collections/smallvec/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
use super::SmallVec;
use crate::{
collections::extend_lifetime,
lazy::LazyArrayLength,
traits::PackedLayout,
};

/// An iterator over shared references to the elements of a small storage vector.
#[derive(Debug, Clone, Copy)]
pub struct Iter<'a, T, N>
pub struct Iter<'a, T, const N: usize>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// The storage vector to iterate over.
vec: &'a SmallVec<T, N>,
Expand All @@ -34,10 +32,9 @@ where
end: u32,
}

impl<'a, T, N> Iter<'a, T, N>
impl<'a, T, const N: usize> Iter<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Creates a new iterator for the given storage vector.
pub(crate) fn new(vec: &'a SmallVec<T, N>) -> Self {
Expand All @@ -54,10 +51,9 @@ where
}
}

impl<'a, T, N> Iterator for Iter<'a, T, N>
impl<'a, T, const N: usize> Iterator for Iter<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
type Item = &'a T;

Expand Down Expand Up @@ -86,17 +82,15 @@ where
}
}

impl<'a, T, N> ExactSizeIterator for Iter<'a, T, N>
impl<'a, T, const N: usize> ExactSizeIterator for Iter<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
}

impl<'a, T, N> DoubleEndedIterator for Iter<'a, T, N>
impl<'a, T, const N: usize> DoubleEndedIterator for Iter<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn next_back(&mut self) -> Option<Self::Item> {
<Self as DoubleEndedIterator>::nth_back(self, 0)
Expand All @@ -118,10 +112,9 @@ where

/// An iterator over exclusive references to the elements of a small storage vector.
#[derive(Debug)]
pub struct IterMut<'a, T, N>
pub struct IterMut<'a, T, const N: usize>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// The storage vector to iterate over.
vec: &'a mut SmallVec<T, N>,
Expand All @@ -131,10 +124,9 @@ where
end: u32,
}

impl<'a, T, N> IterMut<'a, T, N>
impl<'a, T, const N: usize> IterMut<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Creates a new iterator for the given storage vector.
pub(crate) fn new(vec: &'a mut SmallVec<T, N>) -> Self {
Expand All @@ -152,10 +144,9 @@ where
}
}

impl<'a, T, N> IterMut<'a, T, N>
impl<'a, T, const N: usize> IterMut<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn get_mut<'b>(&'b mut self, at: u32) -> Option<&'a mut T> {
self.vec.get_mut(at).map(|value| {
Expand All @@ -171,10 +162,9 @@ where
}
}

impl<'a, T, N> Iterator for IterMut<'a, T, N>
impl<'a, T, const N: usize> Iterator for IterMut<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
type Item = &'a mut T;

Expand Down Expand Up @@ -203,17 +193,15 @@ where
}
}

impl<'a, T, N> ExactSizeIterator for IterMut<'a, T, N>
impl<'a, T, const N: usize> ExactSizeIterator for IterMut<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
}

impl<'a, T, N> DoubleEndedIterator for IterMut<'a, T, N>
impl<'a, T, const N: usize> DoubleEndedIterator for IterMut<'a, T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn next_back(&mut self) -> Option<Self::Item> {
<Self as DoubleEndedIterator>::nth_back(self, 0)
Expand Down
22 changes: 7 additions & 15 deletions crates/storage/src/collections/smallvec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use crate::{
lazy::{
Lazy,
LazyArray,
LazyArrayLength,
},
traits::PackedLayout,
};
Expand All @@ -55,31 +54,28 @@ type Index = u32;
/// `Vec` due to the internal differences.
/// - Allows to store up to N elements.
#[derive(Debug)]
pub struct SmallVec<T, N>
pub struct SmallVec<T, const N: usize>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// The current length of the small vector.
len: Lazy<u32>,
/// The entries of the small vector.
elems: LazyArray<T, N>,
}

impl<T, N> Default for SmallVec<T, N>
impl<T, const N: usize> Default for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
fn default() -> Self {
Self::new()
}
}

impl<T, N> SmallVec<T, N>
impl<T, const N: usize> SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Clears the underlying storage cells of the storage vector.
///
Expand All @@ -102,10 +98,9 @@ where
}
}

impl<T, N> SmallVec<T, N>
impl<T, const N: usize> SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Creates a new empty vector.
pub fn new() -> Self {
Expand Down Expand Up @@ -134,10 +129,9 @@ where
}
}

impl<T, N> SmallVec<T, N>
impl<T, const N: usize> SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Returns an iterator yielding shared references to all elements.
///
Expand Down Expand Up @@ -195,10 +189,9 @@ where
}
}

impl<T, N> SmallVec<T, N>
impl<T, const N: usize> SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Appends an element to the back of the vector.
pub fn push(&mut self, value: T) {
Expand All @@ -212,10 +205,9 @@ where
}
}

impl<T, N> SmallVec<T, N>
impl<T, const N: usize> SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
/// Pops the last element from the vector and returns it.
//
Expand Down
10 changes: 3 additions & 7 deletions crates/storage/src/collections/smallvec/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@

use super::SmallVec;
use crate::{
lazy::LazyArrayLength,
traits::{
KeyPtr,
PackedLayout,
SpreadLayout,
},
};
use generic_array::typenum::Unsigned;

#[cfg(feature = "std")]
const _: () = {
Expand All @@ -36,10 +34,9 @@ const _: () = {
};
use scale_info::TypeInfo;

impl<T, N> StorageLayout for SmallVec<T, N>
impl<T, const N: usize> StorageLayout for SmallVec<T, N>
where
T: PackedLayout + TypeInfo + 'static,
N: LazyArrayLength<T>,
{
fn layout(key_ptr: &mut KeyPtr) -> Layout {
Layout::Struct(StructLayout::new(vec![
Expand All @@ -53,12 +50,11 @@ const _: () = {
}
};

impl<T, N> SpreadLayout for SmallVec<T, N>
impl<T, const N: usize> SpreadLayout for SmallVec<T, N>
where
T: PackedLayout,
N: LazyArrayLength<T>,
{
const FOOTPRINT: u64 = 1 + <N as Unsigned>::U64;
const FOOTPRINT: u64 = 1 + N as u64;

fn pull_spread(ptr: &mut KeyPtr) -> Self {
Self {
Expand Down
Loading

0 comments on commit 41b6294

Please sign in to comment.