-
-
Notifications
You must be signed in to change notification settings - Fork 175
cstr16: add method to get the underlying bytes #788
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to leave out the AsRef/Borrow impls, as it would not be as clear when reading the code as an explicit call to
to_bytes_with_nul
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd much rather drop
self.to_bytes()
and renameself.to_bytes_with_nul()
toself.to_bytes()
. Then we can keep AsRef and Borrow. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that sounds reasonable. Also, I didn't think of this before, but it should probably be
as_bytes
instead ofto_bytes
(per the conversion naming guidelines).For Borrow and AsRef, do you have an example of how they'd get used in practice? Like
<CStr16 as AsRef<[u8]>>::as_ref(string)
is pretty verbose so I assume a user would usually prefer to writestring.as_bytes()
. But perhaps there's some generic code where the former would be more usable? An example would help me clear up my doubts since I always get a bit confused with those two very-similar-seeming traits.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. For example
FileSystem::write()
consumescontent: impl AsRef<[u8]>
as second parameter. This is equivalent to the standard lib's implementation.Hence, you can just write a CStr16 to a file.
I only implemented both, AsRef and Borrow, as the standard library also does this for a lot of types (for example for std::path::Path). In my past years I've come accross a lot of code that used
AsRef
-types as parameters butBorrow
only once or twice - Probably good to have both, tho.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update Hm, now I'm confused.. https://doc.rust-lang.org/core/ffi/struct.CStr.html provides
to_bytes()
andto_bytes_with_nul()
methods, but those method implementations are cheap (other than the naming convention implies).But I do not see a strong reason why we should be consistent with https://doc.rust-lang.org/core/ffi/struct.CStr.html .. but..on the other hand.. we can also stay consistent with
std::ffi::CStr
and provideto_bytes()
,to_bytes_until_nul()
, and drop AsRef and Borrow, as it might be confusing then.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. I would guess the reason is this:
Several
CStr
methods make note that the implementation of the type may change in the future to make it more FFI friendly. Not sure how likely that is to ever actually happen.I'm realizing I don't have a very strong opinion here :)
How about: just provide the
as_bytes
method (so no separate with/without null methods), and also include theAsRef
andBorrow
impls?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done