This seems nice, but I would like to be able to use this feature without using unsafe code, also this doesn’t solve the problem of expensive moves, (when you call buffer.into_inner() you move a potentially large object around). Also, I would like to allow Drop types and a number of rules are there so that Drop types are safe, and not confusing.
This is a fairly advanced feature, and goes into the realm of micro-optimization, and is in general not needed. But in the cases that it is needed, it would be good to have Drop types, to be able to directly initialize into the allocated memory and to be completely unsafe free.
But I do want to note, we don’t have to be completely safe around Drop types, because Rust does not guarantee that destructors run, we even have a type ManuallyDrop, and a function forget that explicitly allows you to not run destructors. This would just be really confusing to new people, so everything related to drop types in this rationale can just be lints that are error-by-default.
Rationale for Rules
&write T can only be assigned to once
This rule prevents unintended writes and allows for Drop types to be used safely, without fear of memory leaks.
After being written to &write T are promoted to a &mut T
This is mostly just ergonomics, to allow free usage of the value we just created.
Writing does not drop old value.
This is necessary for handling uninitialized memory. Note the semantics are different from &mut T, so it would cause confusing bugs to allow this coercion if Drop types are allowed.
You can only reference partially initialized memory through a &write T
This is also to ensure that uninitialized memory does not get read from.
&write T follows the same rules as &mut T for the borrow
This note is so we have a xor relation between many reads and one write, to ensure memory safety, for the same reasons as &mut T
If you access fields of T using a &write T
-
T: !Drop , this is only initially to ease of implementation (don’t have to worry about drops)
- This restriction could be removed
This is just in-case there was some unsoundness with &write pointers to Drop types
You cannot conditionally assign to a &write T
This removes the need for unsafe code.
Functions and closures that take a &write T argument must initialize before returning
This also ensures that we don’t need unsafe code, and prevents bugs where you take a &write reference, but you don’t need it.
&writeT cannot be coerced to and from another type
This would be confusing if drop types are allowed, otherwise, it is unnecessary
- You can take a
&write T on any T
Just a note, I don’t know if I need to say it, but I will.