Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ fn assert_invariants<T: Copy + PartialEq + Debug, const N: usize>(
"`is_empty()` mismatch"
);
assert_eq!(
small_vec.as_slice(),
small_vec.as_ref(),
std_vec.as_slice(),
"`as_slice()` mismatch"
"`as_ref()` mismatch"
);

// Capacity & spilling invariants
Expand Down
6 changes: 3 additions & 3 deletions src/comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ where Item: PartialEq<U>
{
#[inline]
fn eq(&self, other: &SmallVec<U, INLINE, Heap>) -> bool {
let other = other.as_slice();
let other = other.as_ref();
if self.len() != other.len() {
return false;
}
Expand All @@ -63,7 +63,7 @@ where Item: PartialOrd
{
#[inline]
fn partial_cmp(&self, other: &SmallVec<Item, INLINE, Heap>) -> Option<core::cmp::Ordering> {
self.as_slice().partial_cmp(other.as_slice())
self.as_ref().partial_cmp(other.as_ref())
}
}

Expand All @@ -72,6 +72,6 @@ where Item: Ord
{
#[inline]
fn cmp(&self, other: &SmallVec<Item, INLINE, Heap>) -> core::cmp::Ordering {
self.as_slice().cmp(other.as_slice())
self.as_ref().cmp(other.as_ref())
}
}
15 changes: 14 additions & 1 deletion src/iterators/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,23 @@ impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> Drop
}
}

impl<'valid, Item, const INLINE: usize, Heap: Allocator> AsRef<[Item]>
for Drain<'valid, Item, INLINE, Heap>
{
#[inline]
fn as_ref(&self) -> &[Item] {
self.iter.as_slice()
}
}

impl<Item, const INLINE: usize, Heap: Allocator> Drain<'_, Item, INLINE, Heap> {
#[deprecated(
since = "2.0.0-beta.3",
note = "use `<Drain<'_, Item, INLINE, Heap> as AsRef<[Item]>>::as_ref` instead"
)]
#[must_use]
pub fn as_slice(&self) -> &[Item] {
self.iter.as_slice()
self.as_ref()
}

/// The range from `self.vec.length` to `self.tail_start` contains elements
Expand Down
2 changes: 1 addition & 1 deletion src/iterators/extractif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("ExtractIf")
.field(&self.vec.as_slice())
.field(&self.vec.as_ref())
.finish()
}
}
Expand Down
32 changes: 27 additions & 5 deletions src/iterators/intoiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ unsafe impl<Item: Sync, const INLINE: usize, Heap: Allocator + Sync> Sync
{
}

impl<Item, const INLINE: usize, Heap: Allocator> IntoIter<Item, INLINE, Heap> {
impl<Item, const INLINE: usize, Heap: Allocator> AsRef<[Item]> for IntoIter<Item, INLINE, Heap> {
#[inline]
pub const fn as_slice(&self) -> &[Item] {
fn as_ref(&self) -> &[Item] {
let (end, on_heap) = self.end.parts();
// SAFETY: `end` tells which buffer is active, and the members in
// `self.begin..end` are all initialized. So the pointer arithmetic is
Expand All @@ -59,16 +59,38 @@ impl<Item, const INLINE: usize, Heap: Allocator> IntoIter<Item, INLINE, Heap> {
core::slice::from_raw_parts(ptr.add(self.begin), end - self.begin)
}
}
}

impl<Item, const INLINE: usize, Heap: Allocator> AsMut<[Item]> for IntoIter<Item, INLINE, Heap> {
#[inline]
pub const fn as_mut_slice(&mut self) -> &mut [Item] {
fn as_mut(&mut self) -> &mut [Item] {
let (end, on_heap) = self.end.parts();
// SAFETY: see above
unsafe {
let ptr = self.raw.as_mut_ptr(on_heap);
core::slice::from_raw_parts_mut(ptr.add(self.begin), end - self.begin)
}
}
}

impl<Item, const INLINE: usize, Heap: Allocator> IntoIter<Item, INLINE, Heap> {
#[deprecated(
since = "2.0.0-beta.3",
note = "use `<IntoIter<Item, INLINE, Heap> as AsRef<[Item]>>::as_ref` instead"
)]
#[inline]
pub fn as_slice(&self) -> &[Item] {
self.as_ref()
}

#[deprecated(
since = "2.0.0-beta.3",
note = "use `<IntoIter<Item, INLINE, Heap> as AsMut<[Item]>>::as_mut` instead"
)]
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [Item] {
self.as_mut()
}

#[cfg(feature = "specialization")]
pub(crate) fn mark_consumed(&mut self) {
Expand Down Expand Up @@ -165,7 +187,7 @@ impl<Item: Clone, const INLINE: usize, Heap: Allocator + Clone> Clone
allocator: self.allocator.clone()
};

vec.extend(self.as_slice());
vec.extend(self.as_ref());

vec.into_iter()
}
Expand Down Expand Up @@ -194,6 +216,6 @@ impl<Item, const INLINE: usize, Heap: Allocator> IntoIterator for SmallVec<Item,

impl<Item: Debug, const INLINE: usize, Heap: Allocator> Debug for IntoIter<Item, INLINE, Heap> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("IntoIter").field(&self.as_slice()).finish()
f.debug_tuple("IntoIter").field(&self.as_ref()).finish()
}
}
22 changes: 13 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,18 +970,22 @@ impl<Item, const INLINE: usize, Heap: Allocator> SmallVec<Item, INLINE, Heap> {
unsafe { &mut *ptr }
}

