use std::ffi::{OsStr, OsString};
#[cfg(feature = "unstable")]
use std::pin::Pin;
use crate::path::Path;
#[cfg(feature = "unstable")]
use crate::prelude::*;
#[cfg(feature = "unstable")]
use crate::stream::{self, FromStream, IntoStream};
#[derive(Debug, PartialEq, Default)]
pub struct PathBuf {
inner: std::path::PathBuf,
}
impl PathBuf {
pub fn new() -> PathBuf {
std::path::PathBuf::new().into()
}
pub fn as_path(&self) -> &Path {
self.inner.as_path().into()
}
pub fn push<P: AsRef<Path>>(&mut self, path: P) {
self.inner.push(path.as_ref())
}
pub fn pop(&mut self) -> bool {
self.inner.pop()
}
pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
self.inner.set_file_name(file_name)
}
pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
self.inner.set_extension(extension)
}
pub fn into_os_string(self) -> OsString {
self.inner.into_os_string()
}
pub fn into_boxed_path(self) -> Box<Path> {
let rw = Box::into_raw(self.inner.into_boxed_path()) as *mut Path;
unsafe { Box::from_raw(rw) }
}
}
impl std::ops::Deref for PathBuf {
type Target = Path;
fn deref(&self) -> &Path {
self.as_ref()
}
}
impl std::borrow::Borrow<Path> for PathBuf {
fn borrow(&self) -> &Path {
&**self
}
}
impl From<std::path::PathBuf> for PathBuf {
fn from(path: std::path::PathBuf) -> PathBuf {
PathBuf { inner: path }
}
}
impl Into<std::path::PathBuf> for PathBuf {
fn into(self) -> std::path::PathBuf {
self.inner
}
}
impl From<OsString> for PathBuf {
fn from(path: OsString) -> PathBuf {
std::path::PathBuf::from(path).into()
}
}
impl From<&str> for PathBuf {
fn from(path: &str) -> PathBuf {
std::path::PathBuf::from(path).into()
}
}
impl AsRef<Path> for PathBuf {
fn as_ref(&self) -> &Path {
Path::new(&self.inner)
}
}
impl AsRef<std::path::Path> for PathBuf {
fn as_ref(&self) -> &std::path::Path {
self.inner.as_ref()
}
}
#[cfg(feature = "unstable")]
impl<P: AsRef<Path>> stream::Extend<P> for PathBuf {
fn extend<'a, S: IntoStream<Item = P>>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>>
where
P: 'a,
<S as IntoStream>::IntoStream: 'a,
{
let stream = stream.into_stream();
Box::pin(stream.for_each(move |item| self.push(item.as_ref())))
}
}
#[cfg(feature = "unstable")]
impl<'b, P: AsRef<Path> + 'b> FromStream<P> for PathBuf {
#[inline]
fn from_stream<'a, S: IntoStream<Item = P>>(
stream: S,
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
where
<S as IntoStream>::IntoStream: 'a,
{
let stream = stream.into_stream();
Box::pin(async move {
pin_utils::pin_mut!(stream);
let mut out = Self::new();
stream::extend(&mut out, stream).await;
out
})
}
}