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

Implementing support for async_std types #26

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ edition = "2018"

[dependencies]
stable_deref_trait = { version = "1.0", optional = true }
async-std = { version = "1", features = ["attributes"] ,optional = true }

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[lib]
doctest = false
Copy link
Author

@gabrik gabrik Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added to avoid testing the documentation examples.
When testing the async feature the documentation examples fail, this is why I disabled it.


[dev-dependencies]
tempdir = "0.3"
owning_ref = "0.4.1"

gabrik marked this conversation as resolved.
Show resolved Hide resolved
[features]
default = []
async = ["async-std"]
15 changes: 12 additions & 3 deletions examples/cat.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#[cfg(not(feature = "async"))]
extern crate memmap2;

#[cfg(not(feature = "async"))]
use memmap2::Mmap;
#[cfg(not(feature = "async"))]
use std::env;
#[cfg(not(feature = "async"))]
use std::fs::File;
#[cfg(not(feature = "async"))]
use std::io::{self, Write};

use memmap2::Mmap;

/// Output a file's contents to stdout. The file path must be provided as the first process
/// argument.
#[cfg(not(feature = "async"))]
fn main() {
let path = env::args()
.nth(1)
Expand All @@ -21,3 +25,8 @@ fn main() {
.write_all(&mmap[..])
.expect("failed to output the file contents");
}

#[cfg(feature = "async")]
fn main() {
println!("Nothing to do")
}
62 changes: 62 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,116 @@ use crate::stub::file_len;
use crate::stub::MmapInner;

use std::fmt;
#[cfg(not(feature = "async"))]
use std::fs::File;

use std::io::{Error, ErrorKind, Result};
use std::ops::{Deref, DerefMut};
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
use std::slice;
use std::usize;

#[cfg(feature = "async")]
#[cfg(windows)]
pub struct MmapRawDescriptor(async_std::os::windows::io::RawHandle);

#[cfg(not(feature = "async"))]
#[cfg(windows)]
pub struct MmapRawDescriptor<'a>(&'a File);

#[cfg(not(feature = "async"))]
#[cfg(unix)]
pub struct MmapRawDescriptor(std::os::unix::io::RawFd);

#[cfg(feature = "async")]
#[cfg(unix)]
pub struct MmapRawDescriptor(async_std::os::unix::io::RawFd);

#[cfg(not(feature = "async"))]
#[cfg(not(any(unix, windows)))]
pub struct MmapRawDescriptor<'a>(&'a File);

#[cfg(feature = "async")]
#[cfg(not(any(unix, windows)))]
pub struct MmapRawDescriptor<'a>(&'a async_std::fs::File);

pub trait MmapAsRawDesc {
fn as_raw_desc(&self) -> MmapRawDescriptor;
}

#[cfg(not(feature = "async"))]
#[cfg(windows)]
impl MmapAsRawDesc for &File {
fn as_raw_desc(&self) -> MmapRawDescriptor {
MmapRawDescriptor(self)
}
}

#[cfg(feature = "async")]
#[cfg(windows)]
impl MmapAsRawDesc for &async_std::fs::File {
fn as_raw_desc(&self) -> MmapRawDescriptor {
MmapRawDescriptor(self.as_raw_handle())
}
}

#[cfg(feature = "async")]
#[cfg(windows)]
impl MmapAsRawDesc for async_std::os::windows::io::RawHandle {
fn as_raw_desc(&self) -> MmapRawDescriptor {
MmapRawDescriptor(*self)
}
}

#[cfg(feature = "async")]
#[cfg(unix)]
impl MmapAsRawDesc for &async_std::fs::File {
fn as_raw_desc(&self) -> MmapRawDescriptor {
MmapRawDescriptor(self.as_raw_fd())
}
}

#[cfg(not(feature = "async"))]
#[cfg(unix)]
impl MmapAsRawDesc for &File {
fn as_raw_desc(&self) -> MmapRawDescriptor {
MmapRawDescriptor(self.as_raw_fd())
}
}

#[cfg(not(feature = "async"))]
#[cfg(unix)]
impl MmapAsRawDesc for std::os::unix::io::RawFd {
fn as_raw_desc(&self) -> MmapRawDescriptor {
MmapRawDescriptor(*self)
}
}

#[cfg(feature = "async")]
#[cfg(unix)]
impl MmapAsRawDesc for async_std::os::unix::io::RawFd {
fn as_raw_desc(&self) -> MmapRawDescriptor {
MmapRawDescriptor(*self)
}
}

#[cfg(not(feature = "async"))]
#[cfg(not(any(unix, windows)))]
impl MmapAsRawDesc for &File {
fn as_raw_desc(&self) -> MmapRawDescriptor {
MmapRawDescriptor(self)
}
}

#[cfg(feature = "async")]
#[cfg(not(any(unix, windows)))]
impl MmapAsRawDesc for &async_std::fs::File {
fn as_raw_desc(&self) -> MmapRawDescriptor {
MmapRawDescriptor(self)
}
}

/// A memory map builder, providing advanced options and flags for specifying memory map behavior.
///
/// `MmapOptions` can be used to create an anonymous memory map using [`map_anon()`], or a
Expand Down Expand Up @@ -900,6 +961,7 @@ impl fmt::Debug for MmapMut {
}
}

#[cfg(not(feature = "async"))]
#[cfg(test)]
mod test {
extern crate tempdir;
Expand Down
Loading