#[deprecated(
since = "2.0.0-beta.3",
note = "use `<SmallVec<Item, INLINE, Heap> as AsRef<[Item]>>::as_ref` instead"
)]
#[inline]
pub const fn as_slice(&self) -> &[Item] {
let (length, on_heap) = self.length.parts();
// SAFETY: all the elements in `..length` are initialized
unsafe { core::slice::from_raw_parts(self.raw.as_ptr(on_heap), length) }
pub fn as_slice(&self) -> &[Item] {
self.as_ref()
}

#[deprecated(
since = "2.0.0-beta.3",
note = "use `<SmallVec<Item, INLINE, Heap> as AsMut<[Item]>>::as_mut` instead"
)]
#[inline]
pub const fn as_mut_slice(&mut self) -> &mut [Item] {
let (length, on_heap) = self.length.parts();
// SAFETY: see above
unsafe { core::slice::from_raw_parts_mut(self.raw.as_mut_ptr(on_heap), length) }
pub fn as_mut_slice(&mut self) -> &mut [Item] {
self.as_mut()
}

#[inline]
Expand Down Expand Up @@ -1817,7 +1821,7 @@ impl<'valid, Item, const INLINE: usize, Heap: Allocator> IntoIterator

impl<Item: Hash, const INLINE: usize, Heap: Allocator> Hash for SmallVec<Item, INLINE, Heap> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_slice().hash(state)
self.as_ref().hash(state)
}
}

Expand Down
19 changes: 13 additions & 6 deletions src/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,34 @@ use {
impl<Item, const INLINE: usize, Heap: Allocator> Borrow<[Item]> for SmallVec<Item, INLINE, Heap> {
#[inline]
fn borrow(&self) -> &[Item] {
self.as_slice()
self.as_ref()
}
}

impl<Item, const INLINE: usize, Heap: Allocator> BorrowMut<[Item]>
for SmallVec<Item, INLINE, Heap>
{
#[inline]
fn borrow_mut(&mut self) -> &mut [Item] {
self.as_mut_slice()
self.as_mut()
}
}

impl<Item, const INLINE: usize, Heap: Allocator> AsRef<[Item]> for SmallVec<Item, INLINE, Heap> {
#[inline]
fn as_ref(&self) -> &[Item] {
self.as_slice()
let (length, on_heap) = self.length.parts();
// SAFETY: all the elements in `..length` are initialized
unsafe { core::slice::from_raw_parts(self.raw.as_ptr(on_heap), length) }
}
}

impl<Item, const INLINE: usize, Heap: Allocator> AsMut<[Item]> for SmallVec<Item, INLINE, Heap> {
#[inline]
fn as_mut(&mut self) -> &mut [Item] {
self.as_mut_slice()
let (length, on_heap) = self.length.parts();
// SAFETY: see above
unsafe { core::slice::from_raw_parts_mut(self.raw.as_mut_ptr(on_heap), length) }
}
}

Expand All @@ -48,12 +54,13 @@ impl<Item, const INLINE: usize, Heap: Allocator> Deref for SmallVec<Item, INLINE

#[inline]
fn deref(&self) -> &Self::Target {
self.as_slice()
self.as_ref()
}
}

impl<Item, const INLINE: usize, Heap: Allocator> DerefMut for SmallVec<Item, INLINE, Heap> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
self.as_mut()
}
}
2 changes: 1 addition & 1 deletion src/specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<Item, const INLINE: usize, const M: usize, Heap: Allocator>
SpecExtend<Item, IntoIter<Item, M, Heap>> for SmallVec<Item, INLINE, Heap>
{
fn spec_extend(&mut self, mut iter: IntoIter<Item, M, Heap>) {
let slice = iter.as_slice();
let slice = iter.as_ref();
let length = slice.len();
let old_len = self.len();

Expand Down
22 changes: 22 additions & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ fn exact_size_iterator() {
}

#[test]
#[allow(deprecated)]
fn into_iter_as_slice() {
let vec = SmallVec::<u32, 2>::from(&[1, 2, 3][..]);
let mut iter = vec.clone().into_iter();
Expand All @@ -723,6 +724,27 @@ fn into_iter_as_slice() {
assert_eq!(iter.as_mut_slice(), &[2]);
}

#[test]
fn into_iter_as_ref() {
let vec = SmallVec::<u32, 2>::from(&[1, 2, 3][..]);
let mut iter = vec.clone().into_iter();
assert_eq!(iter.as_ref(), &[1, 2, 3]);
assert_eq!(iter.as_mut(), &[1, 2, 3]);
iter.next();
assert_eq!(iter.as_ref(), &[2, 3]);
assert_eq!(iter.as_mut(), &[2, 3]);
iter.next_back();
assert_eq!(iter.as_ref(), &[2]);
assert_eq!(iter.as_mut(), &[2]);
}

#[test]
fn drain_as_ref() {
let mut vec = SmallVec::<u32, 4>::from(&[1, 2, 3, 4][..]);
let drain = vec.drain(1..3);
assert_eq!(drain.as_ref(), &[2, 3]);
}

#[test]
fn into_iter_clone() {
// Test that the cloned iterator yields identical elements and that it owns
Expand Down
4 changes: 2 additions & 2 deletions tests/zeroize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn zeroize_inline() {

// It is still usable.
v.extend_from_slice(&[1, 2, 3]);
assert_eq!(v.as_slice(), &[1, 2, 3]);
assert_eq!(v.as_ref(), &[1, 2, 3]);
}

#[test]
Expand All @@ -42,7 +42,7 @@ fn zeroize_spilled() {
assert_fully_zeroed(&mut v);

v.extend_from_slice(&[10, 11]);
assert_eq!(v.as_slice(), &[10, 11]);
assert_eq!(v.as_ref(), &[10, 11]);
}

#[test]
Expand Down
Loading