Repository navigation
Open
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-blockedStatus: This tracking issue is blocked on another tracking issueStatus: This tracking issue is blocked on another tracking issueT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
Feature gate: #![feature(smart_pointer_try_map)]
This is a tracking issue for map and try_map functions for various smart pointer types, as per rust-lang/libs-team#364
Public API
// Taking T by value
impl Box<T> {
fn map<U>(this: Self, f: impl FnOnce(T) -> U) -> Box<U>;
fn try_map<R>(this: Self, f: impl FnOnce(T) -> R) -> ChangeOutputType<R, Box<R::Output>>
where
R: Try,
R::Residual: Residual<Box<R::Output, A>>;
}
impl UniqueRc<T> {
fn map<U>(this: Self, f: impl FnOnce(T) -> U) -> UniqueRc<U>;
fn try_map<R>(this: Self, f: impl FnOnce(T) -> R) -> ChangeOutputType<R, UniqueRc<R::Output>>
where
R: Try,
R::Residual: Residual<UniqueRc<R::Output, A>>;
}
impl UniqueArc<T> {
fn map<U>(this: Self, f: impl FnOnce(T) -> U) -> UniqueArc<U>;
fn try_map<R>(this: Self, f: impl FnOnce(T) -> R) -> ChangeOutputType<R, UniqueArc<R::Output>>
where
R: Try,
R::Residual: Residual<UniqueArc<R::Output, A>>;
}
// Taking T by reference
impl Rc<T> {
fn map<U>(this: Self, f: impl FnOnce(&T) -> U) -> Rc<U>;
fn try_map<R>(this: Self, f: impl FnOnce(&T) -> R) -> ChangeOutputType<R, Rc<R::Output>>
where
R: Try,
R::Residual: Residual<Rc<R::Output, A>>;
}
impl Arc<T> {
fn map<U>(this: Self, f: impl FnOnce(&T) -> U) -> Arc<<U>;
fn try_map<R>(this: Self, f: impl FnOnce(&T) -> R) -> ChangeOutputType<R, Arc<<R::Output>>
where
R: Try,
R::Residual: Residual<Arc<R::Output, A>>;
}Steps / History
(Remember to update the S-tracking-* label when checking boxes.)
- Implementation: smart pointer (try_)map #144420
- Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
Footnotes
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-blockedStatus: This tracking issue is blocked on another tracking issueStatus: This tracking issue is blocked on another tracking issueT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Activity
tmke8 commentedon Sep 21, 2025
I see in the linked PR that if size or alignment of the input and output type don't match, then it falls back to
Box::new(f(*this)). This sort of makes sense but it kind of defeats the purpose of the function. What about returning a compiler error instead, if size or alignment don't match?Rollup merge of rust-lang#144420 - Qelxiros:smart_pointer_try_map, r=…
Auto merge of #144420 - Qelxiros:smart_pointer_try_map, r=joboet
Auto merge of #144420 - Qelxiros:smart_pointer_try_map, r=joboet
Auto merge of #144420 - Qelxiros:smart_pointer_try_map, r=joboet
Auto merge of #144420 - Qelxiros:smart_pointer_try_map, r=joboet
Auto merge of #144420 - Qelxiros:smart_pointer_try_map, r=joboet
Auto merge of #144420 - Qelxiros:smart_pointer_try_map, r=joboet
orlp commentedon Nov 5, 2025
@tmke8 That makes the function unusable in generic code (e.g.
Box<T>->Box<T::Foo>whereT: HasFoo), big downvote from me.Auto merge of rust-lang#144420 - Qelxiros:smart_pointer_try_map, r=jo…
Auto merge of rust-lang#144420 - Qelxiros:smart_pointer_try_map, r=jo…
tmke8 commentedon Nov 11, 2025
Well, I was imagining that the compiler error would be thrown on monomorphization, in which case your code would work fine, I think. But I have to admit I'm not an expert on where compiler errors can be raised.
EDIT: sorry, "static assert" is a much better term for what I was imagining than "compiler error"
EDIT2: e.g., using something like this: https://docs.rs/static_assertions/latest/static_assertions/macro.assert_eq_align.html
23 remaining items
Rollup merge of rust-lang#160534 - Qelxiros:stabilize-smart-pointer-m…
Rollup merge of rust-lang#160534 - Qelxiros:stabilize-smart-pointer-m…
Rollup merge of rust-lang#160534 - Qelxiros:stabilize-smart-pointer-m…
Rollup merge of rust-lang#160534 - Qelxiros:stabilize-smart-pointer-m…
Rollup merge of rust-lang#160534 - Qelxiros:stabilize-smart-pointer-m…
Rollup merge of rust-lang#160534 - Qelxiros:stabilize-smart-pointer-m…
Unrolled build for #160534 in rollup 162117
Rollup merge of #160534 - Qelxiros:stabilize-smart-pointer-map, r=nia-e
sugar700 commentedon Sep 3, 2026
I find this API to be a bit surprising, I would rather expect
Arc'smapmethod to work more likeMappedMutexGuard.Something like could be pretty useful especially for async code, due to the fact that async code tends to be
Archeavy.camsteffen commentedon Sep 4, 2026
I think the docs could be clearer about the conditions for reusing the allocation. I wasn't sure if it would reuse the allocation if the mapped value is smaller.
nia-e commentedon Sep 4, 2026
I'd avoid guaranteeing it regardless - at best we could make it a grow/shrink call, but promising reuse of the allocation would require us to track the original layout used to make the allocation
orlp commentedon Sep 4, 2026
I think it's harmless to guarantee it when
T's size and alignment match that ofU(which is also the only time we do it currently).nia-e commentedon Sep 5, 2026
That's probably fair. Some part of me is scared that it'll turn out to be weird for some reason I've yet to think of but I'd be okay +1'ing that (will need fcp)