Skip to content

Commit 2479837

Browse files
committed
Under cfg(test), restrict impl<A: Allocator> From<String<A>> for Box<str, A> to A = Global.
1 parent 836008e commit 2479837

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

library/alloc/src/string/string.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,6 +2732,10 @@ impl<A: Allocator> From<Box<str, A>> for String<A> {
27322732
}
27332733
}
27342734

2735+
// When compiling in test mode, `Box` is not actually local, so this impl is incoherent
2736+
// since `A` is the "main" type.
2737+
// To work around this, restrict this impl to `A = Global` under cfg(test).
2738+
#[cfg(not(test))]
27352739
#[cfg(not(no_global_oom_handling))]
27362740
#[stable(feature = "box_from_str", since = "1.20.0")]
27372741
impl<A: Allocator> From<String<A>> for Box<str, A> {
@@ -2751,6 +2755,27 @@ impl<A: Allocator> From<String<A>> for Box<str, A> {
27512755
}
27522756
}
27532757

2758+
// See above `impl<A: Allocator> From<String<A>> for Box<str, A>`
2759+
#[cfg(test)]
2760+
#[cfg(not(no_global_oom_handling))]
2761+
#[stable(feature = "box_from_str", since = "1.20.0")]
2762+
impl From<String> for Box<str> {
2763+
/// Converts the given [`String`] to a boxed `str` slice that is owned.
2764+
///
2765+
/// # Examples
2766+
///
2767+
/// ```
2768+
/// let s1: String = String::from("hello world");
2769+
/// let s2: Box<str> = Box::from(s1);
2770+
/// let s3: String = String::from(s2);
2771+
///
2772+
/// assert_eq!("hello world", s3)
2773+
/// ```
2774+
fn from(s: String) -> Box<str> {
2775+
s.into_boxed_str()
2776+
}
2777+
}
2778+
27542779
#[cfg(not(no_global_oom_handling))]
27552780
#[stable(feature = "string_from_cow_str", since = "1.14.0")]
27562781
impl<'a> From<Cow<'a, str>> for String {

0 commit comments

Comments
 (0)