-
Notifications
You must be signed in to change notification settings - Fork 1
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
InvariantFree trait #6
Comments
One interesting point here is how this trait interacts with others. For instance zeroable - should the zeroable type assume |
I don't quite follow what this trait is doing, but if it's anything at all other than "this struct is repr(transparent)" then it needs a different name. |
That’s fair. The point of this trait is “this type has no internal invariants so it’s fine to initialize one in any state.” |
(Just to throw more stuff into the bikeshed, maybe |
In |
|
I'm curious about the relationship of Correct me if I'm wrong, but if It is safe to transmute an arbitrary byte sequence of the correct size and alignment to a value of type However, if Is this an accurate summary? If so, I think this would be very useful. I'm writing a serialization/deserialization library, and for some types I'd like to assert something like: "Values of this type can be created by casting from a byte sequence of the right length and alignment, however type-specific invariants must be checked before the resulting value can be used safely." but also for some other types "Values of this type can be created by casting from a byte sequence, and then no further checks are required to guarantee safety." If As for the name, I like |
@casey I think your definition of The only think I'm not sure of is if |
I'm also not sure if we should make a distinction between types that require their invariants to be checked to used safely vs types that require invariants to be checked to be used correctly. This is a subtle distinction, but I can imagine safe could that requires a caller to verify invariants in order for certain operations to be used correctly even though if they are used incorrectly it does not lead to unsafe code (but perhaps to wrong results or panics). |
If that's the case, than I think that
If
In my own codebase, I have a lot of unsafe casts that I believe are always As a side note, even if those casts are safe, the functions that perform them
It's probably a good idea to separate the naming of those traits from whether
I personally think this distinction is useful, because there is such a huge |
This trait indicates a type has no internal validity requirements. Even when two types are layout compatible it is not generically safe to cast between them. Types may define additional invariants of its attributes and even unsafely depend on them, such as requiring all its constructors to perform some form of global initialization.
This is in many regards a structural marker trait. A type could safely implement/derive the trait iff all of its attributes implement the trait as well, similar to the
Copy
trait. This is because an incorrectimpl
could only be written by the same crate that tries to rely on its invariants, a pure logic bug in that crate, and no other safe code is affected. Adding this trait incore
would thus present a unique solution that is currently not possible to implement in a pure user-crate.Prior art
The
typic
crate has aTransparent
trait, that needs to be manually and unsafely implemented for types with private fields. It is the exact additional requirement that differentiates itstransmute_safe
casts fromtransmute_sound
casts. However, this implementation currently has a bug as the automatic trait derive does not assert the structural condition, that all fields implement the trait as well.In the
zerocopy
andbytemuck
crates this trait is implicitely part of theFromBytes
andPod
trait respectively.Future directions
Similar to the
Copy
trait there could be anunsafe impl Transparent
in the future, that would allow a type to implement the trait despite some attribute or structurally contained type not doing so. This served a very similar purpose than discussed in rust #25053 forUnsafellCell
,MaybeUninit
, etc. and other types where it is not possible in safe code to access the attribute of the contained type.Another possible use would be dynamic version where the valid instance is inspected to determine if its safety invariants are met. How that interface would look could be discussed or prototyped in one of the bytecast crates, the advantages of
core
implementation do not apply.The text was updated successfully, but these errors were encountered: