-
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
Allow the unsafe
qualifier on struct fields
#80
Conversation
+1 cc @Tobba |
Should initialising an |
@huonw I do think it's worth having a discussion over what actions, exactly, should be restricted, rather than just tacitly accepting a blanket restriction on all accesses. As for your question itself, I'm not sure, though off the top of my head I see no reason to make it so. Similarly I personally see no reason why reading an unsafe field ought to require an unsafe block, but the straw poll on IRC seemed to favor the "make everything require unsafe" approach (with notable dissent from @eddyb). |
Initialization seems like it shouldn't be unsafe. On Mon, May 19, 2014 at 12:40 AM, Ben Striegel notifications@github.comwrote:
|
I would think the only reason to use an Hence, in my mind, anything that sets a value for that field should be Vec {
capacity: 0,
length: 10,
pointer: ptr::null()
} I agree that reading the field doesn't necessarily need to be unsafe (since, in almost all cases, reading can't change the invariants... although maybe it can for "memory-mapped" hardware, where reading/writing from specific places in memory has external side-effects). However, I wonder if most of the advantages of this can be covered by something like (i.e. avoid extending the language): // unfortunately, this name overlaps with `core::ty::Unsafe`
struct Unsafe<T> {
value: T
}
impl<T> Unsafe<T> {
unsafe fn new(value: T) -> Unsafe<T> {
Unsafe { value: T }
}
fn move(self) -> T { self.value }
unsafe fn get_mut<'a>(&'a mut self) -> &'a mut T { &mut self.value }
}
impl<T> Deref<T> for Unsafe<T> {
fn deref<'a>(&'a self) -> &'a T { &self.value }
} (Using my scheme of initialisation being unsafe, and @bstrie's of reading being safe.) |
This doesn't seem very useful to me. Within a module I would expect the authors to know what they're doing, and the unit-tests to save them when they do not. For other users, you could simply introduce getters and setters, and functions/methods can already be marked unsafe. |
Agreed with @Thiez. This doesn't seem like it needs language-level support. |
I agree with @Thiez as well. Not only that, but having the As a side note, I believe this would also prevent deriving any traits for the struct. |
@kballard I would think it's very rare that types with |
@huonw #[deriving(Clone)]
pub struct Path {
repr: Vec<u8>,
sepidx: Option<uint>
} It would be a good candidate for That's not to say I want to make the |
I don't think it should be necessary to wrap a struct that has (what would be) As a bonus that hasn't been mentioned yet, marking fields as unsafe means that (correctly-written) unsafe blocks can have (barring other loopholes) truly safe interfaces, even within a module - Vec's |
I also want to draw attention to a function from Vec:
Note that the function is manually annotated as If we have There are probably more subtle examples where a function without any unsafe blocks is manipulating fields in an unsafe way. |
Meta note on RFC writing style: I don't understand how @bstrie expects the opening example from the Detailed Design to be re-written after this feature is added. Is just Basically, if you are going to drive presentation via an example, please finish each example, and no switching midway. |
Per https://github.com/rust-lang/rust/wiki/Meeting-weekly-2014-06-17#rfc-pr-80-unsafe-fields-httpsgithubcomrust-langrfcspull80-, let's not do this right now. While there is some agreement that unsafe fields are desirable, the |
We probably should have marked this as "postponed", since the phrase used when we closed it was "let's not do this right now" :) So, adding postponed label. |
It seems like the right time to reopen this. |
futures-mio: update comment and simplify echo example
No description provided.