forked from mirror/async-std
Compare commits
6 commits
master
...
poc-serde-
Author | SHA1 | Date | |
---|---|---|---|
|
c70f00e915 | ||
|
3c92ffeb5c | ||
|
6b0a81012b | ||
|
c5a757fb60 | ||
|
bcc134cd02 | ||
|
4e88f104c5 |
3 changed files with 32 additions and 17 deletions
|
@ -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"
|
||||
|
|
12
src/lib.rs
12
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:
|
||||
//!
|
||||
|
|
|
@ -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<P: AsRef<Path>>(&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<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`.
|
||||
|
@ -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.
|
||||
|
@ -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<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) }
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<PathBuf> for Rc<Path> {
|
|||
|
||||
impl AsRef<OsStr> for PathBuf {
|
||||
fn as_ref(&self) -> &OsStr {
|
||||
self.inner.as_ref()
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue