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

Use custom traits instead of libstd's MetadataExt and PermissionsExt. #343

Merged
merged 6 commits into from
Jan 10, 2024
Merged
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
3 changes: 3 additions & 0 deletions cap-async-std/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub use cap_primitives::fs::{DirBuilder, FileType, Metadata, OpenOptions, Permis
// Re-export conditional types from `cap_primitives`.
#[cfg(any(unix, target_os = "vxworks", all(windows, windows_file_type_ext)))]
pub use cap_primitives::fs::FileTypeExt;
pub use cap_primitives::fs::MetadataExt;
#[cfg(unix)]
pub use cap_primitives::fs::PermissionsExt;

// Re-export things from `async_std` that we can use as-is.
#[cfg(target_os = "wasi")]
Expand Down
3 changes: 3 additions & 0 deletions cap-async-std/src/fs_utf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub use crate::fs::{DirBuilder, FileType, Metadata, OpenOptions, Permissions};
// Re-export conditional types from `cap_primitives`.
#[cfg(any(unix, target_os = "vxworks", all(windows, windows_file_type_ext)))]
pub use cap_primitives::fs::FileTypeExt;
pub use cap_primitives::fs::MetadataExt;
#[cfg(unix)]
pub use cap_primitives::fs::PermissionsExt;

// Re-export `camino` to make it easy for users to depend on the same
// version we do, because we use its types in our public API.
Expand Down
50 changes: 45 additions & 5 deletions cap-fs-ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,60 @@ fn use_feature(feature: &str) {

/// Test whether the rustc at `var("RUSTC")` supports the given feature.
fn has_feature(feature: &str) -> bool {
can_compile(&format!(
"#![allow(stable_features)]\n#![feature({})]",
feature
))
}

/// Test whether the rustc at `var("RUSTC")` can compile the given code.
fn can_compile<T: AsRef<str>>(test: T) -> bool {
use std::process::Stdio;

let out_dir = var("OUT_DIR").unwrap();
let rustc = var("RUSTC").unwrap();
let target = var("TARGET").unwrap();

// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string,
// as documented [here].
// [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
let wrapper = var("RUSTC_WRAPPER")
.ok()
.and_then(|w| if w.is_empty() { None } else { Some(w) });

let mut child = std::process::Command::new(rustc)
.arg("--crate-type=rlib") // Don't require `main`.
let mut cmd = if let Some(wrapper) = wrapper {
let mut cmd = std::process::Command::new(wrapper);
// The wrapper's first argument is supposed to be the path to rustc.
cmd.arg(rustc);
cmd
} else {
std::process::Command::new(rustc)
};

cmd.arg("--crate-type=rlib") // Don't require `main`.
.arg("--emit=metadata") // Do as little as possible but still parse.
.arg("--target")
.arg(target)
.arg("--out-dir")
.arg(out_dir) // Put the output somewhere inconsequential.
.arg(out_dir); // Put the output somewhere inconsequential.

// If Cargo wants to set RUSTFLAGS, use that.
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
if !rustflags.is_empty() {
for arg in rustflags.split('\x1f') {
cmd.arg(arg);
}
}
}

let mut child = cmd
.arg("-") // Read from stdin.
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
.stdin(Stdio::piped()) // Stdin is a pipe.
.stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing.
.spawn()
.unwrap();

writeln!(child.stdin.take().unwrap(), "#![feature({})]", feature).unwrap();
writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap();

child.wait().unwrap().success()
}
6 changes: 3 additions & 3 deletions cap-fs-ext/src/metadata_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ impl MetadataExt for std::fs::Metadata {
impl MetadataExt for cap_primitives::fs::Metadata {
#[inline]
fn dev(&self) -> u64 {
std::os::unix::fs::MetadataExt::dev(self)
cap_primitives::fs::MetadataExt::dev(self)
}

#[inline]
fn ino(&self) -> u64 {
std::os::unix::fs::MetadataExt::ino(self)
cap_primitives::fs::MetadataExt::ino(self)
}

#[inline]
fn nlink(&self) -> u64 {
std::os::unix::fs::MetadataExt::nlink(self)
cap_primitives::fs::MetadataExt::nlink(self)
}
}

Expand Down
50 changes: 45 additions & 5 deletions cap-primitives/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,60 @@ fn use_feature(feature: &str) {

/// Test whether the rustc at `var("RUSTC")` supports the given feature.
fn has_feature(feature: &str) -> bool {
can_compile(&format!(
"#![allow(stable_features)]\n#![feature({})]",
feature
))
}

/// Test whether the rustc at `var("RUSTC")` can compile the given code.
fn can_compile<T: AsRef<str>>(test: T) -> bool {
use std::process::Stdio;

let out_dir = var("OUT_DIR").unwrap();
let rustc = var("RUSTC").unwrap();
let target = var("TARGET").unwrap();

// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string,
// as documented [here].
// [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
let wrapper = var("RUSTC_WRAPPER")
.ok()
.and_then(|w| if w.is_empty() { None } else { Some(w) });

let mut child = std::process::Command::new(rustc)
.arg("--crate-type=rlib") // Don't require `main`.
let mut cmd = if let Some(wrapper) = wrapper {
let mut cmd = std::process::Command::new(wrapper);
// The wrapper's first argument is supposed to be the path to rustc.
cmd.arg(rustc);
cmd
} else {
std::process::Command::new(rustc)
};

cmd.arg("--crate-type=rlib") // Don't require `main`.
.arg("--emit=metadata") // Do as little as possible but still parse.
.arg("--target")
.arg(target)
.arg("--out-dir")
.arg(out_dir) // Put the output somewhere inconsequential.
.arg(out_dir); // Put the output somewhere inconsequential.

// If Cargo wants to set RUSTFLAGS, use that.
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
if !rustflags.is_empty() {
for arg in rustflags.split('\x1f') {
cmd.arg(arg);
}
}
}

let mut child = cmd
.arg("-") // Read from stdin.
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
.stdin(Stdio::piped()) // Stdin is a pipe.
.stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing.
.spawn()
.unwrap();

writeln!(child.stdin.take().unwrap(), "#![feature({})]", feature).unwrap();
writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap();

child.wait().unwrap().success()
}
Loading