-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Make accessing Unsafe<T> fields unsafe #182
Conversation
This RFC very specifically only makes the fields of the pub struct Unsafe<T> {
pub unsafe value: T,
pub marker1: marker::InvariantType<T>
} This approach avoids special-casing |
The reason I prefer this to be |
Hrm. I suppose you have a point, although anyone doing that would probably know they're doing something very silly anyway. What if an |
The entire point here is to allow initialization without |
@arielb1 Why is that useful? End-users of Rust APIs do not construct |
Dereferencing Unsafe is semantically special because it can trigger undefined behaviour (while dereferencing any other field can't trigger UB by itself). |
@arielb1 You can't dereference Assuming you mean "accessing a field of", I'm still not sure how this is relevant to the use of |
I meant "taking a reference to a field of". It is the only way you can have undefined behaviour outside of an unsafe block, which is weird. |
@arielb1 Well, taking a reference in this case is largely equivalent to creating a |
Sure. And therefore it should be unsafe (like dereferencing a regular |
@arielb1 Yes, accessing the My only point of contention here is whether it makes sense to special-case field access of |
Accessing fields that aren't the |
Just noticed one point worth arguing:
That's not true if unsafe fields are unsafe to initialize just as they are unsafe to modify. You can (and should) enforce that construction of a Vec take place in an unsafe block as well. |
I definitely don't think this should be specific to |
I agree with all of what @thestinger just said. Unsafe fields are valuable in many situations, and hopefully will be implemented as their own thing at some point. But that's a point for discussion here: #80 Perhaps |
The issue here is that accessing Unsafe can result in undefined behaviour while accessing other fields can't - i.e. other unsafe fields should probably be readable safely, while here they can't/ Thinking about it more, actual unsafe fields do seem like a good addition (but I think that is less important than this change, and I have no problem with it being delayed past 1.0, etc.) – privacy is subtly different from unsafeness, the former being "don't access this as it may not be here next version", while the latter means "be careful while touching this, you may break something" – logically, However, this orthogonality isn't really related to the As a summary, there are 3 different reasons for controlled field access: Privacy – the field is an implementation detail, to ensure the consequences of changing its semantics are limited. These seem different enough (well you can say there's a "no data race" invariant, but that is a rather weird one, being breakable by reads). |
@arielb1: I'm fully aware that |
You can read the field of an |
Changing the way unsafe fields work on Rust has been discussed a few times, there are several potential designs, and the need is not pressing. With all the other changes in the pipeline, this is not something we want to do now. Thank you. |
Future::into_stream
Pretty: https://github.com/arielb1/rfcs/blob/unsafe-unsafe-access/active/0000-unsafe-unsafe-access.md