Skip to content

Commit 5f9fc8f

Browse files
borsgitbot
authored and
gitbot
committed
Auto merge of rust-lang#134332 - Zalathar:rollup-oe23hkw, r=Zalathar
Rollup of 7 pull requests Successful merges: - rust-lang#130361 (std::net: Solaris supports `SOCK_CLOEXEC` as well since 11.4.) - rust-lang#133406 (Add value accessor methods to `Mutex` and `RwLock`) - rust-lang#133633 (don't show the full linker args unless `--verbose` is passed) - rust-lang#134285 (Add some convenience helper methods on `hir::Safety`) - rust-lang#134310 (Add clarity to the examples of some `Vec` & `VecDeque` methods) - rust-lang#134313 (Don't make a def id for `impl_trait_in_bindings`) - rust-lang#134315 (A couple of polonius fact generation cleanups) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4ebb522 + aa5bd6e commit 5f9fc8f

File tree

9 files changed

+557
-122
lines changed

9 files changed

+557
-122
lines changed

alloc/src/collections/vec_deque/mod.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
18691869
///
18701870
/// # Panics
18711871
///
1872-
/// Panics if `index` is greater than deque's length
1872+
/// Panics if `index` is strictly greater than deque's length
18731873
///
18741874
/// # Examples
18751875
///
@@ -1884,6 +1884,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
18841884
///
18851885
/// vec_deque.insert(1, 'd');
18861886
/// assert_eq!(vec_deque, &['a', 'd', 'b', 'c']);
1887+
///
1888+
/// vec_deque.insert(4, 'e');
1889+
/// assert_eq!(vec_deque, &['a', 'd', 'b', 'c', 'e']);
18871890
/// ```
18881891
#[stable(feature = "deque_extras_15", since = "1.5.0")]
18891892
#[track_caller]
@@ -1928,13 +1931,13 @@ impl<T, A: Allocator> VecDeque<T, A> {
19281931
/// use std::collections::VecDeque;
19291932
///
19301933
/// let mut buf = VecDeque::new();
1931-
/// buf.push_back(1);
1932-
/// buf.push_back(2);
1933-
/// buf.push_back(3);
1934-
/// assert_eq!(buf, [1, 2, 3]);
1934+
/// buf.push_back('a');
1935+
/// buf.push_back('b');
1936+
/// buf.push_back('c');
1937+
/// assert_eq!(buf, ['a', 'b', 'c']);
19351938
///
1936-
/// assert_eq!(buf.remove(1), Some(2));
1937-
/// assert_eq!(buf, [1, 3]);
1939+
/// assert_eq!(buf.remove(1), Some('b'));
1940+
/// assert_eq!(buf, ['a', 'c']);
19381941
/// ```
19391942
#[stable(feature = "rust1", since = "1.0.0")]
19401943
#[rustc_confusables("delete", "take")]
@@ -1982,10 +1985,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
19821985
/// ```
19831986
/// use std::collections::VecDeque;
19841987
///
1985-
/// let mut buf: VecDeque<_> = [1, 2, 3].into();
1988+
/// let mut buf: VecDeque<_> = ['a', 'b', 'c'].into();
19861989
/// let buf2 = buf.split_off(1);
1987-
/// assert_eq!(buf, [1]);
1988-
/// assert_eq!(buf2, [2, 3]);
1990+
/// assert_eq!(buf, ['a']);
1991+
/// assert_eq!(buf2, ['b', 'c']);
19891992
/// ```
19901993
#[inline]
19911994
#[must_use = "use `.truncate()` if you don't need the other half"]

alloc/src/vec/mod.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1953,11 +1953,11 @@ impl<T, A: Allocator> Vec<T, A> {
19531953
/// # Examples
19541954
///
19551955
/// ```
1956-
/// let mut vec = vec![1, 2, 3];
1957-
/// vec.insert(1, 4);
1958-
/// assert_eq!(vec, [1, 4, 2, 3]);
1959-
/// vec.insert(4, 5);
1960-
/// assert_eq!(vec, [1, 4, 2, 3, 5]);
1956+
/// let mut vec = vec!['a', 'b', 'c'];
1957+
/// vec.insert(1, 'd');
1958+
/// assert_eq!(vec, ['a', 'd', 'b', 'c']);
1959+
/// vec.insert(4, 'e');
1960+
/// assert_eq!(vec, ['a', 'd', 'b', 'c', 'e']);
19611961
/// ```
19621962
///
19631963
/// # Time complexity
@@ -2024,9 +2024,9 @@ impl<T, A: Allocator> Vec<T, A> {
20242024
/// # Examples
20252025
///
20262026
/// ```
2027-
/// let mut v = vec![1, 2, 3];
2028-
/// assert_eq!(v.remove(1), 2);
2029-
/// assert_eq!(v, [1, 3]);
2027+
/// let mut v = vec!['a', 'b', 'c'];
2028+
/// assert_eq!(v.remove(1), 'b');
2029+
/// assert_eq!(v, ['a', 'c']);
20302030
/// ```
20312031
#[stable(feature = "rust1", since = "1.0.0")]
20322032
#[track_caller]
@@ -2715,10 +2715,10 @@ impl<T, A: Allocator> Vec<T, A> {
27152715
/// # Examples
27162716
///
27172717
/// ```
2718-
/// let mut vec = vec![1, 2, 3];
2718+
/// let mut vec = vec!['a', 'b', 'c'];
27192719
/// let vec2 = vec.split_off(1);
2720-
/// assert_eq!(vec, [1]);
2721-
/// assert_eq!(vec2, [2, 3]);
2720+
/// assert_eq!(vec, ['a']);
2721+
/// assert_eq!(vec2, ['b', 'c']);
27222722
/// ```
27232723
#[cfg(not(no_global_oom_handling))]
27242724
#[inline]
@@ -2982,9 +2982,9 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
29822982
/// vec.resize(3, "world");
29832983
/// assert_eq!(vec, ["hello", "world", "world"]);
29842984
///
2985-
/// let mut vec = vec![1, 2, 3, 4];
2986-
/// vec.resize(2, 0);
2987-
/// assert_eq!(vec, [1, 2]);
2985+
/// let mut vec = vec!['a', 'b', 'c', 'd'];
2986+
/// vec.resize(2, '_');
2987+
/// assert_eq!(vec, ['a', 'b']);
29882988
/// ```
29892989
#[cfg(not(no_global_oom_handling))]
29902990
#[stable(feature = "vec_resize", since = "1.5.0")]

core/src/slice/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1883,23 +1883,23 @@ impl<T> [T] {
18831883
/// # Examples
18841884
///
18851885
/// ```
1886-
/// let v = [1, 2, 3, 4, 5, 6];
1886+
/// let v = ['a', 'b', 'c'];
18871887
///
18881888
/// {
18891889
/// let (left, right) = v.split_at(0);
18901890
/// assert_eq!(left, []);
1891-
/// assert_eq!(right, [1, 2, 3, 4, 5, 6]);
1891+
/// assert_eq!(right, ['a', 'b', 'c']);
18921892
/// }
18931893
///
18941894
/// {
18951895
/// let (left, right) = v.split_at(2);
1896-
/// assert_eq!(left, [1, 2]);
1897-
/// assert_eq!(right, [3, 4, 5, 6]);
1896+
/// assert_eq!(left, ['a', 'b']);
1897+
/// assert_eq!(right, ['c']);
18981898
/// }
18991899
///
19001900
/// {
1901-
/// let (left, right) = v.split_at(6);
1902-
/// assert_eq!(left, [1, 2, 3, 4, 5, 6]);
1901+
/// let (left, right) = v.split_at(3);
1902+
/// assert_eq!(left, ['a', 'b', 'c']);
19031903
/// assert_eq!(right, []);
19041904
/// }
19051905
/// ```
@@ -1969,23 +1969,23 @@ impl<T> [T] {
19691969
/// # Examples
19701970
///
19711971
/// ```
1972-
/// let v = [1, 2, 3, 4, 5, 6];
1972+
/// let v = ['a', 'b', 'c'];
19731973
///
19741974
/// unsafe {
19751975
/// let (left, right) = v.split_at_unchecked(0);
19761976
/// assert_eq!(left, []);
1977-
/// assert_eq!(right, [1, 2, 3, 4, 5, 6]);
1977+
/// assert_eq!(right, ['a', 'b', 'c']);
19781978
/// }
19791979
///
19801980
/// unsafe {
19811981
/// let (left, right) = v.split_at_unchecked(2);
1982-
/// assert_eq!(left, [1, 2]);
1983-
/// assert_eq!(right, [3, 4, 5, 6]);
1982+
/// assert_eq!(left, ['a', 'b']);
1983+
/// assert_eq!(right, ['c']);
19841984
/// }
19851985
///
19861986
/// unsafe {
1987-
/// let (left, right) = v.split_at_unchecked(6);
1988-
/// assert_eq!(left, [1, 2, 3, 4, 5, 6]);
1987+
/// let (left, right) = v.split_at_unchecked(3);
1988+
/// assert_eq!(left, ['a', 'b', 'c']);
19891989
/// assert_eq!(right, []);
19901990
/// }
19911991
/// ```

std/src/sync/mutex.rs

+104-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ mod tests;
44
use crate::cell::UnsafeCell;
55
use crate::fmt;
66
use crate::marker::PhantomData;
7-
use crate::mem::ManuallyDrop;
7+
use crate::mem::{self, ManuallyDrop};
88
use crate::ops::{Deref, DerefMut};
99
use crate::ptr::NonNull;
10-
use crate::sync::{LockResult, TryLockError, TryLockResult, poison};
10+
use crate::sync::{LockResult, PoisonError, TryLockError, TryLockResult, poison};
1111
use crate::sys::sync as sys;
1212

1313
/// A mutual exclusion primitive useful for protecting shared data
@@ -273,6 +273,100 @@ impl<T> Mutex<T> {
273273
pub const fn new(t: T) -> Mutex<T> {
274274
Mutex { inner: sys::Mutex::new(), poison: poison::Flag::new(), data: UnsafeCell::new(t) }
275275
}
276+
277+
/// Returns the contained value by cloning it.
278+
///
279+
/// # Errors
280+
///
281+
/// If another user of this mutex panicked while holding the mutex, then
282+
/// this call will return an error instead.
283+
///
284+
/// # Examples
285+
///
286+
/// ```
287+
/// #![feature(lock_value_accessors)]
288+
///
289+
/// use std::sync::Mutex;
290+
///
291+
/// let mut mutex = Mutex::new(7);
292+
///
293+
/// assert_eq!(mutex.get_cloned().unwrap(), 7);
294+
/// ```
295+
#[unstable(feature = "lock_value_accessors", issue = "133407")]
296+
pub fn get_cloned(&self) -> Result<T, PoisonError<()>>
297+
where
298+
T: Clone,
299+
{
300+
match self.lock() {
301+
Ok(guard) => Ok((*guard).clone()),
302+
Err(_) => Err(PoisonError::new(())),
303+
}
304+
}
305+
306+
/// Sets the contained value.
307+
///
308+
/// # Errors
309+
///
310+
/// If another user of this mutex panicked while holding the mutex, then
311+
/// this call will return an error containing the provided `value` instead.
312+
///
313+
/// # Examples
314+
///
315+
/// ```
316+
/// #![feature(lock_value_accessors)]
317+
///
318+
/// use std::sync::Mutex;
319+
///
320+
/// let mut mutex = Mutex::new(7);
321+
///
322+
/// assert_eq!(mutex.get_cloned().unwrap(), 7);
323+
/// mutex.set(11).unwrap();
324+
/// assert_eq!(mutex.get_cloned().unwrap(), 11);
325+
/// ```
326+
#[unstable(feature = "lock_value_accessors", issue = "133407")]
327+
pub fn set(&self, value: T) -> Result<(), PoisonError<T>> {
328+
if mem::needs_drop::<T>() {
329+
// If the contained value has non-trivial destructor, we
330+
// call that destructor after the lock being released.
331+
self.replace(value).map(drop)
332+
} else {
333+
match self.lock() {
334+
Ok(mut guard) => {
335+
*guard = value;
336+
337+
Ok(())
338+
}
339+
Err(_) => Err(PoisonError::new(value)),
340+
}
341+
}
342+
}
343+
344+
/// Replaces the contained value with `value`, and returns the old contained value.
345+
///
346+
/// # Errors
347+
///
348+
/// If another user of this mutex panicked while holding the mutex, then
349+
/// this call will return an error containing the provided `value` instead.
350+
///
351+
/// # Examples
352+
///
353+
/// ```
354+
/// #![feature(lock_value_accessors)]
355+
///
356+
/// use std::sync::Mutex;
357+
///
358+
/// let mut mutex = Mutex::new(7);
359+
///
360+
/// assert_eq!(mutex.replace(11).unwrap(), 7);
361+
/// assert_eq!(mutex.get_cloned().unwrap(), 11);
362+
/// ```
363+
#[unstable(feature = "lock_value_accessors", issue = "133407")]
364+
pub fn replace(&self, value: T) -> LockResult<T> {
365+
match self.lock() {
366+
Ok(mut guard) => Ok(mem::replace(&mut *guard, value)),
367+
Err(_) => Err(PoisonError::new(value)),
368+
}
369+
}
276370
}
277371

278372
impl<T: ?Sized> Mutex<T> {
@@ -290,7 +384,8 @@ impl<T: ?Sized> Mutex<T> {
290384
/// # Errors
291385
///
292386
/// If another user of this mutex panicked while holding the mutex, then
293-
/// this call will return an error once the mutex is acquired.
387+
/// this call will return an error once the mutex is acquired. The acquired
388+
/// mutex guard will be contained in the returned error.
294389
///
295390
/// # Panics
296391
///
@@ -331,7 +426,8 @@ impl<T: ?Sized> Mutex<T> {
331426
///
332427
/// If another user of this mutex panicked while holding the mutex, then
333428
/// this call will return the [`Poisoned`] error if the mutex would
334-
/// otherwise be acquired.
429+
/// otherwise be acquired. An acquired lock guard will be contained
430+
/// in the returned error.
335431
///
336432
/// If the mutex could not be acquired because it is already locked, then
337433
/// this call will return the [`WouldBlock`] error.
@@ -438,7 +534,8 @@ impl<T: ?Sized> Mutex<T> {
438534
/// # Errors
439535
///
440536
/// If another user of this mutex panicked while holding the mutex, then
441-
/// this call will return an error instead.
537+
/// this call will return an error containing the the underlying data
538+
/// instead.
442539
///
443540
/// # Examples
444541
///
@@ -465,7 +562,8 @@ impl<T: ?Sized> Mutex<T> {
465562
/// # Errors
466563
///
467564
/// If another user of this mutex panicked while holding the mutex, then
468-
/// this call will return an error instead.
565+
/// this call will return an error containing a mutable reference to the
566+
/// underlying data instead.
469567
///
470568
/// # Examples
471569
///

0 commit comments

Comments
 (0)