Skip to content

Commit 804fff1

Browse files
Auto merge of #146013 - Jules-Bertholet:from-wrapper, r=<try>
Add `From` impls for wrapper types
2 parents fe55364 + b38c598 commit 804fff1

File tree

12 files changed

+94
-17
lines changed

12 files changed

+94
-17
lines changed

library/alloc/src/boxed/thin.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,12 @@ impl<T: ?Sized + Error> Error for ThinBox<T> {
430430
self.deref().source()
431431
}
432432
}
433+
434+
#[cfg(not(no_global_oom_handling))]
435+
#[unstable(feature = "thin_box", issue = "92791")]
436+
impl<T> From<T> for ThinBox<T> {
437+
#[inline(always)]
438+
fn from(value: T) -> Self {
439+
Self::new(value)
440+
}
441+
}

library/alloc/src/rc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3816,6 +3816,15 @@ impl<T: ?Sized, A: Allocator> AsMut<T> for UniqueRc<T, A> {
38163816
#[unstable(feature = "unique_rc_arc", issue = "112566")]
38173817
impl<T: ?Sized, A: Allocator> Unpin for UniqueRc<T, A> {}
38183818

3819+
#[cfg(not(no_global_oom_handling))]
3820+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
3821+
impl<T> From<T> for UniqueRc<T> {
3822+
#[inline(always)]
3823+
fn from(value: T) -> Self {
3824+
Self::new(value)
3825+
}
3826+
}
3827+
38193828
#[unstable(feature = "unique_rc_arc", issue = "112566")]
38203829
impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for UniqueRc<T, A> {
38213830
/// Equality for two `UniqueRc`s.

library/alloc/src/sync.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4242,6 +4242,15 @@ impl<T: ?Sized, A: Allocator> AsMut<T> for UniqueArc<T, A> {
42424242
}
42434243
}
42444244

4245+
#[cfg(not(no_global_oom_handling))]
4246+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
4247+
impl<T> From<T> for UniqueArc<T> {
4248+
#[inline(always)]
4249+
fn from(value: T) -> Self {
4250+
Self::new(value)
4251+
}
4252+
}
4253+
42454254
#[unstable(feature = "unique_rc_arc", issue = "112566")]
42464255
impl<T: ?Sized, A: Allocator> Unpin for UniqueArc<T, A> {}
42474256

library/core/src/cell/lazy.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ impl<T: fmt::Debug, F> fmt::Debug for LazyCell<T, F> {
370370
}
371371
}
372372

373+
#[stable(feature = "from_wrapper_impls", since = "CURRENT_RUSTC_VERSION")]
374+
impl<T, F> From<T> for LazyCell<T, F> {
375+
/// Constructs a `LazyCell` that starts already initialized
376+
/// with the provided value.
377+
#[inline]
378+
fn from(value: T) -> Self {
379+
Self { state: UnsafeCell::new(State::Init(value)) }
380+
}
381+
}
382+
373383
#[cold]
374384
#[inline(never)]
375385
const fn panic_poisoned() -> ! {

library/core/src/mem/manually_drop.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,11 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
276276

277277
#[unstable(feature = "deref_pure_trait", issue = "87121")]
278278
unsafe impl<T: ?Sized> DerefPure for ManuallyDrop<T> {}
279+
280+
#[stable(feature = "from_wrapper_impls", since = "CURRENT_RUSTC_VERSION")]
281+
impl<T> From<T> for ManuallyDrop<T> {
282+
#[inline(always)]
283+
fn from(value: T) -> Self {
284+
Self::new(value)
285+
}
286+
}

library/core/src/panic/unwind_safe.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,11 @@ impl<S: AsyncIterator> AsyncIterator for AssertUnwindSafe<S> {
311311
self.0.size_hint()
312312
}
313313
}
314+
315+
#[stable(feature = "from_wrapper_impls", since = "CURRENT_RUSTC_VERSION")]
316+
impl<T> From<T> for AssertUnwindSafe<T> {
317+
#[inline(always)]
318+
fn from(value: T) -> Self {
319+
Self(value)
320+
}
321+
}

library/std/src/backtrace/tests.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ fn generate_fake_frames() -> Vec<BacktraceFrame> {
4444
#[test]
4545
fn test_debug() {
4646
let backtrace = Backtrace {
47-
inner: Inner::Captured(LazyLock::preinit(Capture {
48-
actual_start: 1,
49-
frames: generate_fake_frames(),
50-
})),
47+
inner: Inner::Captured(
48+
(Capture { actual_start: 1, frames: generate_fake_frames() }).into(),
49+
),
5150
};
5251

5352
#[rustfmt::skip]
@@ -66,10 +65,9 @@ fn test_debug() {
6665
#[test]
6766
fn test_frames() {
6867
let backtrace = Backtrace {
69-
inner: Inner::Captured(LazyLock::preinit(Capture {
70-
actual_start: 1,
71-
frames: generate_fake_frames(),
72-
})),
68+
inner: Inner::Captured(
69+
(Capture { actual_start: 1, frames: generate_fake_frames() }).into(),
70+
),
7371
};
7472

7573
let frames = backtrace.frames();

library/std/src/sync/lazy_lock.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,6 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
105105
LazyLock { once: Once::new(), data: UnsafeCell::new(Data { f: ManuallyDrop::new(f) }) }
106106
}
107107

108-
/// Creates a new lazy value that is already initialized.
109-
#[inline]
110-
#[cfg(test)]
111-
pub(crate) fn preinit(value: T) -> LazyLock<T, F> {
112-
let once = Once::new();
113-
once.call_once(|| {});
114-
LazyLock { once, data: UnsafeCell::new(Data { value: ManuallyDrop::new(value) }) }
115-
}
116-
117108
/// Consumes this `LazyLock` returning the stored value.
118109
///
119110
/// Returns `Ok(value)` if `Lazy` is initialized and `Err(f)` otherwise.
@@ -401,6 +392,19 @@ impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F> {
401392
}
402393
}
403394

395+
#[stable(feature = "from_wrapper_impls", since = "CURRENT_RUSTC_VERSION")]
396+
impl<T, F> From<T> for LazyLock<T, F> {
397+
/// Constructs a `LazyLock` that starts already initialized
398+
/// with the provided value.
399+
#[inline]
400+
fn from(value: T) -> Self {
401+
LazyLock {
402+
once: Once::new_complete(),
403+
data: UnsafeCell::new(Data { value: ManuallyDrop::new(value) }),
404+
}
405+
}
406+
}
407+
404408
#[cold]
405409
#[inline(never)]
406410
fn panic_poisoned() -> ! {

library/std/src/sync/poison/once.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ impl Once {
8282
Once { inner: sys::Once::new() }
8383
}
8484

85+
/// Creates a new `Once` value that starts already completed.
86+
#[inline]
87+
#[must_use]
88+
pub(crate) const fn new_complete() -> Once {
89+
Once { inner: sys::Once::new_complete() }
90+
}
91+
8592
/// Performs an initialization routine once and only once. The given closure
8693
/// will be executed if this is the first time `call_once` has been called,
8794
/// and otherwise the routine will *not* be invoked.

library/std/src/sys/sync/once/futex.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ impl Once {
7575
Once { state_and_queued: Futex::new(INCOMPLETE) }
7676
}
7777

78+
#[inline]
79+
pub const fn new_complete() -> Once {
80+
Once { state_and_queued: Futex::new(COMPLETE) }
81+
}
82+
7883
#[inline]
7984
pub fn is_completed(&self) -> bool {
8085
// Use acquire ordering to make all initialization changes visible to the

0 commit comments

Comments
 (0)