Skip to content

Commit 56d02ae

Browse files
committed
Auto merge of #112624 - matthiaskrgr:rollup-db6ta1b, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #98202 (Implement `TryFrom<&OsStr>` for `&str`) - #107619 (Specify behavior of HashSet::insert) - #109814 (Stabilize String::leak) - #111974 (Update runtime guarantee for `select_nth_unstable`) - #112109 (Don't print unsupported split-debuginfo modes with `-Zunstable-options`) - #112506 (Properly check associated consts for infer placeholders) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f21bc1e + e705af9 commit 56d02ae

File tree

9 files changed

+62
-28
lines changed

9 files changed

+62
-28
lines changed

alloc/src/string.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,26 +1853,27 @@ impl String {
18531853
/// Consumes and leaks the `String`, returning a mutable reference to the contents,
18541854
/// `&'a mut str`.
18551855
///
1856-
/// This is mainly useful for data that lives for the remainder of
1857-
/// the program's life. Dropping the returned reference will cause a memory
1858-
/// leak.
1856+
/// The caller has free choice over the returned lifetime, including `'static`. Indeed,
1857+
/// this function is ideally used for data that lives for the remainder of the program's life,
1858+
/// as dropping the returned reference will cause a memory leak.
18591859
///
18601860
/// It does not reallocate or shrink the `String`,
18611861
/// so the leaked allocation may include unused capacity that is not part
1862-
/// of the returned slice.
1862+
/// of the returned slice. If you don't want that, call [`into_boxed_str`],
1863+
/// and then [`Box::leak`].
1864+
///
1865+
/// [`into_boxed_str`]: Self::into_boxed_str
18631866
///
18641867
/// # Examples
18651868
///
18661869
/// Simple usage:
18671870
///
18681871
/// ```
1869-
/// #![feature(string_leak)]
1870-
///
18711872
/// let x = String::from("bucket");
18721873
/// let static_ref: &'static mut str = x.leak();
18731874
/// assert_eq!(static_ref, "bucket");
18741875
/// ```
1875-
#[unstable(feature = "string_leak", issue = "102929")]
1876+
#[stable(feature = "string_leak", since = "CURRENT_RUSTC_VERSION")]
18761877
#[inline]
18771878
pub fn leak<'a>(self) -> &'a mut str {
18781879
let slice = self.vec.leak();

core/src/slice/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,7 +2995,7 @@ impl<T> [T] {
29952995
/// This reordering has the additional property that any value at position `i < index` will be
29962996
/// less than or equal to any value at a position `j > index`. Additionally, this reordering is
29972997
/// unstable (i.e. any number of equal elements may end up at position `index`), in-place
2998-
/// (i.e. does not allocate), and *O*(*n*) on average. The worst-case performance is *O*(*n* log *n*).
2998+
/// (i.e. does not allocate), and runs in *O*(*n*) time.
29992999
/// This function is also known as "kth element" in other libraries.
30003000
///
30013001
/// It returns a triplet of the following from the reordered slice:
@@ -3045,9 +3045,8 @@ impl<T> [T] {
30453045
/// This reordering has the additional property that any value at position `i < index` will be
30463046
/// less than or equal to any value at a position `j > index` using the comparator function.
30473047
/// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
3048-
/// position `index`), in-place (i.e. does not allocate), and *O*(*n*) on average.
3049-
/// The worst-case performance is *O*(*n* log *n*). This function is also known as
3050-
/// "kth element" in other libraries.
3048+
/// position `index`), in-place (i.e. does not allocate), and runs in *O*(*n*) time.
3049+
/// This function is also known as "kth element" in other libraries.
30513050
///
30523051
/// It returns a triplet of the following from
30533052
/// the slice reordered according to the provided comparator function: the subslice prior to
@@ -3101,8 +3100,7 @@ impl<T> [T] {
31013100
/// This reordering has the additional property that any value at position `i < index` will be
31023101
/// less than or equal to any value at a position `j > index` using the key extraction function.
31033102
/// Additionally, this reordering is unstable (i.e. any number of equal elements may end up at
3104-
/// position `index`), in-place (i.e. does not allocate), and *O*(*n*) on average.
3105-
/// The worst-case performance is *O*(*n* log *n*).
3103+
/// position `index`), in-place (i.e. does not allocate), and runs in *O*(*n*) time.
31063104
/// This function is also known as "kth element" in other libraries.
31073105
///
31083106
/// It returns a triplet of the following from

std/src/collections/hash/set.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,9 @@ where
868868
/// Returns whether the value was newly inserted. That is:
869869
///
870870
/// - If the set did not previously contain this value, `true` is returned.
871-
/// - If the set already contained this value, `false` is returned.
871+
/// - If the set already contained this value, `false` is returned,
872+
/// and the set is not modified: original value is not replaced,
873+
/// and the value passed as argument is dropped.
872874
///
873875
/// # Examples
874876
///

std/src/collections/hash/set/tests.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::HashSet;
33

44
use crate::panic::{catch_unwind, AssertUnwindSafe};
55
use crate::sync::atomic::{AtomicU32, Ordering};
6+
use crate::sync::Arc;
67

78
#[test]
89
fn test_zero_capacities() {
@@ -502,3 +503,22 @@ fn const_with_hasher() {
502503
const X: HashSet<(), ()> = HashSet::with_hasher(());
503504
assert_eq!(X.len(), 0);
504505
}
506+
507+
#[test]
508+
fn test_insert_does_not_overwrite_the_value() {
509+
let first_value = Arc::new(17);
510+
let second_value = Arc::new(17);
511+
512+
let mut set = HashSet::new();
513+
let inserted = set.insert(first_value.clone());
514+
assert!(inserted);
515+
516+
let inserted = set.insert(second_value);
517+
assert!(!inserted);
518+
519+
assert!(
520+
Arc::ptr_eq(set.iter().next().unwrap(), &first_value),
521+
"Insert must not overwrite the value, so the contained value pointer \
522+
must be the same as first value pointer we inserted"
523+
);
524+
}

std/src/ffi/os_str.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ impl OsStr {
745745
without modifying the original"]
746746
#[inline]
747747
pub fn to_str(&self) -> Option<&str> {
748-
self.inner.to_str()
748+
self.inner.to_str().ok()
749749
}
750750

751751
/// Converts an `OsStr` to a <code>[Cow]<[str]></code>.
@@ -1165,6 +1165,24 @@ impl<'a> From<Cow<'a, OsStr>> for OsString {
11651165
}
11661166
}
11671167

1168+
#[stable(feature = "str_tryfrom_osstr_impl", since = "CURRENT_RUSTC_VERSION")]
1169+
impl<'a> TryFrom<&'a OsStr> for &'a str {
1170+
type Error = crate::str::Utf8Error;
1171+
1172+
/// Tries to convert an `&OsStr` to a `&str`.
1173+
///
1174+
/// ```
1175+
/// use std::ffi::OsStr;
1176+
///
1177+
/// let os_str = OsStr::new("foo");
1178+
/// let as_str = <&str>::try_from(os_str).unwrap();
1179+
/// assert_eq!(as_str, "foo");
1180+
/// ```
1181+
fn try_from(value: &'a OsStr) -> Result<Self, Self::Error> {
1182+
value.inner.to_str()
1183+
}
1184+
}
1185+
11681186
#[stable(feature = "box_default_extra", since = "1.17.0")]
11691187
impl Default for Box<OsStr> {
11701188
#[inline]

std/src/sys/unix/os_str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ impl Slice {
207207
unsafe { Slice::from_os_str_bytes_unchecked(s.as_bytes()) }
208208
}
209209

210-
pub fn to_str(&self) -> Option<&str> {
211-
str::from_utf8(&self.inner).ok()
210+
pub fn to_str(&self) -> Result<&str, crate::str::Utf8Error> {
211+
str::from_utf8(&self.inner)
212212
}
213213

214214
pub fn to_string_lossy(&self) -> Cow<'_, str> {

std/src/sys/windows/os_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl Slice {
166166
unsafe { mem::transmute(Wtf8::from_str(s)) }
167167
}
168168

169-
pub fn to_str(&self) -> Option<&str> {
169+
pub fn to_str(&self) -> Result<&str, crate::str::Utf8Error> {
170170
self.inner.as_str()
171171
}
172172

std/src/sys_common/wtf8.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -626,13 +626,8 @@ impl Wtf8 {
626626
///
627627
/// This does not copy the data.
628628
#[inline]
629-
pub fn as_str(&self) -> Option<&str> {
630-
// Well-formed WTF-8 is also well-formed UTF-8
631-
// if and only if it contains no surrogate.
632-
match self.next_surrogate(0) {
633-
None => Some(unsafe { str::from_utf8_unchecked(&self.bytes) }),
634-
Some(_) => None,
635-
}
629+
pub fn as_str(&self) -> Result<&str, str::Utf8Error> {
630+
str::from_utf8(&self.bytes)
636631
}
637632

638633
/// Creates an owned `Wtf8Buf` from a borrowed `Wtf8`.

std/src/sys_common/wtf8/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,11 @@ fn wtf8_code_points() {
521521

522522
#[test]
523523
fn wtf8_as_str() {
524-
assert_eq!(Wtf8::from_str("").as_str(), Some(""));
525-
assert_eq!(Wtf8::from_str("aé 💩").as_str(), Some("aé 💩"));
524+
assert_eq!(Wtf8::from_str("").as_str(), Ok(""));
525+
assert_eq!(Wtf8::from_str("aé 💩").as_str(), Ok("aé 💩"));
526526
let mut string = Wtf8Buf::new();
527527
string.push(CodePoint::from_u32(0xD800).unwrap());
528-
assert_eq!(string.as_str(), None);
528+
assert!(string.as_str().is_err());
529529
}
530530

531531
#[test]

0 commit comments

Comments
 (0)