-
-
Notifications
You must be signed in to change notification settings - Fork 793
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
impls for null-terminated FFI string types #801
Conversation
serde/src/de/impls.rs
Outdated
@@ -295,6 +297,33 @@ impl Deserialize for String { | |||
|
|||
/////////////////////////////////////////////////////////////////////////////// | |||
|
|||
#[cfg(feature = "std")] | |||
impl Deserialize for Box<CStr> { |
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.
Please remove this impl unless you can implement it safely. The internal representation of CStr is not stable and there are plans to change it in the future.
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.
Ah, that's unfortunate. This is basically a direct translation of String::into_boxed_str
, since the equivalent CString::into_boxed_cstr
method does not exist. I think it's more common (and useful) to have the impl
for CStr
... Also allows working with, say Arc<CStr>
(or arccstr). Should I then also remove the Serialize
impl
?
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.
No, keep the Serialize impl. It's fine to support serializing a type without supporting deserializing it.
serde/src/de/impls.rs
Outdated
fn deserialize<D>(deserializer: D) -> Result<CString, D::Error> | ||
where D: Deserializer | ||
{ | ||
let mut v: Vec<u8> = try!(Deserialize::deserialize(deserializer)); |
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.
An empty vec would be an error right?
serde/src/ser/impls.rs
Outdated
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where S: Serializer | ||
{ | ||
(self.to_bytes_with_nul()).serialize(serializer) |
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.
This does not require parentheses around self.to_bytes_with_nul().
What are the reasons for including the null terminator in the serialized representation? |
serde/src/ser/impls.rs
Outdated
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where S: Serializer | ||
{ | ||
(self.to_bytes_with_nul()).serialize(serializer) |
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.
Using serialize_bytes
would allow most serializers to serialize this more efficiently than other types of slices.
Good point about including the NULL bytes in the serialization. I'll remove that. |
Ah, so in writing the tests I realized that using |
Unless there's a way to |
let v: Vec<u8> = try!(ByteBuf::deserialize(deserializer)).into(); |
That works, though I'd have to import |
Yep that's fine. |
So, it looks like we can also land deserialization for |
@dtolnay hmm -- why is |
You're missing #[cfg(feature = "std")] on the import. |
Okay, I think this is ready to merge @dtolnay. |
@jonhoo @dtolnay is there something open on serde's end to implement that once the method is stabilised? there's now a tracking issue for that feature at rust-lang/rust#40380 |
@clarcharr Not as far as I know, but I'm watching the issue and the PR, so will submit a follow-up PR here once it lands. |
From #800:
serde
currently ships with animpl
ofSerialize
andDeserialize
forstr
andString
, but not for the null-terminated C-like string typesffi::CStr
andffi::CString
. Under the hood, these string implementations are very similar, and providing serialization implementations for the latter two shouldn't be too hard.This patch adds serialization and deserialization of those types through null-terminated
[u8]
s. I'm guessing I should also implement tests for this, but didn't find where those should go. Feedback welcome!