Skip to content

Use as_bytes_with_nul for deepsize of CString #27

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lopopolo
Copy link

CString::as_bytes_with_nul is cheap to call - it directly returns a
reference to the inner byte slice.

as_bytes_with_nul performs no allocations or validations.

See:

as_bytes_with_nul has been stable since Rust 1.0.0.

`CString::as_bytes_with_nul` is cheap to call - it directly returns a
reference to the inner byte slice.

`as_bytes_with_nul` performs  no allocations or validations.

See:

- https://doc.rust-lang.org/std/ffi/struct.CString.html#method.as_bytes_with_nul
- https://doc.rust-lang.org/src/std/ffi/c_str.rs.html#635-637

`as_bytes_with_nul` has been stable since Rust 1.0.0.
Comment on lines -103 to -104
// doesn't seem avoidable. This assumes that the allocation
// is the exact length of the string and the added null
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in practice this assumption is true since the inner repr of CString is a Box<[u8]> which is acquired by calling Vec::into_boxed_slice which documents that excess capacity is dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant