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

Add proof of concept serde support with PathBuf #512

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ default = [
"mio-uds",
"num_cpus",
"pin-project-lite",
"serde",
]
docs = ["attributes", "unstable"]
unstable = ["default", "broadcaster"]
Expand Down Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//!
Expand Down
35 changes: 18 additions & 17 deletions src/path/pathbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -84,7 +85,7 @@ impl PathBuf {
/// assert_eq!(path, PathBuf::from("/etc"));
/// ```
pub fn push<P: AsRef<Path>>(&mut self, path: P) {
self.inner.push(path.as_ref())
self.0.push(path.as_ref())
}

/// Truncates `self` to [`self.parent`].
Expand All @@ -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`.
Expand Down Expand Up @@ -138,7 +139,7 @@ impl PathBuf {
/// assert!(buf == PathBuf::from("/baz.txt"));
/// ```
pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
self.inner.set_file_name(file_name)
self.0.set_file_name(file_name)
}

/// Updates [`self.extension`] to `extension`.
Expand Down Expand Up @@ -167,7 +168,7 @@ impl PathBuf {
/// assert_eq!(Path::new("/feel/the.dark_side"), p.as_path());
/// ```
pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
self.inner.set_extension(extension)
self.0.set_extension(extension)
}

/// Consumes the `PathBuf`, returning its internal [`OsString`] storage.
Expand All @@ -183,15 +184,15 @@ 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`].
///
/// [`Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html
/// [`Path`]: struct.Path.html
pub fn into_boxed_path(self) -> Box<Path> {
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) }
}
}
Expand Down Expand Up @@ -223,13 +224,13 @@ impl<T: ?Sized + AsRef<OsStr>> From<&T> for PathBuf {

impl From<OsString> for PathBuf {
fn from(s: OsString) -> PathBuf {
PathBuf { inner: s.into() }
PathBuf(s.into())
}
}

impl From<PathBuf> for OsString {
fn from(path_buf: PathBuf) -> OsString {
path_buf.inner.into()
path_buf.0.into()
}
}

Expand Down Expand Up @@ -265,7 +266,7 @@ impl Deref for PathBuf {
type Target = Path;

fn deref(&self) -> &Path {
Path::new(&self.inner)
Path::new(&self.0)
}
}

Expand Down Expand Up @@ -314,7 +315,7 @@ impl From<PathBuf> for Rc<Path> {

impl AsRef<OsStr> for PathBuf {
fn as_ref(&self) -> &OsStr {
self.inner.as_ref()
self.0.as_ref()
}
}

Expand Down Expand Up @@ -355,18 +356,18 @@ impl<'b, P: AsRef<Path> + 'b> FromStream<P> for PathBuf {

impl From<std::path::PathBuf> for PathBuf {
fn from(path: std::path::PathBuf) -> PathBuf {
PathBuf { inner: path }
PathBuf(path)
}
}

impl Into<std::path::PathBuf> for PathBuf {
fn into(self) -> std::path::PathBuf {
self.inner
self.0
}
}

impl AsRef<std::path::Path> for PathBuf {
fn as_ref(&self) -> &std::path::Path {
self.inner.as_ref()
self.0.as_ref()
}
}