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

Implement Serialize/Deserialize for OsStr/OsString #824

Merged
merged 1 commit into from
Mar 27, 2017

Conversation

alexcrichton
Copy link
Contributor

@alexcrichton alexcrichton commented Mar 27, 2017

This commit implements the two serde traits for the libstd OsStr and
OsString types. This came up as a use case during implementing sccache where
we're basically just doing IPC to communicate paths around. Additionally the
Path and PathBuf implementations have been updated to delegate to the os
string ones.

These types are platform-specific, however, so the serialization/deserialization
isn't trivial. Currently this "fakes" a newtype variant for Unix/Windows to
prevent cross-platform serialization/deserialization. This means if you're doing
IPC within the same OS (e.g. Windows to Windows) then serialization should be
infallible. If you're doing IPC across platforms (e.g. Unix to Windows) then
using OsString is guaranteed to fail as bytes from one OS won't deserialize on
the other (even if they're unicode).

Closes #823

@alexcrichton alexcrichton force-pushed the osstr-osstring branch 8 times, most recently from 18fbaa8 to d968d0f Compare March 27, 2017 17:31
@dtolnay
Copy link
Member

dtolnay commented Mar 27, 2017

I have to run but the test is wrong - the windows test says "Widnows" but is skipped because of the early return in the previous test.

I would prefer to update the test macro to look like:

#[cfg(unix)]
test_osstring {
    {
        use std::os::unix::ffi::OsStringExt;
        OsString::from_vec(vec![1, 2, 3])
    } => &[
        /* ... */
    ],
}
#[cfg(windows)]
test_osstring {
    {
        use std::os::windows::ffi::OsStringExt;
        OsString::from_wide(&[1, 2, 3])
    } => &[
        /* ... */
    ],
}

If you don't get to it I will do this myself this afternoon and then publish a release for you.

This commit implements the two serde traits for the libstd `OsStr` and
`OsString` types. This came up as a use case during implementing sccache where
we're basically just doing IPC to communicate paths around. Additionally the
`Path` and `PathBuf` implementations have been updated to delegate to the os
string ones.

These types are platform-specific, however, so the serialization/deserialization
isn't trivial. Currently this "fakes" a newtype variant for Unix/Windows to
prevent cross-platform serialization/deserialization. This means if you're doing
IPC within the same OS (e.g. Windows to Windows) then serialization should be
infallible. If you're doing IPC across platforms (e.g.  Unix to Windows) then
using `OsString` is guaranteed to fail as bytes from one OS won't deserialize on
the other (even if they're unicode).
@alexcrichton
Copy link
Contributor Author

Oops good catch, I couldn't quite get #[cfg] to work out in the macro unfortunately so I just extracted it to a separate test case

@dtolnay dtolnay merged commit dbc9a60 into serde-rs:master Mar 27, 2017
@dtolnay
Copy link
Member

dtolnay commented Mar 28, 2017

Thanks, I released this in serde 0.9.12 and filed mozilla/sccache#93 to follow up on the sccache side.

@alexcrichton
Copy link
Contributor Author

Thanks @dtolnay!

@clarfonthey
Copy link

@alexcrichton A bit late on this, but one comment: shouldn't the newtype have a separate variant for redox? Because stdlib has a separate implementation for redox and I assume that there's a reason for that.

@alexcrichton
Copy link
Contributor Author

I think that's a backwards compatible extension?

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

Successfully merging this pull request may close these issues.

3 participants