1
Fork 0

Rollup merge of #89899 - jkugelman:must-use-alloc, r=joshtriplett

Add #[must_use] to remaining alloc functions

I've run out of compelling reasons to group functions together across crates so I'm just going to go module-by-module. This is everything remaining from the `alloc` crate.

I ignored these because they might be used to purposefully leak memory... or other allocator shenanigans? I dunno. I'll add them if y'all tell me to.

```rust
alloc::alloc          unsafe fn alloc(layout: Layout) -> *mut u8;
alloc::alloc          unsafe fn alloc_zeroed(layout: Layout) -> *mut u8;
alloc::sync::Arc<T>   fn into_raw(this: Self) -> *const T;
```

I don't know why clippy ignored these. I added them myself:

```rust
alloc::collections::btree_map::BTreeMap<K, V>   fn range<T: ?Sized, R>(&self, range: R) -> Range<'_, K, V>;
alloc::collections::btree_set::BTreeSet<T>      fn range<K: ?Sized, R>(&self, range: R) -> Range<'_, T>;
```

I added these non-mutating `mut` functions:

```rust
alloc::collections::btree_map::BTreeMap<K, V>     fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<'_, K, V>;
alloc::collections::btree_map::BTreeMap<K, V>     fn iter_mut(&mut self) -> IterMut<'_, K, V>;
alloc::collections::btree_map::BTreeMap<K, V>     fn values_mut(&mut self) -> ValuesMut<'_, K, V>;
alloc::collections::linked_list::LinkedList<T>    fn iter_mut(&mut self) -> IterMut<'_, T>;
alloc::collections::linked_list::LinkedList<T>    fn cursor_front_mut(&mut self) -> CursorMut<'_, T>;
alloc::collections::linked_list::LinkedList<T>    fn cursor_back_mut(&mut self) -> CursorMut<'_, T>;
alloc::collections::linked_list::LinkedList<T>    fn front_mut(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::LinkedList<T>    fn back_mut(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::CursorMut<'a, T> fn current(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::CursorMut<'a, T> fn peek_next(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::CursorMut<'a, T> fn peek_prev(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::CursorMut<'a, T> fn front_mut(&mut self) -> Option<&mut T>;
alloc::collections::linked_list::CursorMut<'a, T> fn back_mut(&mut self) -> Option<&mut T>;
```

I moved a few existing `#[must_use]`s from functions onto the iterator types they return: `IntoIterSorted`, `IntoKeys`, `IntoValues`.

Parent issue: #89692

r? `@joshtriplett`
This commit is contained in:
Matthias Krüger 2021-10-31 00:33:24 +02:00 committed by GitHub
commit 1adb664392
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 78 additions and 23 deletions

View file

@ -290,7 +290,7 @@ where
let mut c = 0; let mut c = 0;
for i in 0..BENCH_RANGE_SIZE { for i in 0..BENCH_RANGE_SIZE {
for j in i + 1..BENCH_RANGE_SIZE { for j in i + 1..BENCH_RANGE_SIZE {
black_box(map.range(f(i, j))); let _ = black_box(map.range(f(i, j)));
c += 1; c += 1;
} }
} }
@ -322,7 +322,7 @@ fn bench_iter(b: &mut Bencher, repeats: i32, size: i32) {
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect(); let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
b.iter(|| { b.iter(|| {
for _ in 0..repeats { for _ in 0..repeats {
black_box(map.iter()); let _ = black_box(map.iter());
} }
}); });
} }

View file

@ -512,6 +512,7 @@ impl<T: Ord> BinaryHeap<T> {
/// let vec = heap.into_sorted_vec(); /// let vec = heap.into_sorted_vec();
/// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7]); /// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7]);
/// ``` /// ```
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "binary_heap_extras_15", since = "1.5.0")] #[stable(feature = "binary_heap_extras_15", since = "1.5.0")]
pub fn into_sorted_vec(mut self) -> Vec<T> { pub fn into_sorted_vec(mut self) -> Vec<T> {
let mut end = self.len(); let mut end = self.len();
@ -850,7 +851,6 @@ impl<T> BinaryHeap<T> {
/// ///
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), vec![5, 4]); /// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), vec![5, 4]);
/// ``` /// ```
#[must_use = "`self` will be dropped if the result is not used"]
#[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")] #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
pub fn into_iter_sorted(self) -> IntoIterSorted<T> { pub fn into_iter_sorted(self) -> IntoIterSorted<T> {
IntoIterSorted { inner: self } IntoIterSorted { inner: self }
@ -877,6 +877,7 @@ impl<T> BinaryHeap<T> {
/// # Time complexity /// # Time complexity
/// ///
/// Cost is *O*(1) in the worst case. /// Cost is *O*(1) in the worst case.
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn peek(&self) -> Option<&T> { pub fn peek(&self) -> Option<&T> {
self.data.get(0) self.data.get(0)
@ -894,6 +895,7 @@ impl<T> BinaryHeap<T> {
/// assert!(heap.capacity() >= 100); /// assert!(heap.capacity() >= 100);
/// heap.push(4); /// heap.push(4);
/// ``` /// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn capacity(&self) -> usize { pub fn capacity(&self) -> usize {
self.data.capacity() self.data.capacity()
@ -1203,6 +1205,7 @@ impl<T> Drop for Hole<'_, T> {
/// documentation for more. /// documentation for more.
/// ///
/// [`iter`]: BinaryHeap::iter /// [`iter`]: BinaryHeap::iter
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, T: 'a> { pub struct Iter<'a, T: 'a> {
iter: slice::Iter<'a, T>, iter: slice::Iter<'a, T>,
@ -1337,6 +1340,7 @@ impl<I> AsIntoIter for IntoIter<I> {
} }
} }
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")] #[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct IntoIterSorted<T> { pub struct IntoIterSorted<T> {

View file

@ -288,6 +288,7 @@ where
/// documentation for more. /// documentation for more.
/// ///
/// [`iter`]: BTreeMap::iter /// [`iter`]: BTreeMap::iter
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, K: 'a, V: 'a> { pub struct Iter<'a, K: 'a, V: 'a> {
range: LazyLeafRange<marker::Immut<'a>, K, V>, range: LazyLeafRange<marker::Immut<'a>, K, V>,
@ -316,6 +317,7 @@ pub struct IterMut<'a, K: 'a, V: 'a> {
_marker: PhantomData<&'a mut (K, V)>, _marker: PhantomData<&'a mut (K, V)>,
} }
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "collection_debug", since = "1.17.0")] #[stable(feature = "collection_debug", since = "1.17.0")]
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> { impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -359,6 +361,7 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
/// documentation for more. /// documentation for more.
/// ///
/// [`keys`]: BTreeMap::keys /// [`keys`]: BTreeMap::keys
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Keys<'a, K: 'a, V: 'a> { pub struct Keys<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V>, inner: Iter<'a, K, V>,
@ -377,6 +380,7 @@ impl<K: fmt::Debug, V> fmt::Debug for Keys<'_, K, V> {
/// documentation for more. /// documentation for more.
/// ///
/// [`values`]: BTreeMap::values /// [`values`]: BTreeMap::values
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Values<'a, K: 'a, V: 'a> { pub struct Values<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V>, inner: Iter<'a, K, V>,
@ -395,6 +399,7 @@ impl<K, V: fmt::Debug> fmt::Debug for Values<'_, K, V> {
/// documentation for more. /// documentation for more.
/// ///
/// [`values_mut`]: BTreeMap::values_mut /// [`values_mut`]: BTreeMap::values_mut
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "map_values_mut", since = "1.10.0")] #[stable(feature = "map_values_mut", since = "1.10.0")]
pub struct ValuesMut<'a, K: 'a, V: 'a> { pub struct ValuesMut<'a, K: 'a, V: 'a> {
inner: IterMut<'a, K, V>, inner: IterMut<'a, K, V>,
@ -413,6 +418,7 @@ impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
/// See its documentation for more. /// See its documentation for more.
/// ///
/// [`into_keys`]: BTreeMap::into_keys /// [`into_keys`]: BTreeMap::into_keys
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "map_into_keys_values", since = "1.54.0")] #[stable(feature = "map_into_keys_values", since = "1.54.0")]
pub struct IntoKeys<K, V> { pub struct IntoKeys<K, V> {
inner: IntoIter<K, V>, inner: IntoIter<K, V>,
@ -431,6 +437,7 @@ impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
/// See its documentation for more. /// See its documentation for more.
/// ///
/// [`into_values`]: BTreeMap::into_values /// [`into_values`]: BTreeMap::into_values
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "map_into_keys_values", since = "1.54.0")] #[stable(feature = "map_into_keys_values", since = "1.54.0")]
pub struct IntoValues<K, V> { pub struct IntoValues<K, V> {
inner: IntoIter<K, V>, inner: IntoIter<K, V>,
@ -449,6 +456,7 @@ impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
/// documentation for more. /// documentation for more.
/// ///
/// [`range`]: BTreeMap::range /// [`range`]: BTreeMap::range
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "btree_range", since = "1.17.0")] #[stable(feature = "btree_range", since = "1.17.0")]
pub struct Range<'a, K: 'a, V: 'a> { pub struct Range<'a, K: 'a, V: 'a> {
inner: LeafRange<marker::Immut<'a>, K, V>, inner: LeafRange<marker::Immut<'a>, K, V>,
@ -467,6 +475,7 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Range<'_, K, V> {
/// documentation for more. /// documentation for more.
/// ///
/// [`range_mut`]: BTreeMap::range_mut /// [`range_mut`]: BTreeMap::range_mut
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "btree_range", since = "1.17.0")] #[stable(feature = "btree_range", since = "1.17.0")]
pub struct RangeMut<'a, K: 'a, V: 'a> { pub struct RangeMut<'a, K: 'a, V: 'a> {
inner: LeafRange<marker::ValMut<'a>, K, V>, inner: LeafRange<marker::ValMut<'a>, K, V>,
@ -1265,7 +1274,6 @@ impl<K, V> BTreeMap<K, V> {
/// assert_eq!(keys, [1, 2]); /// assert_eq!(keys, [1, 2]);
/// ``` /// ```
#[inline] #[inline]
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "map_into_keys_values", since = "1.54.0")] #[stable(feature = "map_into_keys_values", since = "1.54.0")]
pub fn into_keys(self) -> IntoKeys<K, V> { pub fn into_keys(self) -> IntoKeys<K, V> {
IntoKeys { inner: self.into_iter() } IntoKeys { inner: self.into_iter() }
@ -1288,7 +1296,6 @@ impl<K, V> BTreeMap<K, V> {
/// assert_eq!(values, ["hello", "goodbye"]); /// assert_eq!(values, ["hello", "goodbye"]);
/// ``` /// ```
#[inline] #[inline]
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "map_into_keys_values", since = "1.54.0")] #[stable(feature = "map_into_keys_values", since = "1.54.0")]
pub fn into_values(self) -> IntoValues<K, V> { pub fn into_values(self) -> IntoValues<K, V> {
IntoValues { inner: self.into_iter() } IntoValues { inner: self.into_iter() }

View file

@ -347,6 +347,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
/// map.entry("poneyland").or_insert(12); /// map.entry("poneyland").or_insert(12);
/// assert_eq!(map.entry("poneyland").key(), &"poneyland"); /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
/// ``` /// ```
#[must_use]
#[stable(feature = "map_entry_keys", since = "1.10.0")] #[stable(feature = "map_entry_keys", since = "1.10.0")]
pub fn key(&self) -> &K { pub fn key(&self) -> &K {
self.handle.reborrow().into_kv().0 self.handle.reborrow().into_kv().0
@ -391,6 +392,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
/// assert_eq!(o.get(), &12); /// assert_eq!(o.get(), &12);
/// } /// }
/// ``` /// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn get(&self) -> &V { pub fn get(&self) -> &V {
self.handle.reborrow().into_kv().1 self.handle.reborrow().into_kv().1

View file

@ -744,35 +744,35 @@ fn test_range_equal_empty_cases() {
#[should_panic] #[should_panic]
fn test_range_equal_excluded() { fn test_range_equal_excluded() {
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect(); let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();
map.range((Excluded(2), Excluded(2))); let _ = map.range((Excluded(2), Excluded(2)));
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn test_range_backwards_1() { fn test_range_backwards_1() {
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect(); let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();
map.range((Included(3), Included(2))); let _ = map.range((Included(3), Included(2)));
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn test_range_backwards_2() { fn test_range_backwards_2() {
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect(); let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();
map.range((Included(3), Excluded(2))); let _ = map.range((Included(3), Excluded(2)));
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn test_range_backwards_3() { fn test_range_backwards_3() {
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect(); let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();
map.range((Excluded(3), Included(2))); let _ = map.range((Excluded(3), Included(2)));
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn test_range_backwards_4() { fn test_range_backwards_4() {
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect(); let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();
map.range((Excluded(3), Excluded(2))); let _ = map.range((Excluded(3), Excluded(2)));
} }
#[test] #[test]
@ -783,7 +783,7 @@ fn test_range_finding_ill_order_in_map() {
// we cause a different panic than `test_range_backwards_1` does. // we cause a different panic than `test_range_backwards_1` does.
// A more refined `should_panic` would be welcome. // A more refined `should_panic` would be welcome.
if Cyclic3::C < Cyclic3::A { if Cyclic3::C < Cyclic3::A {
map.range(Cyclic3::C..=Cyclic3::A); let _ = map.range(Cyclic3::C..=Cyclic3::A);
} }
} }
@ -824,7 +824,7 @@ fn test_range_finding_ill_order_in_range_ord() {
} }
let map = (0..12).map(|i| (CompositeKey(i, EvilTwin(i)), ())).collect::<BTreeMap<_, _>>(); let map = (0..12).map(|i| (CompositeKey(i, EvilTwin(i)), ())).collect::<BTreeMap<_, _>>();
map.range(EvilTwin(5)..=EvilTwin(7)); let _ = map.range(EvilTwin(5)..=EvilTwin(7));
} }
#[test] #[test]
@ -1239,32 +1239,32 @@ fn test_borrow() {
#[allow(dead_code)] #[allow(dead_code)]
fn get<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) { fn get<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
v.get(t); let _ = v.get(t);
} }
#[allow(dead_code)] #[allow(dead_code)]
fn get_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) { fn get_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: &T) {
v.get_mut(t); let _ = v.get_mut(t);
} }
#[allow(dead_code)] #[allow(dead_code)]
fn get_key_value<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) { fn get_key_value<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
v.get_key_value(t); let _ = v.get_key_value(t);
} }
#[allow(dead_code)] #[allow(dead_code)]
fn contains_key<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) { fn contains_key<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: &T) {
v.contains_key(t); let _ = v.contains_key(t);
} }
#[allow(dead_code)] #[allow(dead_code)]
fn range<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: T) { fn range<T: Ord>(v: &BTreeMap<Box<T>, ()>, t: T) {
v.range(t..); let _ = v.range(t..);
} }
#[allow(dead_code)] #[allow(dead_code)]
fn range_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: T) { fn range_mut<T: Ord>(v: &mut BTreeMap<Box<T>, ()>, t: T) {
v.range_mut(t..); let _ = v.range_mut(t..);
} }
#[allow(dead_code)] #[allow(dead_code)]

View file

@ -92,6 +92,7 @@ impl<T: Clone> Clone for BTreeSet<T> {
/// See its documentation for more. /// See its documentation for more.
/// ///
/// [`iter`]: BTreeSet::iter /// [`iter`]: BTreeSet::iter
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, T: 'a> { pub struct Iter<'a, T: 'a> {
iter: Keys<'a, T, ()>, iter: Keys<'a, T, ()>,
@ -123,6 +124,7 @@ pub struct IntoIter<T> {
/// See its documentation for more. /// See its documentation for more.
/// ///
/// [`range`]: BTreeSet::range /// [`range`]: BTreeSet::range
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Debug)] #[derive(Debug)]
#[stable(feature = "btree_range", since = "1.17.0")] #[stable(feature = "btree_range", since = "1.17.0")]
pub struct Range<'a, T: 'a> { pub struct Range<'a, T: 'a> {
@ -668,6 +670,7 @@ impl<T> BTreeSet<T> {
/// set.insert(2); /// set.insert(2);
/// assert_eq!(set.first(), Some(&1)); /// assert_eq!(set.first(), Some(&1));
/// ``` /// ```
#[must_use]
#[unstable(feature = "map_first_last", issue = "62924")] #[unstable(feature = "map_first_last", issue = "62924")]
pub fn first(&self) -> Option<&T> pub fn first(&self) -> Option<&T>
where where
@ -694,6 +697,7 @@ impl<T> BTreeSet<T> {
/// set.insert(2); /// set.insert(2);
/// assert_eq!(set.last(), Some(&2)); /// assert_eq!(set.last(), Some(&2));
/// ``` /// ```
#[must_use]
#[unstable(feature = "map_first_last", issue = "62924")] #[unstable(feature = "map_first_last", issue = "62924")]
pub fn last(&self) -> Option<&T> pub fn last(&self) -> Option<&T>
where where

View file

@ -613,8 +613,8 @@ fn test_ord_absence() {
set.is_empty(); set.is_empty();
set.len(); set.len();
set.clear(); set.clear();
set.iter(); let _ = set.iter();
set.into_iter(); let _ = set.into_iter();
} }
fn set_debug<K: Debug>(set: BTreeSet<K>) { fn set_debug<K: Debug>(set: BTreeSet<K>) {

View file

@ -64,6 +64,7 @@ struct Node<T> {
/// ///
/// This `struct` is created by [`LinkedList::iter()`]. See its /// This `struct` is created by [`LinkedList::iter()`]. See its
/// documentation for more. /// documentation for more.
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, T: 'a> { pub struct Iter<'a, T: 'a> {
head: Option<NonNull<Node<T>>>, head: Option<NonNull<Node<T>>>,
@ -99,6 +100,7 @@ impl<T> Clone for Iter<'_, T> {
/// ///
/// This `struct` is created by [`LinkedList::iter_mut()`]. See its /// This `struct` is created by [`LinkedList::iter_mut()`]. See its
/// documentation for more. /// documentation for more.
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct IterMut<'a, T: 'a> { pub struct IterMut<'a, T: 'a> {
head: Option<NonNull<Node<T>>>, head: Option<NonNull<Node<T>>>,
@ -529,6 +531,7 @@ impl<T> LinkedList<T> {
/// ///
/// The cursor is pointing to the "ghost" non-element if the list is empty. /// The cursor is pointing to the "ghost" non-element if the list is empty.
#[inline] #[inline]
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn cursor_front(&self) -> Cursor<'_, T> { pub fn cursor_front(&self) -> Cursor<'_, T> {
Cursor { index: 0, current: self.head, list: self } Cursor { index: 0, current: self.head, list: self }
@ -538,6 +541,7 @@ impl<T> LinkedList<T> {
/// ///
/// The cursor is pointing to the "ghost" non-element if the list is empty. /// The cursor is pointing to the "ghost" non-element if the list is empty.
#[inline] #[inline]
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn cursor_front_mut(&mut self) -> CursorMut<'_, T> { pub fn cursor_front_mut(&mut self) -> CursorMut<'_, T> {
CursorMut { index: 0, current: self.head, list: self } CursorMut { index: 0, current: self.head, list: self }
@ -547,6 +551,7 @@ impl<T> LinkedList<T> {
/// ///
/// The cursor is pointing to the "ghost" non-element if the list is empty. /// The cursor is pointing to the "ghost" non-element if the list is empty.
#[inline] #[inline]
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn cursor_back(&self) -> Cursor<'_, T> { pub fn cursor_back(&self) -> Cursor<'_, T> {
Cursor { index: self.len.checked_sub(1).unwrap_or(0), current: self.tail, list: self } Cursor { index: self.len.checked_sub(1).unwrap_or(0), current: self.tail, list: self }
@ -556,6 +561,7 @@ impl<T> LinkedList<T> {
/// ///
/// The cursor is pointing to the "ghost" non-element if the list is empty. /// The cursor is pointing to the "ghost" non-element if the list is empty.
#[inline] #[inline]
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn cursor_back_mut(&mut self) -> CursorMut<'_, T> { pub fn cursor_back_mut(&mut self) -> CursorMut<'_, T> {
CursorMut { index: self.len.checked_sub(1).unwrap_or(0), current: self.tail, list: self } CursorMut { index: self.len.checked_sub(1).unwrap_or(0), current: self.tail, list: self }
@ -678,6 +684,7 @@ impl<T> LinkedList<T> {
/// assert_eq!(dl.front(), Some(&1)); /// assert_eq!(dl.front(), Some(&1));
/// ``` /// ```
#[inline] #[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn front(&self) -> Option<&T> { pub fn front(&self) -> Option<&T> {
unsafe { self.head.as_ref().map(|node| &node.as_ref().element) } unsafe { self.head.as_ref().map(|node| &node.as_ref().element) }
@ -706,6 +713,7 @@ impl<T> LinkedList<T> {
/// assert_eq!(dl.front(), Some(&5)); /// assert_eq!(dl.front(), Some(&5));
/// ``` /// ```
#[inline] #[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn front_mut(&mut self) -> Option<&mut T> { pub fn front_mut(&mut self) -> Option<&mut T> {
unsafe { self.head.as_mut().map(|node| &mut node.as_mut().element) } unsafe { self.head.as_mut().map(|node| &mut node.as_mut().element) }
@ -728,6 +736,7 @@ impl<T> LinkedList<T> {
/// assert_eq!(dl.back(), Some(&1)); /// assert_eq!(dl.back(), Some(&1));
/// ``` /// ```
#[inline] #[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn back(&self) -> Option<&T> { pub fn back(&self) -> Option<&T> {
unsafe { self.tail.as_ref().map(|node| &node.as_ref().element) } unsafe { self.tail.as_ref().map(|node| &node.as_ref().element) }
@ -1178,6 +1187,7 @@ impl<'a, T> Cursor<'a, T> {
/// ///
/// This returns `None` if the cursor is currently pointing to the /// This returns `None` if the cursor is currently pointing to the
/// "ghost" non-element. /// "ghost" non-element.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn index(&self) -> Option<usize> { pub fn index(&self) -> Option<usize> {
let _ = self.current?; let _ = self.current?;
@ -1232,6 +1242,7 @@ impl<'a, T> Cursor<'a, T> {
/// ///
/// This returns `None` if the cursor is currently pointing to the /// This returns `None` if the cursor is currently pointing to the
/// "ghost" non-element. /// "ghost" non-element.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn current(&self) -> Option<&'a T> { pub fn current(&self) -> Option<&'a T> {
unsafe { self.current.map(|current| &(*current.as_ptr()).element) } unsafe { self.current.map(|current| &(*current.as_ptr()).element) }
@ -1242,6 +1253,7 @@ impl<'a, T> Cursor<'a, T> {
/// If the cursor is pointing to the "ghost" non-element then this returns /// If the cursor is pointing to the "ghost" non-element then this returns
/// the first element of the `LinkedList`. If it is pointing to the last /// the first element of the `LinkedList`. If it is pointing to the last
/// element of the `LinkedList` then this returns `None`. /// element of the `LinkedList` then this returns `None`.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn peek_next(&self) -> Option<&'a T> { pub fn peek_next(&self) -> Option<&'a T> {
unsafe { unsafe {
@ -1258,6 +1270,7 @@ impl<'a, T> Cursor<'a, T> {
/// If the cursor is pointing to the "ghost" non-element then this returns /// If the cursor is pointing to the "ghost" non-element then this returns
/// the last element of the `LinkedList`. If it is pointing to the first /// the last element of the `LinkedList`. If it is pointing to the first
/// element of the `LinkedList` then this returns `None`. /// element of the `LinkedList` then this returns `None`.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn peek_prev(&self) -> Option<&'a T> { pub fn peek_prev(&self) -> Option<&'a T> {
unsafe { unsafe {
@ -1271,6 +1284,7 @@ impl<'a, T> Cursor<'a, T> {
/// Provides a reference to the front element of the cursor's parent list, /// Provides a reference to the front element of the cursor's parent list,
/// or None if the list is empty. /// or None if the list is empty.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn front(&self) -> Option<&'a T> { pub fn front(&self) -> Option<&'a T> {
self.list.front() self.list.front()
@ -1278,6 +1292,7 @@ impl<'a, T> Cursor<'a, T> {
/// Provides a reference to the back element of the cursor's parent list, /// Provides a reference to the back element of the cursor's parent list,
/// or None if the list is empty. /// or None if the list is empty.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn back(&self) -> Option<&'a T> { pub fn back(&self) -> Option<&'a T> {
self.list.back() self.list.back()
@ -1289,6 +1304,7 @@ impl<'a, T> CursorMut<'a, T> {
/// ///
/// This returns `None` if the cursor is currently pointing to the /// This returns `None` if the cursor is currently pointing to the
/// "ghost" non-element. /// "ghost" non-element.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn index(&self) -> Option<usize> { pub fn index(&self) -> Option<usize> {
let _ = self.current?; let _ = self.current?;
@ -1343,6 +1359,7 @@ impl<'a, T> CursorMut<'a, T> {
/// ///
/// This returns `None` if the cursor is currently pointing to the /// This returns `None` if the cursor is currently pointing to the
/// "ghost" non-element. /// "ghost" non-element.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn current(&mut self) -> Option<&mut T> { pub fn current(&mut self) -> Option<&mut T> {
unsafe { self.current.map(|current| &mut (*current.as_ptr()).element) } unsafe { self.current.map(|current| &mut (*current.as_ptr()).element) }
@ -1631,6 +1648,7 @@ impl<'a, T> CursorMut<'a, T> {
/// Provides a reference to the front element of the cursor's parent list, /// Provides a reference to the front element of the cursor's parent list,
/// or None if the list is empty. /// or None if the list is empty.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn front(&self) -> Option<&T> { pub fn front(&self) -> Option<&T> {
self.list.front() self.list.front()
@ -1638,6 +1656,7 @@ impl<'a, T> CursorMut<'a, T> {
/// Provides a mutable reference to the front element of the cursor's /// Provides a mutable reference to the front element of the cursor's
/// parent list, or None if the list is empty. /// parent list, or None if the list is empty.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn front_mut(&mut self) -> Option<&mut T> { pub fn front_mut(&mut self) -> Option<&mut T> {
self.list.front_mut() self.list.front_mut()
@ -1645,6 +1664,7 @@ impl<'a, T> CursorMut<'a, T> {
/// Provides a reference to the back element of the cursor's parent list, /// Provides a reference to the back element of the cursor's parent list,
/// or None if the list is empty. /// or None if the list is empty.
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn back(&self) -> Option<&T> { pub fn back(&self) -> Option<&T> {
self.list.back() self.list.back()
@ -1671,6 +1691,7 @@ impl<'a, T> CursorMut<'a, T> {
/// assert_eq!(contents.next(), Some(0)); /// assert_eq!(contents.next(), Some(0));
/// assert_eq!(contents.next(), None); /// assert_eq!(contents.next(), None);
/// ``` /// ```
#[must_use]
#[unstable(feature = "linked_list_cursors", issue = "58533")] #[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn back_mut(&mut self) -> Option<&mut T> { pub fn back_mut(&mut self) -> Option<&mut T> {
self.list.back_mut() self.list.back_mut()

View file

@ -65,6 +65,7 @@ pub struct TryReserveError {
impl TryReserveError { impl TryReserveError {
/// Details about the allocation that caused the error /// Details about the allocation that caused the error
#[inline] #[inline]
#[must_use]
#[unstable( #[unstable(
feature = "try_reserve_kind", feature = "try_reserve_kind",
reason = "Uncertain how much info should be exposed", reason = "Uncertain how much info should be exposed",

View file

@ -572,6 +572,7 @@ use crate::string;
/// [`format_args!`]: core::format_args /// [`format_args!`]: core::format_args
/// [`format!`]: crate::format /// [`format!`]: crate::format
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn format(args: Arguments<'_>) -> string::String { pub fn format(args: Arguments<'_>) -> string::String {
let capacity = args.estimated_capacity(); let capacity = args.estimated_capacity();

View file

@ -2246,6 +2246,7 @@ impl<T: ?Sized> Weak<T> {
/// Gets the number of strong (`Rc`) pointers pointing to this allocation. /// Gets the number of strong (`Rc`) pointers pointing to this allocation.
/// ///
/// If `self` was created using [`Weak::new`], this will return 0. /// If `self` was created using [`Weak::new`], this will return 0.
#[must_use]
#[stable(feature = "weak_counts", since = "1.41.0")] #[stable(feature = "weak_counts", since = "1.41.0")]
pub fn strong_count(&self) -> usize { pub fn strong_count(&self) -> usize {
if let Some(inner) = self.inner() { inner.strong() } else { 0 } if let Some(inner) = self.inner() { inner.strong() } else { 0 }
@ -2254,6 +2255,7 @@ impl<T: ?Sized> Weak<T> {
/// Gets the number of `Weak` pointers pointing to this allocation. /// Gets the number of `Weak` pointers pointing to this allocation.
/// ///
/// If no strong pointers remain, this will return zero. /// If no strong pointers remain, this will return zero.
#[must_use]
#[stable(feature = "weak_counts", since = "1.41.0")] #[stable(feature = "weak_counts", since = "1.41.0")]
pub fn weak_count(&self) -> usize { pub fn weak_count(&self) -> usize {
self.inner() self.inner()
@ -2324,6 +2326,7 @@ impl<T: ?Sized> Weak<T> {
/// assert!(!first.ptr_eq(&third)); /// assert!(!first.ptr_eq(&third));
/// ``` /// ```
#[inline] #[inline]
#[must_use]
#[stable(feature = "weak_ptr_eq", since = "1.39.0")] #[stable(feature = "weak_ptr_eq", since = "1.39.0")]
pub fn ptr_eq(&self, other: &Self) -> bool { pub fn ptr_eq(&self, other: &Self) -> bool {
self.ptr.as_ptr() == other.ptr.as_ptr() self.ptr.as_ptr() == other.ptr.as_ptr()

View file

@ -243,6 +243,7 @@ impl str {
/// assert_eq!(*boxed_bytes, *s.as_bytes()); /// assert_eq!(*boxed_bytes, *s.as_bytes());
/// ``` /// ```
#[stable(feature = "str_box_extras", since = "1.20.0")] #[stable(feature = "str_box_extras", since = "1.20.0")]
#[must_use = "`self` will be dropped if the result is not used"]
#[inline] #[inline]
pub fn into_boxed_bytes(self: Box<str>) -> Box<[u8]> { pub fn into_boxed_bytes(self: Box<str>) -> Box<[u8]> {
self.into() self.into()
@ -484,6 +485,7 @@ impl str {
/// assert_eq!(boxed_str.into_string(), string); /// assert_eq!(boxed_str.into_string(), string);
/// ``` /// ```
#[stable(feature = "box_str", since = "1.4.0")] #[stable(feature = "box_str", since = "1.4.0")]
#[must_use = "`self` will be dropped if the result is not used"]
#[inline] #[inline]
pub fn into_string(self: Box<str>) -> String { pub fn into_string(self: Box<str>) -> String {
let slice = Box::<[u8]>::from(self); let slice = Box::<[u8]>::from(self);
@ -508,9 +510,10 @@ impl str {
/// ///
/// ```should_panic /// ```should_panic
/// // this will panic at runtime /// // this will panic at runtime
/// "0123456789abcdef".repeat(usize::MAX); /// let huge = "0123456789abcdef".repeat(usize::MAX);
/// ``` /// ```
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
#[must_use]
#[stable(feature = "repeat_str", since = "1.16.0")] #[stable(feature = "repeat_str", since = "1.16.0")]
pub fn repeat(&self, n: usize) -> String { pub fn repeat(&self, n: usize) -> String {
unsafe { String::from_utf8_unchecked(self.as_bytes().repeat(n)) } unsafe { String::from_utf8_unchecked(self.as_bytes().repeat(n)) }

View file

@ -898,6 +898,7 @@ impl String {
/// assert!(s.capacity() >= 10); /// assert!(s.capacity() >= 10);
/// ``` /// ```
#[inline] #[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn capacity(&self) -> usize { pub fn capacity(&self) -> usize {
self.vec.capacity() self.vec.capacity()
@ -1823,6 +1824,7 @@ impl FromUtf8Error {
/// // the first byte is invalid here /// // the first byte is invalid here
/// assert_eq!(1, error.valid_up_to()); /// assert_eq!(1, error.valid_up_to());
/// ``` /// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn utf8_error(&self) -> Utf8Error { pub fn utf8_error(&self) -> Utf8Error {
self.error self.error

View file

@ -953,6 +953,7 @@ impl<T: ?Sized> Arc<T> {
/// assert_eq!(1, Arc::weak_count(&five)); /// assert_eq!(1, Arc::weak_count(&five));
/// ``` /// ```
#[inline] #[inline]
#[must_use]
#[stable(feature = "arc_counts", since = "1.15.0")] #[stable(feature = "arc_counts", since = "1.15.0")]
pub fn weak_count(this: &Self) -> usize { pub fn weak_count(this: &Self) -> usize {
let cnt = this.inner().weak.load(SeqCst); let cnt = this.inner().weak.load(SeqCst);
@ -982,6 +983,7 @@ impl<T: ?Sized> Arc<T> {
/// assert_eq!(2, Arc::strong_count(&five)); /// assert_eq!(2, Arc::strong_count(&five));
/// ``` /// ```
#[inline] #[inline]
#[must_use]
#[stable(feature = "arc_counts", since = "1.15.0")] #[stable(feature = "arc_counts", since = "1.15.0")]
pub fn strong_count(this: &Self) -> usize { pub fn strong_count(this: &Self) -> usize {
this.inner().strong.load(SeqCst) this.inner().strong.load(SeqCst)
@ -1079,8 +1081,6 @@ impl<T: ?Sized> Arc<T> {
drop(Weak { ptr: self.ptr }); drop(Weak { ptr: self.ptr });
} }
#[inline]
#[stable(feature = "ptr_eq", since = "1.17.0")]
/// Returns `true` if the two `Arc`s point to the same allocation /// Returns `true` if the two `Arc`s point to the same allocation
/// (in a vein similar to [`ptr::eq`]). /// (in a vein similar to [`ptr::eq`]).
/// ///
@ -1098,6 +1098,9 @@ impl<T: ?Sized> Arc<T> {
/// ``` /// ```
/// ///
/// [`ptr::eq`]: core::ptr::eq "ptr::eq" /// [`ptr::eq`]: core::ptr::eq "ptr::eq"
#[inline]
#[must_use]
#[stable(feature = "ptr_eq", since = "1.17.0")]
pub fn ptr_eq(this: &Self, other: &Self) -> bool { pub fn ptr_eq(this: &Self, other: &Self) -> bool {
this.ptr.as_ptr() == other.ptr.as_ptr() this.ptr.as_ptr() == other.ptr.as_ptr()
} }
@ -1904,6 +1907,7 @@ impl<T: ?Sized> Weak<T> {
/// Gets the number of strong (`Arc`) pointers pointing to this allocation. /// Gets the number of strong (`Arc`) pointers pointing to this allocation.
/// ///
/// If `self` was created using [`Weak::new`], this will return 0. /// If `self` was created using [`Weak::new`], this will return 0.
#[must_use]
#[stable(feature = "weak_counts", since = "1.41.0")] #[stable(feature = "weak_counts", since = "1.41.0")]
pub fn strong_count(&self) -> usize { pub fn strong_count(&self) -> usize {
if let Some(inner) = self.inner() { inner.strong.load(SeqCst) } else { 0 } if let Some(inner) = self.inner() { inner.strong.load(SeqCst) } else { 0 }
@ -1920,6 +1924,7 @@ impl<T: ?Sized> Weak<T> {
/// Due to implementation details, the returned value can be off by 1 in /// Due to implementation details, the returned value can be off by 1 in
/// either direction when other threads are manipulating any `Arc`s or /// either direction when other threads are manipulating any `Arc`s or
/// `Weak`s pointing to the same allocation. /// `Weak`s pointing to the same allocation.
#[must_use]
#[stable(feature = "weak_counts", since = "1.41.0")] #[stable(feature = "weak_counts", since = "1.41.0")]
pub fn weak_count(&self) -> usize { pub fn weak_count(&self) -> usize {
self.inner() self.inner()
@ -1999,6 +2004,7 @@ impl<T: ?Sized> Weak<T> {
/// ///
/// [`ptr::eq`]: core::ptr::eq "ptr::eq" /// [`ptr::eq`]: core::ptr::eq "ptr::eq"
#[inline] #[inline]
#[must_use]
#[stable(feature = "weak_ptr_eq", since = "1.39.0")] #[stable(feature = "weak_ptr_eq", since = "1.39.0")]
pub fn ptr_eq(&self, other: &Self) -> bool { pub fn ptr_eq(&self, other: &Self) -> bool {
self.ptr.as_ptr() == other.ptr.as_ptr() self.ptr.as_ptr() == other.ptr.as_ptr()

View file

@ -60,6 +60,7 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> {
/// Returns a reference to the underlying allocator. /// Returns a reference to the underlying allocator.
#[unstable(feature = "allocator_api", issue = "32838")] #[unstable(feature = "allocator_api", issue = "32838")]
#[must_use]
#[inline] #[inline]
pub fn allocator(&self) -> &A { pub fn allocator(&self) -> &A {
unsafe { self.vec.as_ref().allocator() } unsafe { self.vec.as_ref().allocator() }