Lines Matching full:capacity
200 /// [Capacity and Reallocation](#capacity-and-reallocation).
262 /// # Capacity and reallocation
264 /// The capacity of a vector is the amount of space allocated for any future
267 /// within the vector. If a vector's length exceeds its capacity, its capacity
271 /// For example, a vector with capacity 10 and length 0 would be an empty vector
273 /// vector will not change its capacity or cause reallocation to occur. However,
287 /// Most fundamentally, `Vec` is and always will be a (pointer, capacity, length)
293 /// if you construct a `Vec` with capacity 0 via [`Vec::new`], [`vec![]`][`vec!`],
297 /// the `Vec` might not report a [`capacity`] of 0*. `Vec` will allocate if and only
298 /// if <code>[mem::size_of::\<T>]\() * [capacity]\() > 0</code>. In general, `Vec`'s allocation
307 /// you would see if you coerced it to a slice), followed by <code>[capacity] - [len]</code>
310 /// A vector containing the elements `'a'` and `'b'` with capacity 4 can be
312 /// pointer to the head of the allocation in the heap, length and capacity.
316 /// ptr len capacity
348 /// [`push`] and [`insert`] will never (re)allocate if the reported capacity is
350 /// <code>[len] == [capacity]</code>. That is, the reported capacity is completely
362 /// with exactly the requested capacity. If <code>[len] == [capacity]</code>,
374 /// not break, however: using `unsafe` code to write to the excess capacity,
386 /// [capacity]: Vec::capacity
387 /// [`capacity`]: Vec::capacity
427 /// Constructs a new, empty `Vec<T>` with at least the specified capacity.
429 /// The vector will be able to hold at least `capacity` elements without
431 /// `capacity`. If `capacity` is 0, the vector will not allocate.
434 /// minimum *capacity* specified, the vector will have a zero *length*. For
435 /// an explanation of the difference between length and capacity, see
436 /// *[Capacity and reallocation]*.
438 /// If it is important to know the exact allocated capacity of a `Vec`,
439 /// always use the [`capacity`] method after construction.
442 /// and the capacity will always be `usize::MAX`.
444 /// [Capacity and reallocation]: #capacity-and-reallocation
445 /// [`capacity`]: Vec::capacity
449 /// Panics if the new capacity exceeds `isize::MAX` bytes.
456 /// // The vector contains no items, even though it has capacity for more
458 /// assert!(vec.capacity() >= 10);
465 /// assert!(vec.capacity() >= 10);
470 /// assert!(vec.capacity() >= 11);
475 /// assert_eq!(vec_units.capacity(), usize::MAX);
481 pub fn with_capacity(capacity: usize) -> Self { in with_capacity()
482 Self::with_capacity_in(capacity, Global) in with_capacity()
485 /// Tries to construct a new, empty `Vec<T>` with at least the specified capacity.
487 /// The vector will be able to hold at least `capacity` elements without
489 /// `capacity`. If `capacity` is 0, the vector will not allocate.
492 /// minimum *capacity* specified, the vector will have a zero *length*. For
493 /// an explanation of the difference between length and capacity, see
494 /// *[Capacity and reallocation]*.
496 /// If it is important to know the exact allocated capacity of a `Vec`,
497 /// always use the [`capacity`] method after construction.
500 /// and the capacity will always be `usize::MAX`.
502 /// [Capacity and reallocation]: #capacity-and-reallocation
503 /// [`capacity`]: Vec::capacity
510 /// // The vector contains no items, even though it has capacity for more
512 /// assert!(vec.capacity() >= 10);
519 /// assert!(vec.capacity() >= 10);
524 /// assert!(vec.capacity() >= 11);
532 /// assert_eq!(vec_units.capacity(), usize::MAX);
536 pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> { in try_with_capacity()
537 Self::try_with_capacity_in(capacity, Global) in try_with_capacity()
540 /// Creates a `Vec<T>` directly from a pointer, a capacity, and a length.
553 /// * The size of `T` times the `capacity` (ie. the allocated size in bytes) needs
556 /// * `length` needs to be less than or equal to `capacity`.
558 /// * `capacity` needs to be the capacity that the pointer was allocated with.
604 /// let cap = v.capacity();
638 /// assert_eq!(vec.capacity(), 16);
643 pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self { in from_raw_parts()
644 unsafe { Self::from_raw_parts_in(ptr, length, capacity, Global) } in from_raw_parts()
669 /// Constructs a new, empty `Vec<T, A>` with at least the specified capacity
672 /// The vector will be able to hold at least `capacity` elements without
674 /// `capacity`. If `capacity` is 0, the vector will not allocate.
677 /// minimum *capacity* specified, the vector will have a zero *length*. For
678 /// an explanation of the difference between length and capacity, see
679 /// *[Capacity and reallocation]*.
681 /// If it is important to know the exact allocated capacity of a `Vec`,
682 /// always use the [`capacity`] method after construction.
685 /// and the capacity will always be `usize::MAX`.
687 /// [Capacity and reallocation]: #capacity-and-reallocation
688 /// [`capacity`]: Vec::capacity
692 /// Panics if the new capacity exceeds `isize::MAX` bytes.
703 /// // The vector contains no items, even though it has capacity for more
705 /// assert!(vec.capacity() >= 10);
712 /// assert!(vec.capacity() >= 10);
717 /// assert!(vec.capacity() >= 11);
722 /// assert_eq!(vec_units.capacity(), usize::MAX);
727 pub fn with_capacity_in(capacity: usize, alloc: A) -> Self { in with_capacity_in()
728 Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 } in with_capacity_in()
731 /// Tries to construct a new, empty `Vec<T, A>` with at least the specified capacity
734 /// The vector will be able to hold at least `capacity` elements without
736 /// `capacity`. If `capacity` is 0, the vector will not allocate.
739 /// minimum *capacity* specified, the vector will have a zero *length*. For
740 /// an explanation of the difference between length and capacity, see
741 /// *[Capacity and reallocation]*.
743 /// If it is important to know the exact allocated capacity of a `Vec`,
744 /// always use the [`capacity`] method after construction.
747 /// and the capacity will always be `usize::MAX`.
749 /// [Capacity and reallocation]: #capacity-and-reallocation
750 /// [`capacity`]: Vec::capacity
761 /// // The vector contains no items, even though it has capacity for more
763 /// assert!(vec.capacity() >= 10);
770 /// assert!(vec.capacity() >= 10);
775 /// assert!(vec.capacity() >= 11);
783 /// assert_eq!(vec_units.capacity(), usize::MAX);
787 pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> { in try_with_capacity_in()
788 Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 }) in try_with_capacity_in()
791 /// Creates a `Vec<T, A>` directly from a pointer, a capacity, a length,
804 /// * The size of `T` times the `capacity` (ie. the allocated size in bytes) needs
807 /// * `length` needs to be less than or equal to `capacity`.
809 /// * `capacity` needs to [*fit*] the layout size that the pointer was allocated with.
859 /// let cap = v.capacity();
896 /// assert_eq!(vec.capacity(), 16);
901 pub unsafe fn from_raw_parts_in(ptr: *mut T, length: usize, capacity: usize, alloc: A) -> Self { in from_raw_parts_in()
902 unsafe { Vec { buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), len: length } } in from_raw_parts_in()
908 /// the vector (in elements), and the allocated capacity of the
914 /// this is to convert the raw pointer, length, and capacity back
940 (me.as_mut_ptr(), me.len(), me.capacity()) in into_raw_parts()
946 /// the allocated capacity of the data (in elements), and the allocator. These are the same
951 /// this is to convert the raw pointer, length, and capacity back
985 let capacity = me.capacity(); in into_raw_parts_with_alloc() localVariable
988 (ptr, len, capacity, alloc) in into_raw_parts_with_alloc()
999 /// assert!(vec.capacity() >= 10);
1003 pub fn capacity(&self) -> usize { in capacity() method
1004 self.buf.capacity() in capacity()
1007 /// Reserves capacity for at least `additional` more elements to be inserted
1010 /// capacity will be greater than or equal to `self.len() + additional`.
1011 /// Does nothing if capacity is already sufficient.
1015 /// Panics if the new capacity exceeds `isize::MAX` bytes.
1022 /// assert!(vec.capacity() >= 11);
1030 /// Reserves the minimum capacity for at least `additional` more elements to
1033 /// After calling `reserve_exact`, capacity will be greater than or equal to
1034 /// `self.len() + additional`. Does nothing if the capacity is already
1038 /// requests. Therefore, capacity can not be relied upon to be precisely
1045 /// Panics if the new capacity exceeds `isize::MAX` bytes.
1052 /// assert!(vec.capacity() >= 11);
1060 /// Tries to reserve capacity for at least `additional` more elements to be inserted
1062 /// frequent reallocations. After calling `try_reserve`, capacity will be
1064 /// `Ok(())`. Does nothing if capacity is already sufficient. This method
1069 /// If the capacity overflows, or the allocator reports a failure, then an error
1097 /// Tries to reserve the minimum capacity for at least `additional`
1100 /// allocations. After calling `try_reserve_exact`, capacity will be greater
1102 /// Does nothing if the capacity is already sufficient.
1105 /// requests. Therefore, capacity can not be relied upon to be precisely
1112 /// If the capacity overflows, or the allocator reports a failure, then an error
1140 /// Shrinks the capacity of the vector as much as possible.
1150 /// assert!(vec.capacity() >= 10);
1152 /// assert!(vec.capacity() >= 3);
1157 // The capacity is never less than the length, and there's nothing to do when in shrink_to_fit()
1159 // by only calling it with a greater capacity. in shrink_to_fit()
1160 if self.capacity() > self.len { in shrink_to_fit()
1165 /// Shrinks the capacity of the vector with a lower bound.
1167 /// The capacity will remain at least as large as both the length
1170 /// If the current capacity is less than the lower limit, this is a no-op.
1177 /// assert!(vec.capacity() >= 10);
1179 /// assert!(vec.capacity() >= 4);
1181 /// assert!(vec.capacity() >= 3);
1186 if self.capacity() > min_capacity { in shrink_to()
1193 /// If the vector has excess capacity, its items will be moved into a
1194 /// newly-allocated buffer with exactly the right capacity.
1206 /// Any excess capacity is removed:
1212 /// assert!(vec.capacity() >= 10);
1214 /// assert_eq!(slice.into_vec().capacity(), 3);
1237 /// Note that this method has no effect on the allocated capacity
1415 /// - `new_len` must be less than or equal to [`capacity()`].
1418 /// [`capacity()`]: Vec::capacity
1445 /// // 2. `dict_length` <= the capacity (32_768)
1471 /// // 2. `0 <= capacity` always holds whatever `capacity` is.
1482 debug_assert!(new_len <= self.capacity()); in set_len()
1564 if len == self.buf.capacity() { in insert()
1931 /// Panics if the new capacity exceeds `isize::MAX` bytes.
1946 if self.len == self.buf.capacity() { in push()
1968 if self.len == self.buf.capacity() { in try_push()
1979 /// Appends an element if there is sufficient spare capacity, otherwise an error is returned
1982 /// Unlike [`push`] this method will not reallocate when there's insufficient capacity.
1983 … /// The caller should use [`reserve`] or [`try_reserve`] to ensure that there is enough capacity.
2013 if self.len == self.buf.capacity() { in push_within_capacity()
2056 /// Panics if the new capacity exceeds `isize::MAX` bytes.
2162 /// Note that this method has no effect on the allocated capacity
2226 /// the elements `[0, at)` with its previous capacity unchanged.
2262 Vec::with_capacity_in(self.capacity(), self.allocator().clone()), in split_off()
2325 /// so the leaked allocation may include unused capacity that is not part
2352 /// Returns the remaining spare capacity of the vector as a slice of
2389 self.buf.capacity() - self.len, in spare_capacity_mut()
2395 /// capacity of the vector as a slice of `MaybeUninit<T>`.
2397 /// The returned spare capacity slice can be used to fill the vector with data
2463 // - but the allocation extends out to `self.buf.capacity()` elements, possibly in split_at_spare_mut_with_len()
2467 let spare_len = self.buf.capacity() - self.len; in split_at_spare_mut_with_len()
2794 /// - `self.capacity() - self.len()` must be `>= src.len()`
2977 let cap = me.buf.capacity(); in into_iter()
3043 if len == self.capacity() { in extend_desugared()
3069 if len == self.capacity() { in try_extend_desugared()
3111 // truly exceeds usize::MAX, which would eventually lead to a capacity overflow anyway. in extend_trusted()
3115 panic!("capacity overflow"); in extend_trusted()
3477 /// If `v` has excess capacity, its items will be moved into a
3478 /// newly-allocated buffer with exactly the right capacity.
3486 /// Any excess capacity is removed: