diff --git a/src/vec.rs b/src/vec.rs index 0361d41c56..f7c3efbb66 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -118,6 +118,7 @@ impl VecView { /// /// ``` /// use heapless::{Vec, VecView}; + /// /// let buffer: &VecView = &Vec::::from_slice(&[1, 2, 3, 5, 8]).unwrap(); /// assert_eq!(buffer.as_slice(), &[1, 2, 3, 5, 8]); /// ``` @@ -214,7 +215,7 @@ impl VecView { /// Appends an `item` to the back of the collection /// - /// Returns back the `item` if the vector is full + /// Returns back the `item` if the vector is full. pub fn push(&mut self, item: T) -> Result<(), T> { if self.len < self.capacity() { unsafe { self.push_unchecked(item) } @@ -945,6 +946,7 @@ impl Vec { /// let vec: Vec = Vec::from_slice(&[1, 2, 3, 4]).unwrap(); /// let view: &VecView = &vec; /// ``` + #[inline] pub const fn as_view(&self) -> &VecView { self } @@ -964,6 +966,7 @@ impl Vec { /// let mut vec: Vec = Vec::from_slice(&[1, 2, 3, 4]).unwrap(); /// let view: &mut VecView = &mut vec; /// ``` + #[inline] pub fn as_mut_view(&mut self) -> &mut VecView { self } @@ -979,6 +982,7 @@ impl Vec { /// let buffer: Vec = Vec::from_slice(&[1, 2, 3, 5, 8]).unwrap(); /// assert_eq!(buffer.as_slice(), &[1, 2, 3, 5, 8]); /// ``` + #[inline] pub fn as_slice(&self) -> &[T] { self.as_view().as_slice() } @@ -1022,6 +1026,7 @@ impl Vec { /// buffer_slice[0] = 9; /// assert_eq!(buffer.as_slice(), &[9, 2, 3, 5, 8]); /// ``` + #[inline] pub fn as_mut_slice(&mut self) -> &mut [T] { self.as_mut_view().as_mut_slice() } @@ -1032,6 +1037,7 @@ impl Vec { } /// Clears the vector, removing all values. + #[inline] pub fn clear(&mut self) { self.as_mut_view().clear() } @@ -1041,6 +1047,7 @@ impl Vec { /// # Panic /// /// Panics if the vec cannot hold all elements of the iterator. + #[inline] pub fn extend(&mut self, iter: I) where I: IntoIterator, @@ -1064,6 +1071,7 @@ impl Vec { /// assert_eq!(*vec, [1, 2, 3, 4]); /// ``` #[allow(clippy::result_unit_err)] + #[inline] pub fn extend_from_slice(&mut self, other: &[T]) -> Result<(), ()> where T: Clone, @@ -1072,13 +1080,15 @@ impl Vec { } /// Removes the last element from a vector and returns it, or `None` if it's empty + #[inline] pub fn pop(&mut self) -> Option { self.as_mut_view().pop() } /// Appends an `item` to the back of the collection /// - /// Returns back the `item` if the vector is full + /// Returns back the `item` if the vector is full. + #[inline] pub fn push(&mut self, item: T) -> Result<(), T> { self.as_mut_view().push(item) } @@ -1088,6 +1098,7 @@ impl Vec { /// # Safety /// /// This assumes the vec to have at least one element. + #[inline] pub unsafe fn pop_unchecked(&mut self) -> T { self.as_mut_view().pop_unchecked() } @@ -1097,11 +1108,13 @@ impl Vec { /// # Safety /// /// This assumes the vec is not full. + #[inline] pub unsafe fn push_unchecked(&mut self, item: T) { self.as_mut_view().push_unchecked(item) } /// Shortens the vector, keeping the first `len` elements and dropping the rest. + #[inline] pub fn truncate(&mut self, len: usize) { self.as_mut_view().truncate(len) } @@ -1114,6 +1127,7 @@ impl Vec { /// /// See also [`resize_default`](Self::resize_default). #[allow(clippy::result_unit_err)] + #[inline] pub fn resize(&mut self, new_len: usize, value: T) -> Result<(), ()> where T: Clone, @@ -1129,6 +1143,7 @@ impl Vec { /// /// See also [`resize`](Self::resize). #[allow(clippy::result_unit_err)] + #[inline] pub fn resize_default(&mut self, new_len: usize) -> Result<(), ()> where T: Clone + Default, @@ -1258,6 +1273,7 @@ impl Vec { /// assert_eq!(v.swap_remove(0), "foo"); /// assert_eq!(&*v, ["baz", "qux"]); /// ``` + #[inline] pub fn swap_remove(&mut self, index: usize) -> T { self.as_mut_view().swap_remove(index) } @@ -1289,6 +1305,7 @@ impl Vec { /// assert_eq!(unsafe { v.swap_remove_unchecked(0) }, "foo"); /// assert_eq!(&*v, ["baz", "qux"]); /// ``` + #[inline] pub unsafe fn swap_remove_unchecked(&mut self, index: usize) -> T { self.as_mut_view().swap_remove_unchecked(index) } @@ -1371,6 +1388,7 @@ impl Vec { /// vec.insert(4, 5); /// assert_eq!(vec, [1, 4, 2, 3, 5]); /// ``` + #[inline] pub fn insert(&mut self, index: usize, element: T) -> Result<(), T> { self.as_mut_view().insert(index, element) } @@ -1400,6 +1418,7 @@ impl Vec { /// assert_eq!(v.remove(1), 2); /// assert_eq!(v, [1, 3]); /// ``` + #[inline] pub fn remove(&mut self, index: usize) -> T { self.as_mut_view().remove(index) } @@ -1432,6 +1451,7 @@ impl Vec { /// vec.retain(|_| *iter.next().unwrap()); /// assert_eq!(vec, [2, 3, 5]); /// ``` + #[inline] pub fn retain(&mut self, f: F) where F: FnMut(&T) -> bool, @@ -1461,6 +1481,7 @@ impl Vec { /// }); /// assert_eq!(vec, [2, 3, 4]); /// ``` + #[inline] pub fn retain_mut(&mut self, f: F) where F: FnMut(&mut T) -> bool, @@ -1494,6 +1515,7 @@ impl Vec { /// /// assert_eq!(&v, &[0, 1, 2]); /// ``` + #[inline] pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit] { self.as_mut_view().spare_capacity_mut() } @@ -1520,6 +1542,7 @@ impl fmt::Debug for Vec where T: fmt::Debug, { + #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.as_view().fmt(f) } @@ -1735,337 +1758,185 @@ impl IntoIterator for Vec { } } -impl PartialEq> for Vec -where - A: PartialEq, -{ - fn eq(&self, other: &Vec) -> bool { - self.as_view().eq(other.as_view()) - } -} - -impl PartialEq> for VecView -where - A: PartialEq, -{ - fn eq(&self, other: &Vec) -> bool { - self.eq(other.as_view()) - } -} - -impl PartialEq> for Vec -where - A: PartialEq, -{ - fn eq(&self, other: &VecView) -> bool { - self.as_view().eq(other) - } -} - -impl PartialEq> for VecView -where - A: PartialEq, -{ - fn eq(&self, other: &VecView) -> bool { - <[A]>::eq(self, &**other) - } -} - -// Vec == [B] -impl PartialEq<[B]> for Vec -where - A: PartialEq, -{ - fn eq(&self, other: &[B]) -> bool { - self.as_view().eq(other) - } -} - -// VecView == [B] -impl PartialEq<[B]> for VecView -where - A: PartialEq, -{ - fn eq(&self, other: &[B]) -> bool { - <[A]>::eq(self, other) - } -} - -// [B] == Vec -impl PartialEq> for [B] -where - A: PartialEq, -{ - fn eq(&self, other: &Vec) -> bool { - other.as_view().eq(self) - } -} - -// [B] == VecView -impl PartialEq> for [B] -where - A: PartialEq, -{ - fn eq(&self, other: &VecView) -> bool { - <[A]>::eq(other, self) - } -} - -// Vec == &[B] -impl PartialEq<&[B]> for Vec -where - A: PartialEq, -{ - fn eq(&self, other: &&[B]) -> bool { - self.as_view().eq(other) - } -} - -// VecView == &[B] -impl PartialEq<&[B]> for VecView -where - A: PartialEq, -{ - fn eq(&self, other: &&[B]) -> bool { - <[A]>::eq(self, *other) - } -} - -// &[B] == Vec -impl PartialEq> for &[B] -where - A: PartialEq, -{ - fn eq(&self, other: &Vec) -> bool { - other.as_view().eq(self) - } -} - -// &[B] == VecView -impl PartialEq> for &[B] -where - A: PartialEq, -{ - fn eq(&self, other: &VecView) -> bool { - <[A]>::eq(other, *self) - } -} - -// Vec == &mut [B] -impl PartialEq<&mut [B]> for Vec -where - A: PartialEq, -{ - fn eq(&self, other: &&mut [B]) -> bool { - self.as_view().eq(other) - } -} - -// VecView == &mut [B] -impl PartialEq<&mut [B]> for VecView -where - A: PartialEq, -{ - fn eq(&self, other: &&mut [B]) -> bool { - <[A]>::eq(self, &other[..]) - } -} - -// &mut [B] == Vec -impl PartialEq> for &mut [B] -where - A: PartialEq, -{ - fn eq(&self, other: &Vec) -> bool { - other.as_view().eq(self) - } -} - -// &mut [B] == VecView -impl PartialEq> for &mut [B] -where - A: PartialEq, -{ - fn eq(&self, other: &VecView) -> bool { - <[A]>::eq(other, &self[..]) - } -} +macro_rules! impl_cmp_traits { + ($Ty:ident<$T:ident $(, const $M:ident : usize, const $N:ident : usize)?>) => { + impl PartialEq> for $Ty + where + A: PartialEq, + { + #[inline] + fn eq(&self, other: &Vec) -> bool { + self.as_slice().eq(other.as_slice()) + } + } -// Vec == [B; M] -// Equality does not require equal capacity -impl PartialEq<[B; M]> for Vec -where - A: PartialEq, -{ - fn eq(&self, other: &[B; M]) -> bool { - self.as_view().eq(other) - } -} + impl PartialEq> for $Ty + where + A: PartialEq, + { + #[inline] + fn eq(&self, other: &VecView) -> bool { + self.as_slice().eq(other.as_slice()) + } + } -// VecView == [B; M] -// Equality does not require equal capacity -impl PartialEq<[B; M]> for VecView -where - A: PartialEq, -{ - fn eq(&self, other: &[B; M]) -> bool { - <[A]>::eq(self, other) - } -} + impl PartialEq<$Ty> for [A; M] + where + A: PartialEq, + { + #[inline] + fn eq(&self, other: &$Ty) -> bool { + self.eq(other.as_slice()) + } + } -// [B; M] == Vec -// Equality does not require equal capacity -impl PartialEq> for [B; M] -where - A: PartialEq, -{ - fn eq(&self, other: &Vec) -> bool { - other.as_view().eq(self) - } -} + impl PartialEq<$Ty> for &[A; M] + where + A: PartialEq, + { + #[inline] + fn eq(&self, other: &$Ty) -> bool { + (*self).eq(other) + } + } -// [B; M] == VecView -// Equality does not require equal capacity -impl PartialEq> for [B; M] -where - A: PartialEq, -{ - fn eq(&self, other: &VecView) -> bool { - <[A]>::eq(other, self) - } -} + impl PartialEq<$Ty> for [A] + where + A: PartialEq, + { + #[inline] + fn eq(&self, other: &$Ty) -> bool { + self.eq(other.as_slice()) + } + } -// Vec == &[B; M] -// Equality does not require equal capacity -impl PartialEq<&[B; M]> for Vec -where - A: PartialEq, -{ - fn eq(&self, other: &&[B; M]) -> bool { - self.as_view().eq(other) - } -} + impl PartialEq<$Ty> for &[A] + where + A: PartialEq, + { + #[inline] + fn eq(&self, other: &$Ty) -> bool { + (*self).eq(other) + } + } -// VecView == &[B; M] -// Equality does not require equal capacity -impl PartialEq<&[B; M]> for VecView -where - A: PartialEq, -{ - fn eq(&self, other: &&[B; M]) -> bool { - <[A]>::eq(self, *other) - } -} + impl PartialEq<[B; N]> for $Ty + where + A: PartialEq, + { + #[inline] + fn eq(&self, other: &[B; N]) -> bool { + self.as_slice().eq(other.as_slice()) + } + } -// &[B; M] == Vec -// Equality does not require equal capacity -impl PartialEq> for &[B; M] -where - A: PartialEq, -{ - fn eq(&self, other: &Vec) -> bool { - other.as_view().eq(self) - } -} + impl PartialEq<&[B; N]> for $Ty + where + A: PartialEq, + { + #[inline] + fn eq(&self, other: &&[B; N]) -> bool { + self.as_slice().eq(other.as_slice()) + } + } -// &[B; M] == VecView -// Equality does not require equal capacity -impl PartialEq> for &[B; M] -where - A: PartialEq, -{ - fn eq(&self, other: &VecView) -> bool { - <[A]>::eq(other, *self) - } -} + impl PartialEq<[B]> for $Ty + where + A: PartialEq, + { + #[inline] + fn eq(&self, other: &[B]) -> bool { + self.as_slice().eq(other) + } + } -// Implements Eq if underlying data is Eq -impl Eq for Vec where T: Eq {} -impl Eq for VecView where T: Eq {} + impl PartialEq<&[B]> for $Ty + where + A: PartialEq, + { + #[inline] + fn eq(&self, other: &&[B]) -> bool { + self.as_slice().eq(*other) + } + } -impl PartialOrd> for Vec -where - T: PartialOrd, -{ - fn partial_cmp(&self, other: &Vec) -> Option { - self.as_view().partial_cmp(other.as_view()) - } -} + impl Eq for $Ty where T: Eq {} -impl PartialOrd> for VecView -where - T: PartialOrd, -{ - fn partial_cmp(&self, other: &VecView) -> Option { - PartialOrd::partial_cmp(&**self, &**other) - } -} + impl PartialOrd<$Ty> for $Ty + where + T: PartialOrd + { + #[inline] + fn partial_cmp(&self, other: &$Ty) -> Option { + self.as_slice().partial_cmp(other.as_slice()) + } + } -impl Ord for Vec -where - T: Ord, -{ - #[inline] - fn cmp(&self, other: &Self) -> Ordering { - Ord::cmp(&**self, &**other) + impl Ord for $Ty + where + T: Ord + { + #[inline] + fn cmp(&self, other: &Self) -> Ordering { + self.as_slice().cmp(other.as_slice()) + } + } } } -impl ops::Deref for Vec { - type Target = [T]; +impl_cmp_traits!(VecView); +impl_cmp_traits!(Vec); - fn deref(&self) -> &[T] { - self.as_slice() - } -} +macro_rules! impl_ref_traits { + ($Ty:ident<$T:ident $(, const $N:ident : usize)?>) => { + impl ops::Deref for $Ty { + type Target = [T]; -impl ops::DerefMut for Vec { - fn deref_mut(&mut self) -> &mut [T] { - self.as_mut_slice() - } -} - -impl ops::Deref for VecView { - type Target = [T]; + #[inline] + fn deref(&self) -> &Self::Target { + self.as_slice() + } + } - fn deref(&self) -> &[T] { - self.as_slice() - } -} + impl ops::DerefMut for $Ty { + #[inline] + fn deref_mut(&mut self) -> &mut Self::Target { + self.as_mut_slice() + } + } -impl ops::DerefMut for VecView { - fn deref_mut(&mut self) -> &mut [T] { - self.as_mut_slice() - } -} + impl AsRef<$Ty> for $Ty { + #[inline] + fn as_ref(&self) -> &Self { + self + } + } -impl AsRef> for Vec { - #[inline] - fn as_ref(&self) -> &Self { - self - } -} + impl AsMut<$Ty> for $Ty { + #[inline] + fn as_mut(&mut self) -> &mut Self { + self + } + } -impl AsMut> for Vec { - #[inline] - fn as_mut(&mut self) -> &mut Self { - self - } -} + impl AsRef<[T]> for $Ty { + #[inline] + fn as_ref(&self) -> &[T] { + self + } + } -impl AsRef<[T]> for Vec { - #[inline] - fn as_ref(&self) -> &[T] { - self - } + impl AsMut<[T]> for $Ty { + #[inline] + fn as_mut(&mut self) -> &mut [T] { + self + } + } + }; } -impl AsMut<[T]> for Vec { - #[inline] - fn as_mut(&mut self) -> &mut [T] { - self - } -} +impl_ref_traits!(VecView); +impl_ref_traits!(Vec); impl Clone for Vec where