Comments (6)
I see a big reason to be cautious about stabilizing
CloneToUninit
: it is a trait both unsafe to implement and unsafe to call, yet it provides something that many slice-like (as opposed todyn
-like) DSTs would want to implement. We wouldn't want to lock in a doublyunsafe
interface as a key interop trait, rather than some fully safe “placement clone” (in the same sense as “placement new”) that can solve this problem.
I thought about what a safer primitive could be that still enables what we need in make_mut
. I think it might (almost?) exists already:
impl<T, A> Rc<T, A>
where
T: ?Sized,
A: Allocator,
Box<T, RcAllocator<A>>: Clone,
{
pub fn make_mut(this: &mut Self) -> &mut T {...}
}
where RcAllocator<A: Allocator>
is an allocator whose every allocation points to the value
part of a MaybeUninit<RcBox<T>>
. When you ask it to allocate with the Layout of some type T
, it delegates to the underlying allocator to allocate the Layout of RcBox<T>
and offsets that pointer.
Lines 286 to 291 in fcae626
Then make_mut
works by turning the pointer held by the Rc<T, A>
into a Box<T, RcAllocator<A>>
, cloning it, and turning the Box<T, RcAllocator<A>>
back into Rc<T, A>
.
Instead of implementing an unsafe trait CloneToUninit
, types plug into make_mut
support by implementing the safe trait Clone
for Box<MyType, A> where A: Allocator
. The concrete allocator type RcAllocator
itself is not public; your impls are written using a generic A
as they already universally are. Notice no unsafe in this impl!
rust/library/alloc/src/boxed.rs
Lines 2218 to 2222 in fcae626
What I don't know is whether Box
's use of Unique
throws a wrench in the works. As I currently understand it, I think it does not. Casting Rc
's NonNull<RcBox<T>>
into Box<T, RcAllocator<A>>
in order to clone it would not be sound, but I also think that's not necessary for the above to work. If we can rearrange Rc
's layout a bit to achieve a guarantee that Rc<T, A>
has the same layout as Box<T, RcAllocator<A>>
, then we'd only be casting &Rc<T, A>
to &Box<T, RcAllocator<A>>
to pass into the clone impl, and as such, Unique
never comes into play. Obviously I can have as many &Box<T>
aliasing one another's allocations as I want. An actual owned Box<T, RcAllocator<A>>
would never exist except for the value that becomes the return value of the clone
, during which it is unique. I think it all works out as required, but I wouldn't be surprised if I am missing something.
from rust.
Box<T, RcAllocator<A>>: Clone,
One thing that this doesn't help with, that the current CloneToUninit
does, is cloning into a different container type. I'm not sure if that's useful enough to be worthwhile, but it feels to me like it might be an important piece of a future Rust where there are fewer obstacles to using DSTs, which is one of the things I hoped to do by having (a safer version of) CloneToUninit
be public.
from rust.
Tangentially, a generic way to build a container type and then clone or write into its uninitialized payload slot would fit nicely into a solution for rust-lang/wg-allocators#90
from rust.
https://doc.rust-lang.org/nightly/std/clone/trait.CloneToUninit.html#implementors
I think we should consider adding #[doc(hidden)]
to the T: Copy
specializations. They are just distracting when it comes to documenting the public API of this trait.
from rust.
The std-dev-guide says we should use private specialization traits, which we do in most places. The implementation PR promoted a private trait to a public one, so it should have changed the specialization to a subtrait.
from rust.
The std-dev-guide says we should use private specialization traits, which we do in most places. The implementation PR promoted a private trait to a public one, so it should have changed the specialization to a subtrait.
I have implemented this suggestion in #126877 (scope creep, yay).
Also, this has an additional benefit of no longer needing any #[doc(hidden)]
to hide the mess:
I think we should consider adding
#[doc(hidden)]
to theT: Copy
specializations. They are just distracting when it comes to documenting the public API of this trait.
from rust.
Related Issues (20)
- Taking reference to function gets optimized to be 0x1 HOT 5
- document args for `-Zbuild-std` when folks aren't building everything HOT 5
- Well-formedness analysis pass on trait impl paniced HOT 2
- Unnecessary move in trait object comparison HOT 1
- Type inference complains about unknown size for `dyn Any` HOT 3
- ICE(non_lifetime_binders): const bound params not handled correctly HOT 3
- diagnostics should prefer naming items through the extern prelude instead of doc-hidden type aliases
- async lambda with capture ICE HOT 4
- std::net::TcpStream::connect_timeout on vxworks HOT 4
- async lambda fails to copy a value that is Copy HOT 8
- nightly ICE when building flexstr in release mode HOT 6
- `hir::Body`'s documentation incorrectly suggests it doesn't contain parmaeter info for closures
- error: internal compiler error: compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs:486:17: cannot relate region: LUB('{erased}, '{erased}) HOT 12
- Help needed finding a test that changes behaviour when changing DefiningOpaqueTypes::No to Yes
- Binary size creep on Fuchsia after commit 5ced3da HOT 6
- Type mismatch is incorrectly attributed in certain closures / async blocks
- ICE: Found unstable fingerprints for evaluate_obligation - computing whether enum is freeze - optimizing MIR
- Regression in `#[used]` attribute on Windows msvc HOT 16
- ICE in TaggedSerializer (serde) HOT 2
- missed optimization: coalese PathBuf reallocation HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust.