Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

WIP: smart pointer (try_)map #144420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Qelxiros wants to merge 1 commit into rust-lang:master
base: master
Choose a base branch
Loading
from Qelxiros:smart_pointer_try_map
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 78 additions & 2 deletions library/alloc/src/boxed.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ use core::fmt;
use core::future::Future;
use core::hash::{Hash, Hasher};
use core::marker::{Tuple, Unsize};
use core::mem::{self, SizedTypeProperties};
use core::mem::{self, MaybeUninit, SizedTypeProperties};
use core::ops::{
AsyncFn, AsyncFnMut, AsyncFnOnce, CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut,
DerefPure, DispatchFromDyn, LegacyReceiver,
DerefPure, DispatchFromDyn, LegacyReceiver, Residual, Try,
};
use core::pin::{Pin, PinCoerceUnsized};
use core::ptr::{self, NonNull, Unique};
Expand Down Expand Up @@ -389,6 +389,82 @@ impl<T> Box<T> {
pub fn try_new_zeroed() -> Result<Box<mem::MaybeUninit<T>>, AllocError> {
Box::try_new_zeroed_in(Global)
}

/// Maps the value in a box, reusing the allocation if possible.
///
/// `f` is called on the value in the box, and the result is returned, also boxed.
///
/// Note: this is an associated function, which means that you have
/// to call it as `Box::map(b, f)` instead of `b.map(f)`. This
/// is so that there is no conflict with a method on the inner type.
///
/// # Examples
///
/// ```
/// #![feature(smart_pointer_try_map)]
///
/// let b = Box::new(7);
/// let new = Box::map(b, |i| i + 7);
/// assert_eq!(*new, 14);
/// ```
#[unstable(feature = "smart_pointer_try_map", issue = "144419")]
pub fn map<U>(this: Self, f: impl FnOnce(T) -> U) -> Box<U> {
if size_of::<T>() == size_of::<U>() && align_of::<T>() == align_of::<U>() {
let (allocation, value) = unsafe {
let ptr = Box::into_raw(this);
let value = ptr.read();
let allocation = Box::from_raw(ptr.cast::<MaybeUninit<U>>());
(allocation, value)
};

Box::write(allocation, f(value))
} else {
Box::new(f(*this))
}
}

/// Attempts to map the value in a box, reusing the allocation if possible.
///
/// `f` is called on the value in the box, and if the operation succeeds, the result is
/// returned, also boxed.
///
/// Note: this is an associated function, which means that you have
/// to call it as `Box::try_map(b, f)` instead of `b.try_map(f)`. This
/// is so that there is no conflict with a method on the inner type.
///
/// # Examples
///
/// ```
/// #![feature(smart_pointer_try_map)]
///
/// let b = Box::new(7);
/// let new = Box::try_map(b, u32::try_from).unwrap();
/// assert_eq!(*new, 7);
/// ```
#[unstable(feature = "smart_pointer_try_map", issue = "144419")]
pub fn try_map<R>(
this: Self,
f: impl FnOnce(T) -> R,
) -> <R::Residual as Residual<Box<R::Output>>>::TryType
where
R: Try,
R::Residual: Residual<Box<R::Output>>,
{
if size_of::<T>() == size_of::<R::Output>() && align_of::<T>() == align_of::<R::Output>() {
let (allocation, value) = unsafe {
let ptr = Box::into_raw(this);
let value = ptr.read();
let allocation = Box::from_raw(ptr.cast::<MaybeUninit<R::Output>>());
(allocation, value)
};
<R::Residual as Residual<Box<R::Output>>>::TryType::from_output(Box::write(
allocation,
f(value)?,
))
} else {
<R::Residual as Residual<Box<R::Output>>>::TryType::from_output(Box::new(f(*this)?))
}
}
}

impl<T, A: Allocator> Box<T, A> {
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
#![feature(trusted_len)]
#![feature(trusted_random_access)]
#![feature(try_trait_v2)]
#![feature(try_trait_v2_residual)]
#![feature(try_with_capacity)]
#![feature(tuple_trait)]
#![feature(ub_checks)]
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /