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();
640 /// assert_eq!(vec.capacity(), 16);
645 pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self { in from_raw_parts()
646 unsafe { Self::from_raw_parts_in(ptr, length, capacity, Global) } in from_raw_parts()
671 /// Constructs a new, empty `Vec<T, A>` with at least the specified capacity
674 /// The vector will be able to hold at least `capacity` elements without
676 /// `capacity`. If `capacity` is 0, the vector will not allocate.
679 /// minimum *capacity* specified, the vector will have a zero *length*. For
680 /// an explanation of the difference between length and capacity, see
681 /// *[Capacity and reallocation]*.
683 /// If it is important to know the exact allocated capacity of a `Vec`,
684 /// always use the [`capacity`] method after construction.
687 /// and the capacity will always be `usize::MAX`.
689 /// [Capacity and reallocation]: #capacity-and-reallocation
690 /// [`capacity`]: Vec::capacity
694 /// Panics if the new capacity exceeds `isize::MAX` bytes.
705 /// // The vector contains no items, even though it has capacity for more
707 /// assert!(vec.capacity() >= 10);
714 /// assert!(vec.capacity() >= 10);
719 /// assert!(vec.capacity() >= 11);
724 /// assert_eq!(vec_units.capacity(), usize::MAX);
729 pub fn with_capacity_in(capacity: usize, alloc: A) -> Self { in with_capacity_in()
730 Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 } in with_capacity_in()
733 /// Tries to construct a new, empty `Vec<T, A>` with at least the specified capacity
736 /// The vector will be able to hold at least `capacity` elements without
738 /// `capacity`. If `capacity` is 0, the vector will not allocate.
741 /// minimum *capacity* specified, the vector will have a zero *length*. For
742 /// an explanation of the difference between length and capacity, see
743 /// *[Capacity and reallocation]*.
745 /// If it is important to know the exact allocated capacity of a `Vec`,
746 /// always use the [`capacity`] method after construction.
749 /// and the capacity will always be `usize::MAX`.
751 /// [Capacity and reallocation]: #capacity-and-reallocation
752 /// [`capacity`]: Vec::capacity
763 /// // The vector contains no items, even though it has capacity for more
765 /// assert!(vec.capacity() >= 10);
772 /// assert!(vec.capacity() >= 10);
777 /// assert!(vec.capacity() >= 11);
785 /// assert_eq!(vec_units.capacity(), usize::MAX);
789 pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> { in try_with_capacity_in()
790 Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 }) in try_with_capacity_in()
793 /// Creates a `Vec<T, A>` directly from a pointer, a capacity, a length,
806 /// * The size of `T` times the `capacity` (ie. the allocated size in bytes) needs
809 /// * `length` needs to be less than or equal to `capacity`.
811 /// * `capacity` needs to [*fit*] the layout size that the pointer was allocated with.
861 /// let cap = v.capacity();
895 /// assert_eq!(vec.capacity(), 16);
900 pub unsafe fn from_raw_parts_in(ptr: *mut T, length: usize, capacity: usize, alloc: A) -> Self { in from_raw_parts_in()
901 unsafe { Vec { buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), len: length } } in from_raw_parts_in()
907 /// the vector (in elements), and the allocated capacity of the
913 /// this is to convert the raw pointer, length, and capacity back
939 (me.as_mut_ptr(), me.len(), me.capacity()) in into_raw_parts()
945 /// the allocated capacity of the data (in elements), and the allocator. These are the same
950 /// this is to convert the raw pointer, length, and capacity back
984 let capacity = me.capacity(); in into_raw_parts_with_alloc() localVariable
987 (ptr, len, capacity, alloc) in into_raw_parts_with_alloc()
998 /// assert!(vec.capacity() >= 10);
1002 pub fn capacity(&self) -> usize { in capacity() method
1003 self.buf.capacity() in capacity()
1006 /// Reserves capacity for at least `additional` more elements to be inserted
1009 /// capacity will be greater than or equal to `self.len() + additional`.
1010 /// Does nothing if capacity is already sufficient.
1014 /// Panics if the new capacity exceeds `isize::MAX` bytes.
1021 /// assert!(vec.capacity() >= 11);
1029 /// Reserves the minimum capacity for at least `additional` more elements to
1032 /// After calling `reserve_exact`, capacity will be greater than or equal to
1033 /// `self.len() + additional`. Does nothing if the capacity is already
1037 /// requests. Therefore, capacity can not be relied upon to be precisely
1044 /// Panics if the new capacity exceeds `isize::MAX` bytes.
1051 /// assert!(vec.capacity() >= 11);
1059 /// Tries to reserve capacity for at least `additional` more elements to be inserted
1061 /// frequent reallocations. After calling `try_reserve`, capacity will be
1063 /// `Ok(())`. Does nothing if capacity is already sufficient. This method
1068 /// If the capacity overflows, or the allocator reports a failure, then an error
1096 /// Tries to reserve the minimum capacity for at least `additional`
1099 /// allocations. After calling `try_reserve_exact`, capacity will be greater
1101 /// Does nothing if the capacity is already sufficient.
1104 /// requests. Therefore, capacity can not be relied upon to be precisely
1111 /// If the capacity overflows, or the allocator reports a failure, then an error
1139 /// Shrinks the capacity of the vector as much as possible.
1149 /// assert!(vec.capacity() >= 10);
1151 /// assert!(vec.capacity() >= 3);
1156 // The capacity is never less than the length, and there's nothing to do when in shrink_to_fit()
1158 // by only calling it with a greater capacity. in shrink_to_fit()
1159 if self.capacity() > self.len { in shrink_to_fit()
1164 /// Shrinks the capacity of the vector with a lower bound.
1166 /// The capacity will remain at least as large as both the length
1169 /// If the current capacity is less than the lower limit, this is a no-op.
1176 /// assert!(vec.capacity() >= 10);
1178 /// assert!(vec.capacity() >= 4);
1180 /// assert!(vec.capacity() >= 3);
1185 if self.capacity() > min_capacity { in shrink_to()
1192 /// If the vector has excess capacity, its items will be moved into a
1193 /// newly-allocated buffer with exactly the right capacity.
1205 /// Any excess capacity is removed:
1211 /// assert!(vec.capacity() >= 10);
1213 /// assert_eq!(slice.into_vec().capacity(), 3);
1236 /// Note that this method has no effect on the allocated capacity
1414 /// - `new_len` must be less than or equal to [`capacity()`].
1417 /// [`capacity()`]: Vec::capacity
1444 /// // 2. `dict_length` <= the capacity (32_768)
1470 /// // 2. `0 <= capacity` always holds whatever `capacity` is.
1481 debug_assert!(new_len <= self.capacity()); in set_len()
1563 if len == self.buf.capacity() { in insert()
1930 /// Panics if the new capacity exceeds `isize::MAX` bytes.
1945 if self.len == self.buf.capacity() { in push()
1967 if self.len == self.buf.capacity() { in try_push()
1978 /// Appends an element if there is sufficient spare capacity, otherwise an error is returned
1981 /// Unlike [`push`] this method will not reallocate when there's insufficient capacity.
1982 … /// The caller should use [`reserve`] or [`try_reserve`] to ensure that there is enough capacity.
2012 if self.len == self.buf.capacity() { in push_within_capacity()
2055 /// Panics if the new capacity exceeds `isize::MAX` bytes.
2161 /// Note that this method has no effect on the allocated capacity
2225 /// the elements `[0, at)` with its previous capacity unchanged.
2261 Vec::with_capacity_in(self.capacity(), self.allocator().clone()), in split_off()
2324 /// so the leaked allocation may include unused capacity that is not part
2351 /// Returns the remaining spare capacity of the vector as a slice of
2388 self.buf.capacity() - self.len, in spare_capacity_mut()
2394 /// capacity of the vector as a slice of `MaybeUninit<T>`.
2396 /// The returned spare capacity slice can be used to fill the vector with data
2462 // - but the allocation extends out to `self.buf.capacity()` elements, possibly in split_at_spare_mut_with_len()
2466 let spare_len = self.buf.capacity() - self.len; in split_at_spare_mut_with_len()
2809 /// - `self.capacity() - self.len()` must be `>= src.len()`
2992 let cap = me.buf.capacity(); in into_iter()
3058 if len == self.capacity() { in extend_desugared()
3084 if len == self.capacity() { in try_extend_desugared()
3126 // truly exceeds usize::MAX, which would eventually lead to a capacity overflow anyway. in extend_trusted()
3130 panic!("capacity overflow"); in extend_trusted()
3481 /// If `v` has excess capacity, its items will be moved into a
3482 /// newly-allocated buffer with exactly the right capacity.
3490 /// Any excess capacity is removed: