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

OsStr and OsString platform differentiation does not match Rust standard library #2864

Open
surban opened this issue Nov 30, 2024 · 0 comments

Comments

@surban
Copy link

surban commented Nov 30, 2024

This is the platform differentiation of the Rust standard library for OsStr, provided in src/sys/os_str/mod.rs:

cfg_if::cfg_if! {
    if #[cfg(any(
        target_os = "windows",
        target_os = "uefi",
    ))] {
        mod wtf8;
        pub use wtf8::{Buf, Slice};
    } else {
        mod bytes;
        pub use bytes::{Buf, Slice};
    }
}

I.e. OsStr is a WTF-8 encoded string for Windows and UEFI and a bytes array for all other platforms.

However, serde only differentiates between UNIX and Windows for serialization and deserialization, as can be seen from the following serialization code for OsStr:

#[cfg(all(feature = "std", any(unix, windows)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "std", any(unix, windows)))))]
impl Serialize for OsStr {
    #[cfg(unix)]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        use std::os::unix::ffi::OsStrExt;
        serializer.serialize_newtype_variant("OsString", 0, "Unix", self.as_bytes())
    }

    #[cfg(windows)]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        use std::os::windows::ffi::OsStrExt;
        let val = self.encode_wide().collect::<Vec<_>>();
        serializer.serialize_newtype_variant("OsString", 1, "Windows", &val)
    }
}

Obviously there is a mismatch between the standard library and serde, causing problems on platforms other than Windows and UNIX.

I think the best solution would be to use the same platform differentiation as the standard library. Would this be possible?

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

No branches or pull requests

1 participant