Skip to content
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

Serialize/Deserialize a std::ffi::CStr #30

Merged
merged 3 commits into from
Sep 6, 2018
Merged

Conversation

Michael-F-Bryan
Copy link
Contributor

@Michael-F-Bryan Michael-F-Bryan commented Aug 30, 2018

This PR adds implementations of TryIntoCtx and TryFromCtx for &CStr and CString when compiling with the std feature enabled.

We could probably speed the TryFromCtx up a little bit by using something like memchr for finding the string's null terminator seeing as that's the most expensive part, but that adds an extra dependency and I wasn't sure whether it was necessary.

Copy link
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

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

@Michael-F-Bryan thanks for the PR, and sorry for the delay. This looks good to me. It might be good to add a pread/pwrite test as well ? Or perhaps a new section in the readme, examples, etc., up to you. And thanks again for this !

src/ctx.rs Show resolved Hide resolved
src/ctx.rs Outdated
})
};

let data = src.get(..null_byte+1).unwrap();
Copy link
Owner

Choose a reason for hiding this comment

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

Why not just src[..null_byte+1]?

Also so I’m too stupid right now to reason about the +1 indexes and since there are tests checking bytes read I’ll just assume it’s correct :P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remember hitting a really bizarre lifetimes issue, where &src[..null_byte + 1] would reborrow from src giving data a temporary lifetime which only lasted until the end of the function. The get() method returned an &[u8] with the 'a lifetime we want to return.

I believe I was writing this PR using nightly and when I tried to reproduce it in the playground everything worked 😞 Maybe there was a bug in NLL that's been fixed recently?

Also, the +1 is because .. is a half open range. When constructing the &CStr via CStr::from_bytes_with_nul_unchecked(data) we want to include that trailing null, so you need to specify the index after our null terminator.

Copy link
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

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

Lol I resolved your comment accidentally, whatever that means. Anyway, yea I like those proposed names, but we don’t need to put it in now if you’re busy. Going to merge this PR, thanks so much !

@m4b m4b merged commit 2bb18d1 into m4b:master Sep 6, 2018
@Michael-F-Bryan Michael-F-Bryan deleted the cstr branch September 6, 2018 04:19
willglynn pushed a commit to willglynn/scroll that referenced this pull request Nov 2, 2018
Serialize/Deserialize a std::ffi::CStr
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.

2 participants