Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAllow the `unsafe` qualifier on struct fields #80
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
+1 cc @Tobba |
This comment has been minimized.
This comment has been minimized.
|
Should initialising an |
This comment has been minimized.
This comment has been minimized.
|
@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). |
This comment has been minimized.
This comment has been minimized.
|
Initialization seems like it shouldn't be unsafe. On Mon, May 19, 2014 at 12:40 AM, Ben Striegel notifications@github.comwrote:
|
This comment has been minimized.
This comment has been minimized.
|
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 comment has been minimized.
This comment has been minimized.
Thiez
commented
May 19, 2014
|
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. |
This comment has been minimized.
This comment has been minimized.
Valloric
commented
May 19, 2014
|
Agreed with @Thiez. This doesn't seem like it needs language-level support. |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
@kballard I would think it's very rare that types with |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
nmsmith
commented
Jun 14, 2014
|
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 |
This comment has been minimized.
This comment has been minimized.
nmsmith
commented
Jun 15, 2014
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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 |
brson
closed this
Jun 18, 2014
pnkfelix
referenced this pull request
Jul 22, 2014
Closed
rustc: Allow safe access of shareable static muts #14862
This was referenced Jul 23, 2014
This comment has been minimized.
This comment has been minimized.
|
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. |
pnkfelix
added
the
postponed
label
Oct 9, 2014
This comment has been minimized.
This comment has been minimized.
|
It seems like the right time to reopen this. |
bstrie commentedMay 19, 2014
No description provided.