-
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
RFC: truly unsized types #813
Comments
With support from the compiler, unsized types could opt out of pub struct CStr {
head: libc::c_char
}
impl !Sized for CStr { } |
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 |
I have updated the branch with the ideas described above. The rewritten proposal is much leaner and cleaner. |
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
} |
@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) |
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, 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. |
@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()
} |
@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. |
My concern is that |
@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 |
If NSObject does not have DST contents (like object type pointers/references) and implements |
Argh, sorry, my generic |
This came up in #996 as well. |
@SSheldon @nikomatsakis Ping? |
@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. |
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
The text was updated successfully, but these errors were encountered: