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 upTracking issue for CStr::from_bytes_with_nul #31190
Comments
alexcrichton
added
T-libs
B-unstable
labels
Jan 25, 2016
This comment has been minimized.
This comment has been minimized.
|
I'm writing a bunch of systems code, and would like this function, along with a couple of related trait impls I forget right now. One thing that jumps to mind: should the return be |
alexcrichton
added
the
I-nominated
label
Apr 19, 2016
This comment has been minimized.
This comment has been minimized.
CasualX
commented
Apr 24, 2016
•
|
I've just bumped into this function and I'd like to add a consideration: In C/C++ it happens that you get a fixed size buffer eg These days it is not encouraged to do this, but there's plenty of legacy stuff that uses it. In such a case interior nul bytes or not even having an ending nul is not a bug, but expected behaviour. In this case it would be desired to have a function that can convert these into Is there even a use case for when you have a byte slice (char pointer + length) and want it to contain exactly a nul terminated C string? I can only think of unbounded, nul terminated strings (covered by Fixed length char buffers with optional nul terminator can't ever be converted to |
This comment has been minimized.
This comment has been minimized.
|
@CasualX what you describe is not really a C string if it is not null terminted, and should not be used as one. Rust's
The function generally exists for data coming from a Rust application to be used in an FFI context, its main advantages being:
Take the implementation of Deref for CString for example; it uses It's also useful for other situations too though, like your fixed-size character buffer example. To convert it to a |
This comment has been minimized.
This comment has been minimized.
|
@CasualX What you describe sounds like a different abstraction from https://github.com/rust-lang/rust/blob/master/src/libstd/ffi/c_str.rs |
This comment has been minimized.
This comment has been minimized.
CasualX
commented
Apr 25, 2016
|
Ah I interpreted Rust's I came here because in reading some file formats there's quite a lot of 'fixed length char buffers containing a maybe nul terminated C string' for which I was looking how to easily deal with. A simple scan for nul byte should do the trick, thanks. I think it would be better to mimic the fn from_char_buf(buf: &[c_char]) -> &[u8] {
let len = buf.iter()
.enumerate()
.find(|&(_, &byte)| byte == 0)
.map_or_else(|| buf.len(), |(len, _)| len);
&buf[..len]
}But I digress as this is off-topic, to atone for bringing up someting unrelated have some on-topic thoughts: Ok I see, this API exposes the You suggest two use cases:
(My use case wouldn't be covered anyway, assuming the upfront length calculation really does go away at some point) The docs specifically say that the upfront length calculation is an implementation detail that is desired to be changed. Using this as an argument is essentially cementing in the API that the length check is done ahead of time. As for the API the checked version it should most certainly return a The names mirror their cousins in |
This comment has been minimized.
This comment has been minimized.
Yup, that probably works best. A File formats will do this fixed-size character buffer thing, but it's more a storage thing and a way to avoid a variable-length encoding rather than a
Hm? It's not... The point is if you need a similar abstraction to
Yes, that's why I called it a bonus. It's not an argument in favour of stabilizing, but it is a property of the function that probably won't disappear for maybe a year out from now. These kinds of implementation details are what I also forgot to mention the most important argument for the function: it retains lifetime information by borrowing the underlying slice, whereas
Agreed. We can't use the name |
This comment has been minimized.
This comment has been minimized.
CasualX
commented
Apr 25, 2016
|
If a name is what you want, I suggest |
This comment has been minimized.
This comment has been minimized.
|
The libs team decided, however, that we should probably return a |
alexcrichton
added
final-comment-period
and removed
I-nominated
labels
Apr 29, 2016
This comment has been minimized.
This comment has been minimized.
|
The libs team discussed this issue during triage yesterday and the decision was to stabilize |
alexcrichton commentedJan 25, 2016
Being added in #30614, this'll track the unstable status.