diff --git a/Cargo.toml b/Cargo.toml index c1c686555..ed0d39170 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ default = [ "mio-uds", "num_cpus", "pin-project-lite", + "serde", ] docs = ["attributes", "unstable"] unstable = ["default", "broadcaster"] @@ -70,6 +71,7 @@ once_cell = { version = "1.2.0", optional = true } pin-project-lite = { version = "0.1", optional = true } pin-utils = { version = "0.1.0-alpha.4", optional = true } slab = { version = "0.4.2", optional = true } +serde = { version = "1.0.0", optional = true, default-features = false, features = ["derive"] } [dev-dependencies] femme = "1.2.0" diff --git a/src/lib.rs b/src/lib.rs index ddc6462ca..f015e614b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -171,6 +171,18 @@ //! features = ["attributes"] //! ``` //! +//! When the `serde` feature is enabled, it is possible to use `async_std` types such as [`path::PathBuf`] +//! within types that are serialized or deserialized using [`serde`] compatible crates. +//! +//! [`serde`]: https://serde.rs +//! [`path::PathBuf`]: path/struct.PathBuf.html +//! +//! ```toml +//! [dependencies.async-std] +//! version = "1.0.0" +//! features = ["serde"] +//! ``` +//! //! Additionally it's possible to only use the core traits and combinators by //! only enabling the `std` Cargo feature: //! diff --git a/src/path/pathbuf.rs b/src/path/pathbuf.rs index 56a63a47e..a25a6d5aa 100644 --- a/src/path/pathbuf.rs +++ b/src/path/pathbuf.rs @@ -13,14 +13,15 @@ use crate::path::Path; use crate::prelude::*; #[cfg(feature = "unstable")] use crate::stream::{self, FromStream, IntoStream}; +#[cfg(feature = "serde")] +use serde::{Serialize, Deserialize}; /// This struct is an async version of [`std::path::PathBuf`]. /// /// [`std::path::Path`]: https://doc.rust-lang.org/std/path/struct.PathBuf.html +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct PathBuf { - inner: std::path::PathBuf, -} +pub struct PathBuf(std::path::PathBuf); impl PathBuf { /// Allocates an empty `PathBuf`. @@ -49,7 +50,7 @@ impl PathBuf { /// assert_eq!(Path::new("/test"), p.as_path()); /// ``` pub fn as_path(&self) -> &Path { - self.inner.as_path().into() + self.0.as_path().into() } /// Extends `self` with `path`. @@ -84,7 +85,7 @@ impl PathBuf { /// assert_eq!(path, PathBuf::from("/etc")); /// ``` pub fn push>(&mut self, path: P) { - self.inner.push(path.as_ref()) + self.0.push(path.as_ref()) } /// Truncates `self` to [`self.parent`]. @@ -108,7 +109,7 @@ impl PathBuf { /// assert_eq!(Path::new("/"), p); /// ``` pub fn pop(&mut self) -> bool { - self.inner.pop() + self.0.pop() } /// Updates [`self.file_name`] to `file_name`. @@ -138,7 +139,7 @@ impl PathBuf { /// assert!(buf == PathBuf::from("/baz.txt")); /// ``` pub fn set_file_name>(&mut self, file_name: S) { - self.inner.set_file_name(file_name) + self.0.set_file_name(file_name) } /// Updates [`self.extension`] to `extension`. @@ -167,7 +168,7 @@ impl PathBuf { /// assert_eq!(Path::new("/feel/the.dark_side"), p.as_path()); /// ``` pub fn set_extension>(&mut self, extension: S) -> bool { - self.inner.set_extension(extension) + self.0.set_extension(extension) } /// Consumes the `PathBuf`, returning its internal [`OsString`] storage. @@ -183,7 +184,7 @@ impl PathBuf { /// let os_str = p.into_os_string(); /// ``` pub fn into_os_string(self) -> OsString { - self.inner.into_os_string() + self.0.into_os_string() } /// Converts this `PathBuf` into a [boxed][`Box`] [`Path`]. @@ -191,7 +192,7 @@ impl PathBuf { /// [`Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html /// [`Path`]: struct.Path.html pub fn into_boxed_path(self) -> Box { - let rw = Box::into_raw(self.inner.into_boxed_path()) as *mut Path; + let rw = Box::into_raw(self.0.into_boxed_path()) as *mut Path; unsafe { Box::from_raw(rw) } } } @@ -223,13 +224,13 @@ impl> From<&T> for PathBuf { impl From for PathBuf { fn from(s: OsString) -> PathBuf { - PathBuf { inner: s.into() } + PathBuf(s.into()) } } impl From for OsString { fn from(path_buf: PathBuf) -> OsString { - path_buf.inner.into() + path_buf.0.into() } } @@ -265,7 +266,7 @@ impl Deref for PathBuf { type Target = Path; fn deref(&self) -> &Path { - Path::new(&self.inner) + Path::new(&self.0) } } @@ -314,7 +315,7 @@ impl From for Rc { impl AsRef for PathBuf { fn as_ref(&self) -> &OsStr { - self.inner.as_ref() + self.0.as_ref() } } @@ -355,18 +356,18 @@ impl<'b, P: AsRef + 'b> FromStream

for PathBuf { impl From for PathBuf { fn from(path: std::path::PathBuf) -> PathBuf { - PathBuf { inner: path } + PathBuf(path) } } impl Into for PathBuf { fn into(self) -> std::path::PathBuf { - self.inner + self.0 } } impl AsRef for PathBuf { fn as_ref(&self) -> &std::path::Path { - self.inner.as_ref() + self.0.as_ref() } }