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 upMethod for `Option<&T>` that converts to `*const T` #1652
Comments
This comment has been minimized.
This comment has been minimized.
|
Well, you can't have both |
This comment has been minimized.
This comment has been minimized.
if |
This comment has been minimized.
This comment has been minimized.
|
@durka, they could also be named differently. |
This comment has been minimized.
This comment has been minimized.
I also find myself wanting this from time to time. You're right that it seems that |
This comment has been minimized.
This comment has been minimized.
|
For what it's worth, the reason I say that I want this "from time to time" (rather than frequently) is that when writing new The reason I like this approach is that it documents the requirements on the C code directly in the signature, and the Rust code simply assumes that the C code is following the rules that the Rust compiler would normally have enforced. I gave a talk about this at RustCamp (Using Rust from C... or Any Language). |
nrc
added
the
T-libs
label
Aug 17, 2016
This comment has been minimized.
This comment has been minimized.
|
Maybe I'm just ignorant, but it seems a bit niche to me, and I think that Option already has a very inelegant API, though I'm not sure how to fix it... |
This comment has been minimized.
This comment has been minimized.
tmccombs
commented
Dec 10, 2016
|
I'd also like to see this added. A more generic approach would be to define an Then there could be a complementary trait Another advantage of this is that to_ptr and into_raw would work for custom pointer types. |
This comment has been minimized.
This comment has been minimized.
tmccombs
commented
Dec 10, 2016
|
I wrote a crate that implements the traits I mentioned: https://github.com/tmccombs/ptrplus |
This comment has been minimized.
This comment has been minimized.
thenewwazoo
commented
Aug 6, 2017
•
|
I encountered this desire today in an embedded context. I've got C functions being called from a |
jmegaffin commentedJun 16, 2016
•
edited
I find myself often wanting to go from an
Option<&T>to a*const T, which SHOULD be an easy and safe conversion, as everyOption<&T>is guaranteed to correspond a*const Tidentical to it in representation. We already have a conversion,std::pointer::as_ref, the other way around, so I find it strange that the shortest way to do so (without defining a new function) is this:It's just as easy to do the same for
Option<&mut T> -> *mut T, of course, although I can't see the utility in it.It's not as fundamental as
Option<&T> -> *const T, but it would also be nice to haveOption<&CStr> -> *const c_char.