pub type AttrVec = ThinVec<Attribute>;
Expand description
A list of attributes.
Aliased Type§
struct AttrVec { /* private fields */ }
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 8 bytes
Implementations
Source§impl<T> ThinVec<T>where
T: Clone,
impl<T> ThinVec<T>where
T: Clone,
Sourcepub fn resize(&mut self, new_len: usize, value: T)
pub fn resize(&mut self, new_len: usize, value: T)
Resizes the Vec
in-place so that len()
is equal to new_len
.
If new_len
is greater than len()
, the Vec
is extended by the
difference, with each additional slot filled with value
.
If new_len
is less than len()
, the Vec
is simply truncated.
§Examples
let mut vec = thin_vec!["hello"];
vec.resize(3, "world");
assert_eq!(vec, ["hello", "world", "world"]);
let mut vec = thin_vec![1, 2, 3, 4];
vec.resize(2, 0);
assert_eq!(vec, [1, 2]);
Sourcepub fn extend_from_slice(&mut self, other: &[T])
pub fn extend_from_slice(&mut self, other: &[T])
Clones and appends all elements in a slice to the ThinVec
.
Iterates over the slice other
, clones each element, and then appends
it to this ThinVec
. The other
slice is traversed in-order.
Note that this function is same as extend
except that it is
specialized to work with slices instead. If and when Rust gets
specialization this function will likely be deprecated (but still
available).
§Examples
use thin_vec::thin_vec;
let mut vec = thin_vec![1];
vec.extend_from_slice(&[2, 3, 4]);
assert_eq!(vec, [1, 2, 3, 4]);
Source§impl<T> ThinVec<T>
impl<T> ThinVec<T>
Sourcepub fn with_capacity(cap: usize) -> ThinVec<T>
pub fn with_capacity(cap: usize) -> ThinVec<T>
Constructs a new, empty ThinVec<T>
with at least the specified capacity.
The vector will be able to hold at least capacity
elements without
reallocating. This method is allowed to allocate for more elements than
capacity
. If capacity
is 0, the vector will not allocate.
It is important to note that although the returned vector has the minimum capacity specified, the vector will have a zero length.
If it is important to know the exact allocated capacity of a ThinVec
,
always use the capacity
method after construction.
NOTE: unlike Vec
, ThinVec
MUST allocate once to keep track of non-zero
lengths. As such, we cannot provide the same guarantees about ThinVecs
of ZSTs not allocating. However the allocation never needs to be resized
to add more ZSTs, since the underlying array is still length 0.
§Panics
Panics if the new capacity exceeds isize::MAX
bytes.
§Examples
use thin_vec::ThinVec;
let mut vec = ThinVec::with_capacity(10);
// The vector contains no items, even though it has capacity for more
assert_eq!(vec.len(), 0);
assert!(vec.capacity() >= 10);
// These are all done without reallocating...
for i in 0..10 {
vec.push(i);
}
assert_eq!(vec.len(), 10);
assert!(vec.capacity() >= 10);
// ...but this may make the vector reallocate
vec.push(11);
assert_eq!(vec.len(), 11);
assert!(vec.capacity() >= 11);
// A vector of a zero-sized type will always over-allocate, since no
// space is needed to store the actual elements.
let vec_units = ThinVec::<()>::with_capacity(10);
// Only true **without** the gecko-ffi feature!
// assert_eq!(vec_units.capacity(), usize::MAX);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the vector, also referred to as its ‘length’.
§Examples
use thin_vec::thin_vec;
let a = thin_vec![1, 2, 3];
assert_eq!(a.len(), 3);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the vector contains no elements.
§Examples
use thin_vec::ThinVec;
let mut v = ThinVec::new();
assert!(v.is_empty());
v.push(1);
assert!(!v.is_empty());
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the vector can hold without reallocating.
§Examples
use thin_vec::ThinVec;
let vec: ThinVec<i32> = ThinVec::with_capacity(10);
assert_eq!(vec.capacity(), 10);
Sourcepub fn has_capacity(&self) -> bool
pub fn has_capacity(&self) -> bool
Returns true
if the vector has the capacity to hold any element.
Sourcepub unsafe fn set_len(&mut self, len: usize)
pub unsafe fn set_len(&mut self, len: usize)
Forces the length of the vector to new_len
.
This is a low-level operation that maintains none of the normal
invariants of the type. Normally changing the length of a vector
is done using one of the safe operations instead, such as
truncate
, resize
, extend
, or clear
.
§Safety
new_len
must be less than or equal tocapacity()
.- The elements at
old_len..new_len
must be initialized.
§Examples
This method can be useful for situations in which the vector is serving as a buffer for other code, particularly over FFI:
use thin_vec::ThinVec;
pub fn get_dictionary(&self) -> Option<ThinVec<u8>> {
// Per the FFI method's docs, "32768 bytes is always enough".
let mut dict = ThinVec::with_capacity(32_768);
let mut dict_length = 0;
// SAFETY: When `deflateGetDictionary` returns `Z_OK`, it holds that:
// 1. `dict_length` elements were initialized.
// 2. `dict_length` <= the capacity (32_768)
// which makes `set_len` safe to call.
unsafe {
// Make the FFI call...
let r = deflateGetDictionary(self.strm, dict.as_mut_ptr(), &mut dict_length);
if r == Z_OK {
// ...and update the length to what was initialized.
dict.set_len(dict_length);
Some(dict)
} else {
None
}
}
}
While the following example is sound, there is a memory leak since
the inner vectors were not freed prior to the set_len
call:
use thin_vec::thin_vec;
let mut vec = thin_vec![thin_vec![1, 0, 0],
thin_vec![0, 1, 0],
thin_vec![0, 0, 1]];
// SAFETY:
// 1. `old_len..0` is empty so no elements need to be initialized.
// 2. `0 <= capacity` always holds whatever `capacity` is.
unsafe {
vec.set_len(0);
}
Normally, here, one would use clear
instead to correctly drop
the contents and thus not leak memory.
Sourcepub fn insert(&mut self, idx: usize, elem: T)
pub fn insert(&mut self, idx: usize, elem: T)
Inserts an element at position index
within the vector, shifting all
elements after it to the right.
§Panics
Panics if index > len
.
§Examples
use thin_vec::thin_vec;
let mut vec = thin_vec![1, 2, 3];
vec.insert(1, 4);
assert_eq!(vec, [1, 4, 2, 3]);
vec.insert(4, 5);
assert_eq!(vec, [1, 4, 2, 3, 5]);
Sourcepub fn remove(&mut self, idx: usize) -> T
pub fn remove(&mut self, idx: usize) -> T
Removes and returns the element at position index
within the vector,
shifting all elements after it to the left.
Note: Because this shifts over the remaining elements, it has a
worst-case performance of O(n). If you don’t need the order of elements
to be preserved, use swap_remove
instead. If you’d like to remove
elements from the beginning of the ThinVec
, consider using std::collections::VecDeque
.
§Panics
Panics if index
is out of bounds.
§Examples
use thin_vec::thin_vec;
let mut v = thin_vec![1, 2, 3];
assert_eq!(v.remove(1), 2);
assert_eq!(v, [1, 3]);
Sourcepub fn swap_remove(&mut self, idx: usize) -> T
pub fn swap_remove(&mut self, idx: usize) -> T
Removes an element from the vector and returns it.
The removed element is replaced by the last element of the vector.
This does not preserve ordering, but is O(1).
If you need to preserve the element order, use remove
instead.
§Panics
Panics if index
is out of bounds.
§Examples
use thin_vec::thin_vec;
let mut v = thin_vec!["foo", "bar", "baz", "qux"];
assert_eq!(v.swap_remove(1), "bar");
assert_eq!(v, ["foo", "qux", "baz"]);
assert_eq!(v.swap_remove(0), "foo");
assert_eq!(v, ["baz", "qux"]);
Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shortens the vector, keeping the first len
elements and dropping
the rest.
If len
is greater than the vector’s current length, this has no
effect.
The drain
method can emulate truncate
, but causes the excess
elements to be returned instead of dropped.
Note that this method has no effect on the allocated capacity of the vector.
§Examples
Truncating a five element vector to two elements:
use thin_vec::thin_vec;
let mut vec = thin_vec![1, 2, 3, 4, 5];
vec.truncate(2);
assert_eq!(vec, [1, 2]);
No truncation occurs when len
is greater than the vector’s current
length:
use thin_vec::thin_vec;
let mut vec = thin_vec![1, 2, 3];
vec.truncate(8);
assert_eq!(vec, [1, 2, 3]);
Truncating when len == 0
is equivalent to calling the clear
method.
use thin_vec::thin_vec;
let mut vec = thin_vec![1, 2, 3];
vec.truncate(0);
assert_eq!(vec, []);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the vector, removing all values.
Note that this method has no effect on the allocated capacity of the vector.
§Examples
use thin_vec::thin_vec;
let mut v = thin_vec![1, 2, 3];
v.clear();
assert!(v.is_empty());
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Extracts a slice containing the entire vector.
Equivalent to &s[..]
.
§Examples
use thin_vec::thin_vec;
use std::io::{self, Write};
let buffer = thin_vec![1, 2, 3, 5, 8];
io::sink().write(buffer.as_slice()).unwrap();
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Extracts a mutable slice of the entire vector.
Equivalent to &mut s[..]
.
§Examples
use thin_vec::thin_vec;
use std::io::{self, Read};
let mut buffer = vec![0; 3];
io::repeat(0b101).read_exact(buffer.as_mut_slice()).unwrap();
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserve capacity for at least additional
more elements to be inserted.
May reserve more space than requested, to avoid frequent reallocations.
Panics if the new capacity overflows usize
.
Re-allocates only if self.capacity() < self.len() + additional
.
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for additional
more elements to be inserted.
Panics if the new capacity overflows usize
.
Re-allocates only if self.capacity() < self.len() + additional
.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the vector as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the vector that there is space for a few more elements.
§Examples
use thin_vec::ThinVec;
let mut vec = ThinVec::with_capacity(10);
vec.extend([1, 2, 3]);
assert_eq!(vec.capacity(), 10);
vec.shrink_to_fit();
assert!(vec.capacity() >= 3);
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
In other words, remove all elements e
such that f(&e)
returns false
.
This method operates in place and preserves the order of the retained
elements.
§Examples
let mut vec = thin_vec![1, 2, 3, 4];
vec.retain(|&x| x%2 == 0);
assert_eq!(vec, [2, 4]);
Sourcepub fn retain_mut<F>(&mut self, f: F)
pub fn retain_mut<F>(&mut self, f: F)
Retains only the elements specified by the predicate, passing a mutable reference to it.
In other words, remove all elements e
such that f(&mut e)
returns false
.
This method operates in place and preserves the order of the retained
elements.
§Examples
let mut vec = thin_vec![1, 2, 3, 4, 5];
vec.retain_mut(|x| {
*x += 1;
(*x)%2 == 0
});
assert_eq!(vec, [2, 4, 6]);
Sourcepub fn dedup_by_key<F, K>(&mut self, key: F)
pub fn dedup_by_key<F, K>(&mut self, key: F)
Removes consecutive elements in the vector that resolve to the same key.
If the vector is sorted, this removes all duplicates.
§Examples
let mut vec = thin_vec![10, 20, 21, 30, 20];
vec.dedup_by_key(|i| *i / 10);
assert_eq!(vec, [10, 20, 30, 20]);
Sourcepub fn dedup_by<F>(&mut self, same_bucket: F)
pub fn dedup_by<F>(&mut self, same_bucket: F)
Removes consecutive elements in the vector according to a predicate.
The same_bucket
function is passed references to two elements from the vector, and
returns true
if the elements compare equal, or false
if they do not. Only the first
of adjacent equal items is kept.
If the vector is sorted, this removes all duplicates.
§Examples
let mut vec = thin_vec!["foo", "bar", "Bar", "baz", "bar"];
vec.dedup_by(|a, b| a.eq_ignore_ascii_case(b));
assert_eq!(vec, ["foo", "bar", "baz", "bar"]);
Sourcepub fn split_off(&mut self, at: usize) -> ThinVec<T>
pub fn split_off(&mut self, at: usize) -> ThinVec<T>
Splits the collection into two at the given index.
Returns a newly allocated vector containing the elements in the range
[at, len)
. After the call, the original vector will be left containing
the elements [0, at)
with its previous capacity unchanged.
§Panics
Panics if at > len
.
§Examples
use thin_vec::thin_vec;
let mut vec = thin_vec![1, 2, 3];
let vec2 = vec.split_off(1);
assert_eq!(vec, [1]);
assert_eq!(vec2, [2, 3]);
Sourcepub fn append(&mut self, other: &mut ThinVec<T>)
pub fn append(&mut self, other: &mut ThinVec<T>)
Moves all the elements of other
into self
, leaving other
empty.
§Panics
Panics if the new capacity exceeds isize::MAX
bytes.
§Examples
use thin_vec::thin_vec;
let mut vec = thin_vec![1, 2, 3];
let mut vec2 = thin_vec![4, 5, 6];
vec.append(&mut vec2);
assert_eq!(vec, [1, 2, 3, 4, 5, 6]);
assert_eq!(vec2, []);
Sourcepub fn drain<R>(&mut self, range: R) -> Drain<'_, T>where
R: RangeBounds<usize>,
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T>where
R: RangeBounds<usize>,
Removes the specified range from the vector in bulk, returning all removed elements as an iterator. If the iterator is dropped before being fully consumed, it drops the remaining removed elements.
The returned iterator keeps a mutable borrow on the vector to optimize its implementation.
§Panics
Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.
§Leaking
If the returned iterator goes out of scope without being dropped (due to
mem::forget
, for example), the vector may have lost and leaked
elements arbitrarily, including elements outside the range.
§Examples
use thin_vec::{ThinVec, thin_vec};
let mut v = thin_vec![1, 2, 3];
let u: ThinVec<_> = v.drain(1..).collect();
assert_eq!(v, &[1]);
assert_eq!(u, &[2, 3]);
// A full range clears the vector, like `clear()` does
v.drain(..);
assert_eq!(v, &[]);
Sourcepub fn splice<R, I>(
&mut self,
range: R,
replace_with: I,
) -> Splice<'_, <I as IntoIterator>::IntoIter>
pub fn splice<R, I>( &mut self, range: R, replace_with: I, ) -> Splice<'_, <I as IntoIterator>::IntoIter>
Creates a splicing iterator that replaces the specified range in the vector
with the given replace_with
iterator and yields the removed items.
replace_with
does not need to be the same length as range
.
range
is removed even if the iterator is not consumed until the end.
It is unspecified how many elements are removed from the vector
if the Splice
value is leaked.
The input iterator replace_with
is only consumed when the Splice
value is dropped.
This is optimal if:
- The tail (elements in the vector after
range
) is empty, - or
replace_with
yields fewer or equal elements thanrange
’s length - or the lower bound of its
size_hint()
is exact.
Otherwise, a temporary vector is allocated and the tail is moved twice.
§Panics
Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.
§Examples
use thin_vec::{ThinVec, thin_vec};
let mut v = thin_vec![1, 2, 3, 4];
let new = [7, 8, 9];
let u: ThinVec<_> = v.splice(1..3, new).collect();
assert_eq!(v, &[1, 7, 8, 9, 4]);
assert_eq!(u, &[2, 3]);
Trait Implementations
Source§impl<T> BorrowMut<[T]> for ThinVec<T>
impl<T> BorrowMut<[T]> for ThinVec<T>
Source§fn borrow_mut(&mut self) -> &mut [T]
fn borrow_mut(&mut self) -> &mut [T]
Source§impl<T> Extend<T> for ThinVec<T>
impl<T> Extend<T> for ThinVec<T>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T> FlatMapInPlace<T> for ThinVec<T>
impl<T> FlatMapInPlace<T> for ThinVec<T>
fn flat_map_in_place<F, I>(&mut self, f: F)where
F: FnMut(T) -> I,
I: IntoIterator<Item = T>,
Source§impl<T> From<Box<[T]>> for ThinVec<T>
impl<T> From<Box<[T]>> for ThinVec<T>
Source§fn from(s: Box<[T]>) -> ThinVec<T>
fn from(s: Box<[T]>) -> ThinVec<T>
Convert a boxed slice into a vector by transferring ownership of the existing heap allocation.
NOTE: unlike std
, this must reallocate to change the layout!
§Examples
use thin_vec::{ThinVec, thin_vec};
let b: Box<[i32]> = thin_vec![1, 2, 3].into_iter().collect();
assert_eq!(ThinVec::from(b), thin_vec![1, 2, 3]);