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-needs-to-bakeStatus: The implementation is "complete" but it needs time to bake.Status: The implementation is "complete" but it needs time to bake.T-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(clone_from_ref)]
This is a tracking issue for Box::clone_from_ref, Arc::clone_from_ref, and Rc::clone_from_ref. try_* and *_in functions are under this tracking issue as well as allocator_api.
Allows creating a Box<T>/Rc<T>/Arc<T> by cloning from a &T where T: CloneToUninit. Notably, these work even when T is unsized (e.g. a slice).
Public API
// alloc::boxed
impl<T: ?Sized + CloneToUninit> Box<T> {
pub fn clone_from_ref(val: &T) -> Box<T>;
// also under feature(allocator_api)
pub fn try_clone_from_ref(val: &T) -> Result<Box<T>, AllocError>;
}
impl<T: ?Sized + CloneToUninit, A: Allocator> Box<T, A> {
// also under feature(allocator_api)
pub fn clone_from_ref_in(val: &T, alloc: A) -> Box<T, A>;
// also under feature(allocator_api)
pub fn try_clone_from_ref_in(val: &T, alloc: A) -> Result<Box<T, A>, AllocError>;
}
// alloc::rc
impl<T: ?Sized + CloneToUninit> Rc<T> {
pub fn clone_from_ref(val: &T) -> Rc<T>;
// also under feature(allocator_api)
pub fn try_clone_from_ref(val: &T) -> Result<Rc<T>, AllocError>;
}
impl<T: ?Sized + CloneToUninit, A: Allocator> Rc<T, A> {
// also under feature(allocator_api)
pub fn clone_from_ref_in(val: &T, alloc: A) -> Rc<T, A>;
// also under feature(allocator_api)
pub fn try_clone_from_ref_in(val: &T, alloc: A) -> Result<Rc<T, A>, AllocError>;
}
// alloc::sync
impl<T: ?Sized + CloneToUninit> Arc<T> {
pub fn clone_from_ref(val: &T) -> Arc<T>;
// also under feature(allocator_api)
pub fn try_clone_from_ref(val: &T) -> Result<Arc<T>, AllocError>;
}
impl<T: ?Sized + CloneToUninit, A: Allocator> Arc<T, A> {
// also under feature(allocator_api)
pub fn clone_from_ref_in(val: &T, alloc: A) -> Arc<T, A>;
// also under feature(allocator_api)
pub fn try_clone_from_ref_in(val: &T, alloc: A) -> Result<Arc<T, A>, AllocError>;
}Steps / History
(Remember to update the S-tracking-* label when checking boxes.)
- ACP:
Box::new_from_reffor making aBox<T>from a&TwhereT: CloneToUninit + ?Sized(andRcandArc) libs-team#483 - Implementation: Add
Box::clone_from_refand similar underfeature(clone_from_ref)#149079 - Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- None yet.
Footnotes
Reacted by Moony, Enduriel, papill9 and Alan Wu
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-needs-to-bakeStatus: The implementation is "complete" but it needs time to bake.Status: The implementation is "complete" but it needs time to bake.T-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
camsteffen commentedon Nov 21, 2025
Naming thoughts...
It could be called just
clone_fromfor consistency withClone::clone{_from}. Cloning is always from a ref, so the_refseems redundant.Or alternatively
from_clone. This follows the very broad convention ofT::from_*methods. It's a bit odd that it would be just swapping the words fromClone::clone_from, but that method is kinda niche so I don't think that would be too distracting.Rc::clone_from(_ref)looks a lot likeRc::clone, and it doesn't offer any hint that it is not taking anotherRcas an argument.Rc::from_cloneI think has a better tendency to communicate that we're creating from an inner value.Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
Auto merge of rust-lang#149079 - zachs18:clone_from_ref, r=Mark-Simul…
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
Auto merge of rust-lang#149079 - zachs18:clone_from_ref, r=Mark-Simul…
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum
Auto merge of #149079 - zachs18:clone_from_ref, r=Mark-Simulacrum