diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8483b2c..6bb9327 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -13,6 +13,7 @@ env: jobs: test: strategy: + fail-fast: false matrix: os: - ubuntu-latest diff --git a/src/array.rs b/src/array.rs index c8d485d..2ed0a8a 100644 --- a/src/array.rs +++ b/src/array.rs @@ -13,8 +13,6 @@ pub enum IArray { Static(&'static [T]), /// A reference counted slice. Rc(Rc<[T]>), - /// A single element. - Single([T; 1]), } // TODO add insta tests @@ -23,7 +21,6 @@ impl fmt::Debug for IArray { match self { Self::Static(a) => a.fmt(f), Self::Rc(a) => a.fmt(f), - Self::Single(x) => x.fmt(f), } } } @@ -33,7 +30,6 @@ impl Clone for IArray { match self { Self::Static(a) => Self::Static(a), Self::Rc(a) => Self::Rc(a.clone()), - Self::Single(x) => Self::Single(x.clone()), } } } @@ -51,7 +47,7 @@ impl FromIterator for IArray { (_, Some(0)) => Self::EMPTY, (_, Some(1)) => { if let Some(element) = it.next() { - Self::Single([element]) + Self::from([element]) } else { Self::EMPTY } @@ -87,9 +83,9 @@ impl From<&IArray> for IArray { } } -impl From<[T; 1]> for IArray { - fn from(a: [T; 1]) -> IArray { - IArray::Single(a) +impl From<[T; N]> for IArray { + fn from(a: [T; N]) -> IArray { + IArray::Rc(Rc::from(a)) } } @@ -176,7 +172,6 @@ impl IArray { match self { Self::Static(a) => a.len(), Self::Rc(a) => a.len(), - Self::Single(_) => 1, } } @@ -197,7 +192,6 @@ impl IArray { match self { Self::Static(a) => a.is_empty(), Self::Rc(a) => a.is_empty(), - Self::Single(_) => false, } } @@ -218,7 +212,6 @@ impl IArray { match self { Self::Static(a) => a, Self::Rc(a) => a, - Self::Single(a) => a, } } @@ -237,8 +230,6 @@ impl IArray { match self { Self::Static(a) => a.get(index).cloned(), Self::Rc(a) => a.get(index).cloned(), - Self::Single(a) if index == 0 => Some(a[0].clone()), - Self::Single(_) => None, } } @@ -265,17 +256,12 @@ impl IArray { /// // Static references are immutable /// let mut v3 = IArray::::Static(&[1,2,3]); /// assert!(v3.get_mut().is_none()); - /// - /// // Single items always return a mutable reference - /// let mut v4 = IArray::::Single([1]); - /// assert!(v4.get_mut().is_some()); /// ``` #[inline] pub fn get_mut(&mut self) -> Option<&mut [T]> { match self { Self::Rc(ref mut rc) => Rc::get_mut(rc), Self::Static(_) => None, - Self::Single(ref mut a) => Some(a), } } @@ -288,8 +274,6 @@ impl IArray { /// If this array is a `Static`, it clones its elements into a new `Rc` array and returns a /// mutable slice into that new array. /// - /// If this array is a `Single` element, the inner array is returned directly. - /// /// # Examples /// /// ``` @@ -324,18 +308,6 @@ impl IArray { /// assert!(matches!(data, IArray::::Rc(_))); /// assert!(matches!(other_data, IArray::::Static(_))); /// ``` - /// - /// ``` - /// # use implicit_clone::unsync::*; - /// // This will use the inner array directly - /// let mut data = IArray::::Single([1]); - /// let other_data = data.clone(); - /// data.make_mut()[0] = 123; - /// assert_eq!(&[123], data.as_slice()); - /// assert_eq!(&[1], other_data.as_slice()); - /// assert!(matches!(data, IArray::::Single(_))); - /// assert!(matches!(other_data, IArray::::Single(_))); - /// ``` #[inline] pub fn make_mut(&mut self) -> &mut [T] { match self { @@ -356,7 +328,6 @@ impl IArray { _ => unreachable!(), } } - Self::Single(array) => array, } } } @@ -369,8 +340,6 @@ where match self { Self::Static(a) => a.eq(other), Self::Rc(a) => a.eq(*other), - Self::Single(a) if N == 1 => a[0].eq(&other[0]), - Self::Single(_) => false, } } } @@ -383,8 +352,6 @@ where match self { Self::Static(a) => a.eq(other), Self::Rc(a) => a.eq(other), - Self::Single(a) if N == 1 => a[0].eq(&other[0]), - Self::Single(_) => false, } } } @@ -397,7 +364,6 @@ where match self { Self::Static(a) => a.eq(&other), Self::Rc(a) => a.eq(other), - Self::Single(a) => a.eq(other), } } } @@ -410,7 +376,6 @@ where match self { Self::Static(a) => a.eq(other), Self::Rc(a) => a.eq(*other), - Self::Single(a) => a.eq(*other), } } } @@ -464,8 +429,6 @@ mod test_array { fn from_iter_is_optimized() { let array_0 = [].into_iter().collect::>(); assert!(matches!(array_0, IArray::Static(_))); - let array_1 = [1].into_iter().collect::>(); - assert!(matches!(array_1, IArray::Single(_))); let array_2 = [1, 2].into_iter().collect::>(); assert!(matches!(array_2, IArray::Rc(_))); { @@ -478,7 +441,7 @@ mod test_array { let it = [2].into_iter().filter(|x| x % 2 == 0); assert_eq!(it.size_hint(), (0, Some(1))); let array_0_to_1 = it.collect::>(); - assert!(matches!(array_0_to_1, IArray::Single(_))); + assert!(matches!(array_0_to_1, IArray::Rc(_))); } } @@ -514,4 +477,14 @@ mod test_array { let _array: IArray = IArray::from(Rc::from(vec![1, 2, 3])); let _array: IArray = IArray::from([1]); } + + #[test] + fn recursion() { + #[derive(Clone)] + struct _Node { + _children: IArray<_Node>, + } + + impl ImplicitClone for _Node {} + } }