forked from mirror/async-std
Cleanup: replace cfg-if with our macros (#361)
* Cleanup: replace cfg-if with our macros * Prefix macros with cfg_ * Remove #[macro_export] from internal macrosyoshuawuyts-patch-1
parent
46f0fb1c64
commit
ec23632f3e
@ -1,86 +1,84 @@
|
|||||||
use cfg_if::cfg_if;
|
cfg_not_docs! {
|
||||||
|
pub use std::fs::FileType;
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg_docs! {
|
||||||
|
/// The type of a file or directory.
|
||||||
|
///
|
||||||
|
/// A file type is returned by [`Metadata::file_type`].
|
||||||
|
///
|
||||||
|
/// Note that file types are mutually exclusive, i.e. at most one of methods [`is_dir`],
|
||||||
|
/// [`is_file`], and [`is_symlink`] can return `true`.
|
||||||
|
///
|
||||||
|
/// This type is a re-export of [`std::fs::FileType`].
|
||||||
|
///
|
||||||
|
/// [`Metadata::file_type`]: struct.Metadata.html#method.file_type
|
||||||
|
/// [`is_dir`]: #method.is_dir
|
||||||
|
/// [`is_file`]: #method.is_file
|
||||||
|
/// [`is_symlink`]: #method.is_symlink
|
||||||
|
/// [`std::fs::FileType`]: https://doc.rust-lang.org/std/fs/struct.FileType.html
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
|
pub struct FileType {
|
||||||
|
_private: (),
|
||||||
|
}
|
||||||
|
|
||||||
cfg_if! {
|
impl FileType {
|
||||||
if #[cfg(feature = "docs")] {
|
/// Returns `true` if this file type represents a regular directory.
|
||||||
/// The type of a file or directory.
|
|
||||||
///
|
///
|
||||||
/// A file type is returned by [`Metadata::file_type`].
|
/// If this file type represents a symbolic link, this method returns `false`.
|
||||||
///
|
///
|
||||||
/// Note that file types are mutually exclusive, i.e. at most one of methods [`is_dir`],
|
/// # Examples
|
||||||
/// [`is_file`], and [`is_symlink`] can return `true`.
|
|
||||||
///
|
///
|
||||||
/// This type is a re-export of [`std::fs::FileType`].
|
/// ```no_run
|
||||||
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
||||||
|
/// #
|
||||||
|
/// use async_std::fs;
|
||||||
///
|
///
|
||||||
/// [`Metadata::file_type`]: struct.Metadata.html#method.file_type
|
/// let file_type = fs::metadata(".").await?.file_type();
|
||||||
/// [`is_dir`]: #method.is_dir
|
/// println!("{:?}", file_type.is_dir());
|
||||||
/// [`is_file`]: #method.is_file
|
/// #
|
||||||
/// [`is_symlink`]: #method.is_symlink
|
/// # Ok(()) }) }
|
||||||
/// [`std::fs::FileType`]: https://doc.rust-lang.org/std/fs/struct.FileType.html
|
/// ```
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
pub fn is_dir(&self) -> bool {
|
||||||
pub struct FileType {
|
unimplemented!()
|
||||||
_private: (),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileType {
|
/// Returns `true` if this file type represents a regular file.
|
||||||
/// Returns `true` if this file type represents a regular directory.
|
///
|
||||||
///
|
/// If this file type represents a symbolic link, this method returns `false`.
|
||||||
/// If this file type represents a symbolic link, this method returns `false`.
|
///
|
||||||
///
|
/// # Examples
|
||||||
/// # Examples
|
///
|
||||||
///
|
/// ```no_run
|
||||||
/// ```no_run
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
||||||
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
/// #
|
||||||
/// #
|
/// use async_std::fs;
|
||||||
/// use async_std::fs;
|
///
|
||||||
///
|
/// let file_type = fs::metadata("a.txt").await?.file_type();
|
||||||
/// let file_type = fs::metadata(".").await?.file_type();
|
/// println!("{:?}", file_type.is_file());
|
||||||
/// println!("{:?}", file_type.is_dir());
|
/// #
|
||||||
/// #
|
/// # Ok(()) }) }
|
||||||
/// # Ok(()) }) }
|
/// ```
|
||||||
/// ```
|
pub fn is_file(&self) -> bool {
|
||||||
pub fn is_dir(&self) -> bool {
|
unimplemented!()
|
||||||
unimplemented!()
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `true` if this file type represents a regular file.
|
|
||||||
///
|
|
||||||
/// If this file type represents a symbolic link, this method returns `false`.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```no_run
|
|
||||||
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
|
||||||
/// #
|
|
||||||
/// use async_std::fs;
|
|
||||||
///
|
|
||||||
/// let file_type = fs::metadata("a.txt").await?.file_type();
|
|
||||||
/// println!("{:?}", file_type.is_file());
|
|
||||||
/// #
|
|
||||||
/// # Ok(()) }) }
|
|
||||||
/// ```
|
|
||||||
pub fn is_file(&self) -> bool {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `true` if this file type represents a symbolic link.
|
/// Returns `true` if this file type represents a symbolic link.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
||||||
/// #
|
/// #
|
||||||
/// use async_std::fs;
|
/// use async_std::fs;
|
||||||
///
|
///
|
||||||
/// let file_type = fs::metadata("a.txt").await?.file_type();
|
/// let file_type = fs::metadata("a.txt").await?.file_type();
|
||||||
/// println!("{:?}", file_type.is_symlink());
|
/// println!("{:?}", file_type.is_symlink());
|
||||||
/// #
|
/// #
|
||||||
/// # Ok(()) }) }
|
/// # Ok(()) }) }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn is_symlink(&self) -> bool {
|
pub fn is_symlink(&self) -> bool {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
pub use std::fs::FileType;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,58 +1,56 @@
|
|||||||
use cfg_if::cfg_if;
|
cfg_not_docs! {
|
||||||
|
pub use std::fs::Permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg_docs! {
|
||||||
|
/// A set of permissions on a file or directory.
|
||||||
|
///
|
||||||
|
/// This type is a re-export of [`std::fs::Permissions`].
|
||||||
|
///
|
||||||
|
/// [`std::fs::Permissions`]: https://doc.rust-lang.org/std/fs/struct.Permissions.html
|
||||||
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||||
|
pub struct Permissions {
|
||||||
|
_private: (),
|
||||||
|
}
|
||||||
|
|
||||||
cfg_if! {
|
impl Permissions {
|
||||||
if #[cfg(feature = "docs")] {
|
/// Returns the read-only flag.
|
||||||
/// A set of permissions on a file or directory.
|
|
||||||
///
|
///
|
||||||
/// This type is a re-export of [`std::fs::Permissions`].
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// [`std::fs::Permissions`]: https://doc.rust-lang.org/std/fs/struct.Permissions.html
|
/// ```no_run
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
||||||
pub struct Permissions {
|
/// #
|
||||||
_private: (),
|
/// use async_std::fs;
|
||||||
|
///
|
||||||
|
/// let perm = fs::metadata("a.txt").await?.permissions();
|
||||||
|
/// println!("{:?}", perm.readonly());
|
||||||
|
/// #
|
||||||
|
/// # Ok(()) }) }
|
||||||
|
/// ```
|
||||||
|
pub fn readonly(&self) -> bool {
|
||||||
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Permissions {
|
/// Configures the read-only flag.
|
||||||
/// Returns the read-only flag.
|
///
|
||||||
///
|
/// [`fs::set_permissions`]: fn.set_permissions.html
|
||||||
/// # Examples
|
///
|
||||||
///
|
/// # Examples
|
||||||
/// ```no_run
|
///
|
||||||
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
/// ```no_run
|
||||||
/// #
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
||||||
/// use async_std::fs;
|
/// #
|
||||||
///
|
/// use async_std::fs;
|
||||||
/// let perm = fs::metadata("a.txt").await?.permissions();
|
///
|
||||||
/// println!("{:?}", perm.readonly());
|
/// let mut perm = fs::metadata("a.txt").await?.permissions();
|
||||||
/// #
|
/// perm.set_readonly(true);
|
||||||
/// # Ok(()) }) }
|
/// fs::set_permissions("a.txt", perm).await?;
|
||||||
/// ```
|
/// #
|
||||||
pub fn readonly(&self) -> bool {
|
/// # Ok(()) }) }
|
||||||
unimplemented!()
|
/// ```
|
||||||
}
|
pub fn set_readonly(&mut self, readonly: bool) {
|
||||||
|
unimplemented!()
|
||||||
/// Configures the read-only flag.
|
|
||||||
///
|
|
||||||
/// [`fs::set_permissions`]: fn.set_permissions.html
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```no_run
|
|
||||||
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
|
||||||
/// #
|
|
||||||
/// use async_std::fs;
|
|
||||||
///
|
|
||||||
/// let mut perm = fs::metadata("a.txt").await?.permissions();
|
|
||||||
/// perm.set_readonly(true);
|
|
||||||
/// fs::set_permissions("a.txt", perm).await?;
|
|
||||||
/// #
|
|
||||||
/// # Ok(()) }) }
|
|
||||||
/// ```
|
|
||||||
pub fn set_readonly(&mut self, readonly: bool) {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
pub use std::fs::Permissions;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
use crate::future::Future;
|
||||||
|
use crate::io::{self, Seek, SeekFrom};
|
||||||
|
use crate::task::{Context, Poll};
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[allow(missing_debug_implementations)]
|
||||||
|
pub struct SeekFuture<'a, T: Unpin + ?Sized> {
|
||||||
|
pub(crate) seeker: &'a mut T,
|
||||||
|
pub(crate) pos: SeekFrom,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Seek + Unpin + ?Sized> Future for SeekFuture<'_, T> {
|
||||||
|
type Output = io::Result<u64>;
|
||||||
|
|
||||||
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
let pos = self.pos;
|
||||||
|
Pin::new(&mut *self.seeker).poll_seek(cx, pos)
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
//! OS-specific extensions.
|
//! OS-specific extensions.
|
||||||
|
|
||||||
#[cfg(any(unix, feature = "docs"))]
|
cfg_unix! {
|
||||||
#[cfg_attr(feature = "docs", doc(cfg(unix)))]
|
pub mod unix;
|
||||||
pub mod unix;
|
}
|
||||||
|
|
||||||
#[cfg(any(windows, feature = "docs"))]
|
cfg_windows! {
|
||||||
#[cfg_attr(feature = "docs", doc(cfg(windows)))]
|
pub mod windows;
|
||||||
pub mod windows;
|
}
|
||||||
|
@ -1,56 +1,54 @@
|
|||||||
//! Unix-specific I/O extensions.
|
//! Unix-specific I/O extensions.
|
||||||
|
|
||||||
use cfg_if::cfg_if;
|
cfg_not_docs! {
|
||||||
|
pub use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||||
|
}
|
||||||
|
|
||||||
cfg_if! {
|
cfg_docs! {
|
||||||
if #[cfg(feature = "docs")] {
|
/// Raw file descriptors.
|
||||||
/// Raw file descriptors.
|
pub type RawFd = std::os::raw::c_int;
|
||||||
pub type RawFd = std::os::raw::c_int;
|
|
||||||
|
|
||||||
/// A trait to extract the raw unix file descriptor from an underlying
|
/// A trait to extract the raw unix file descriptor from an underlying
|
||||||
/// object.
|
/// object.
|
||||||
|
///
|
||||||
|
/// This is only available on unix platforms and must be imported in order
|
||||||
|
/// to call the method. Windows platforms have a corresponding `AsRawHandle`
|
||||||
|
/// and `AsRawSocket` set of traits.
|
||||||
|
pub trait AsRawFd {
|
||||||
|
/// Extracts the raw file descriptor.
|
||||||
///
|
///
|
||||||
/// This is only available on unix platforms and must be imported in order
|
/// This method does **not** pass ownership of the raw file descriptor
|
||||||
/// to call the method. Windows platforms have a corresponding `AsRawHandle`
|
/// to the caller. The descriptor is only guaranteed to be valid while
|
||||||
/// and `AsRawSocket` set of traits.
|
/// the original object has not yet been destroyed.
|
||||||
pub trait AsRawFd {
|
fn as_raw_fd(&self) -> RawFd;
|
||||||
/// Extracts the raw file descriptor.
|
}
|
||||||
///
|
|
||||||
/// This method does **not** pass ownership of the raw file descriptor
|
|
||||||
/// to the caller. The descriptor is only guaranteed to be valid while
|
|
||||||
/// the original object has not yet been destroyed.
|
|
||||||
fn as_raw_fd(&self) -> RawFd;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A trait to express the ability to construct an object from a raw file
|
/// A trait to express the ability to construct an object from a raw file
|
||||||
|
/// descriptor.
|
||||||
|
pub trait FromRawFd {
|
||||||
|
/// Constructs a new instance of `Self` from the given raw file
|
||||||
/// descriptor.
|
/// descriptor.
|
||||||
pub trait FromRawFd {
|
///
|
||||||
/// Constructs a new instance of `Self` from the given raw file
|
/// This function **consumes ownership** of the specified file
|
||||||
/// descriptor.
|
/// descriptor. The returned object will take responsibility for closing
|
||||||
///
|
/// it when the object goes out of scope.
|
||||||
/// This function **consumes ownership** of the specified file
|
///
|
||||||
/// descriptor. The returned object will take responsibility for closing
|
/// This function is also unsafe as the primitives currently returned
|
||||||
/// it when the object goes out of scope.
|
/// have the contract that they are the sole owner of the file
|
||||||
///
|
/// descriptor they are wrapping. Usage of this function could
|
||||||
/// This function is also unsafe as the primitives currently returned
|
/// accidentally allow violating this contract which can cause memory
|
||||||
/// have the contract that they are the sole owner of the file
|
/// unsafety in code that relies on it being true.
|
||||||
/// descriptor they are wrapping. Usage of this function could
|
unsafe fn from_raw_fd(fd: RawFd) -> Self;
|
||||||
/// accidentally allow violating this contract which can cause memory
|
}
|
||||||
/// unsafety in code that relies on it being true.
|
|
||||||
unsafe fn from_raw_fd(fd: RawFd) -> Self;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A trait to express the ability to consume an object and acquire ownership of
|
/// A trait to express the ability to consume an object and acquire ownership of
|
||||||
/// its raw file descriptor.
|
/// its raw file descriptor.
|
||||||
pub trait IntoRawFd {
|
pub trait IntoRawFd {
|
||||||
/// Consumes this object, returning the raw underlying file descriptor.
|
/// Consumes this object, returning the raw underlying file descriptor.
|
||||||
///
|
///
|
||||||
/// This function **transfers ownership** of the underlying file descriptor
|
/// This function **transfers ownership** of the underlying file descriptor
|
||||||
/// to the caller. Callers are then the unique owners of the file descriptor
|
/// to the caller. Callers are then the unique owners of the file descriptor
|
||||||
/// and must close the descriptor once it's no longer needed.
|
/// and must close the descriptor once it's no longer needed.
|
||||||
fn into_raw_fd(self) -> RawFd;
|
fn into_raw_fd(self) -> RawFd;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
pub use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,50 +1,48 @@
|
|||||||
//! Windows-specific I/O extensions.
|
//! Windows-specific I/O extensions.
|
||||||
|
|
||||||
use cfg_if::cfg_if;
|
cfg_not_docs! {
|
||||||
|
pub use std::os::windows::io::{
|
||||||
|
AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle, RawSocket,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
cfg_if! {
|
cfg_docs! {
|
||||||
if #[cfg(feature = "docs")] {
|
/// Raw HANDLEs.
|
||||||
/// Raw HANDLEs.
|
pub type RawHandle = *mut std::os::raw::c_void;
|
||||||
pub type RawHandle = *mut std::os::raw::c_void;
|
|
||||||
|
|
||||||
/// Raw SOCKETs.
|
/// Raw SOCKETs.
|
||||||
pub type RawSocket = u64;
|
pub type RawSocket = u64;
|
||||||
|
|
||||||
/// Extracts raw handles.
|
/// Extracts raw handles.
|
||||||
pub trait AsRawHandle {
|
pub trait AsRawHandle {
|
||||||
/// Extracts the raw handle, without taking any ownership.
|
/// Extracts the raw handle, without taking any ownership.
|
||||||
fn as_raw_handle(&self) -> RawHandle;
|
fn as_raw_handle(&self) -> RawHandle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct I/O objects from raw handles.
|
/// Construct I/O objects from raw handles.
|
||||||
pub trait FromRawHandle {
|
pub trait FromRawHandle {
|
||||||
/// Constructs a new I/O object from the specified raw handle.
|
/// Constructs a new I/O object from the specified raw handle.
|
||||||
///
|
///
|
||||||
/// This function will **consume ownership** of the handle given,
|
/// This function will **consume ownership** of the handle given,
|
||||||
/// passing responsibility for closing the handle to the returned
|
/// passing responsibility for closing the handle to the returned
|
||||||
/// object.
|
/// object.
|
||||||
///
|
///
|
||||||
/// This function is also unsafe as the primitives currently returned
|
/// This function is also unsafe as the primitives currently returned
|
||||||
/// have the contract that they are the sole owner of the file
|
/// have the contract that they are the sole owner of the file
|
||||||
/// descriptor they are wrapping. Usage of this function could
|
/// descriptor they are wrapping. Usage of this function could
|
||||||
/// accidentally allow violating this contract which can cause memory
|
/// accidentally allow violating this contract which can cause memory
|
||||||
/// unsafety in code that relies on it being true.
|
/// unsafety in code that relies on it being true.
|
||||||
unsafe fn from_raw_handle(handle: RawHandle) -> Self;
|
unsafe fn from_raw_handle(handle: RawHandle) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A trait to express the ability to consume an object and acquire ownership of
|
/// A trait to express the ability to consume an object and acquire ownership of
|
||||||
/// its raw `HANDLE`.
|
/// its raw `HANDLE`.
|
||||||
pub trait IntoRawHandle {
|
pub trait IntoRawHandle {
|
||||||
/// Consumes this object, returning the raw underlying handle.
|
/// Consumes this object, returning the raw underlying handle.
|
||||||
///
|
///
|
||||||
/// This function **transfers ownership** of the underlying handle to the
|
/// This function **transfers ownership** of the underlying handle to the
|
||||||
/// caller. Callers are then the unique owners of the handle and must close
|
/// caller. Callers are then the unique owners of the handle and must close
|
||||||
/// it once it's no longer needed.
|
/// it once it's no longer needed.
|
||||||
fn into_raw_handle(self) -> RawHandle;
|
fn into_raw_handle(self) -> RawHandle;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
pub use std::os::windows::io::{
|
|
||||||
AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle, RawSocket,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue