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 upRFC: truly unsized types #813
Comments
pnkfelix
added
the
postponed
label
Feb 5, 2015
This comment has been minimized.
This comment has been minimized.
|
With support from the compiler, unsized types could opt out of pub struct CStr {
head: libc::c_char
}
impl !Sized for CStr { } |
This comment has been minimized.
This comment has been minimized.
|
The thin-pointer requirements can be expressed with a couple of traits that have blanket impls for all sized types: pub trait AsPtr<Raw = Self> {
fn as_ptr(&self) -> *const Raw;
}
pub trait FromPtr<Raw = Self> {
unsafe fn from_ptr<'a>(ptr: *const Raw) -> &'a Self;
}
impl<T> AsPtr<T> for T {
fn as_ptr(&self) -> *const T { self as *const T }
}
impl<T> FromPtr<T> for T {
unsafe fn from_ptr<'a>(ptr: *const T) -> &'a T { std::mem::transmute(&*ptr) }
}These traits could then be implemented explicitly (or via |
This comment has been minimized.
This comment has been minimized.
|
I have updated the branch with the ideas described above. The rewritten proposal is much leaner and cleaner. |
This comment has been minimized.
This comment has been minimized.
|
Given a type like this, how would I rewrite it using your proposal and implement your traits for it? #[repr(C)] pub struct TOKEN_PRIVILEGES {
PrivilegeCount: DWORD,
Privileges: [LUID_AND_ATTRIBUTES; 0], // This should be unsized
} |
This comment has been minimized.
This comment has been minimized.
|
@retep998 You'd need to opt it out of impl !Sized for TokenPrivileges { }The pointer conversion traits, in your case, can be implemented the same way as with To access individual members of the (BTW, perhaps changing the case of the names to Rust conventions, like I have done above, would make for better bindings; other than function names, the exact C naming is not relevant to Rust code AFAIK, and functions can have their linkage names overridden with an attribute) |
This comment has been minimized.
This comment has been minimized.
|
I have added a question about landing the proposed pointer conversion traits earlier as a lead-in to start adapting code for eventual support of |
mzabaluev
referenced this issue
Feb 11, 2015
Merged
RFC: CStr, the dereferenced complement to CString #592
This comment has been minimized.
This comment has been minimized.
SSheldon
commented
Feb 11, 2015
|
@mzabaluev, your let s = "hello";
let p: *const str = s as *const str;
println!("{:?}", std::mem::size_of_val(&p));Pointers in rust aren't guaranteed to be a single word; even DSTs can be converted to a raw pointer. Since every reference can be converted to a pointer, I'm not sure how this trait would help, and it doesn't guarantee that the pointers you get from it are a single word. |
This comment has been minimized.
This comment has been minimized.
|
@SSheldon, this is as intended. A bound for thin pointers can still be expressed, e.g. fn want_my_pointers_thin<T: ?Sized, U>(v: &T) -> *const U
where T: AsPtr<U>, U: Sized
{
v.as_ptr()
} |
This comment has been minimized.
This comment has been minimized.
SSheldon
commented
Feb 12, 2015
|
@mzabaluev, that struct CStr {
head: libc::c_char,
}
impl !Sized for CStr { }
impl AsPtr<CStr> {
fn as_ptr(&self) -> *const CStr { self as *const CStr }
}even though CStr pointers would be thin, so it should accept them. Sure, we could just define AsPtr<c_char> for CStr instead, but that doesn't help for cases with unsized FFI pointers. |
This comment has been minimized.
This comment has been minimized.
SSheldon
commented
Feb 12, 2015
|
My concern is that |
This comment has been minimized.
This comment has been minimized.
|
@SSheldon your example doesn't seem valid; did you mean: impl AsPtr<CStr> for CStr { ... }If impl AsPtr<c_char> for CStr { ... }You can do this already with DSTs; we've agreed in #592 that |
This comment has been minimized.
This comment has been minimized.
If NSObject does not have DST contents (like object type pointers/references) and implements |
This comment has been minimized.
This comment has been minimized.
|
Argh, sorry, my generic |
This comment has been minimized.
This comment has been minimized.
|
This came up in #996 as well. |
nrc
added
the
T-lang
label
Aug 21, 2016
This comment has been minimized.
This comment has been minimized.
DemiMarie
commented
Feb 21, 2017
|
@SSheldon @nikomatsakis Ping? |
This comment has been minimized.
This comment has been minimized.
SSheldon
commented
Feb 21, 2017
|
@DemiMarie not sure what you're pinging about :) If you're wondering about the status of this, it's still postponed. #1524 was an RFC opened aimed at solving this, there are some good comments recently from that PR. It was postponed again because at the current time there are higher priority things for rust to tackle and solving this would introduce significant complexity. |
pnkfelix commentedFeb 5, 2015
Further subdivide unsized types into dynamically-sized types, implementing
an intrinsic trait
DynamicSize, and types of indeterminate size. Referencesfor the latter kind will be thin, while allocating slots or copying values
of such types is not possible outside unsafe code.
Rendered
Tracking issue for postponed PR #709