Skip to content

Commit

Permalink
Use autocfg instead of a feature for try_exists
Browse files Browse the repository at this point in the history
Per comments on #48 this moves from gating `try_exists` behind a feature to using `autocfg`. This is a build time library that inspects the current runtime and adds flags to the build so if a user is building with Rust 1.63 or higher then this feature will be enabled.
  • Loading branch information
Richard Schneeman committed Nov 12, 2023
1 parent 93a270e commit 4afa6f8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ jobs:
args: --tests -- -D warnings
if: matrix.rust_version == 'stable'

- name: cargo check --features "io_safety path_try_exists"
- name: cargo check --features "io_safety"
uses: actions-rs/cargo@v1
with:
command: clippy
args: --features "io_safety path_try_exists"
args: --features "io_safety"
if: matrix.rust_version == 'stable'
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# fs-err Changelog

* Add `fs_err_try_exists` to `std::path::Path` via extension trait. This feature requires Rust 1.63 or later and is gated behind the `path_try_exists` feature flag. ([#48](https://github.com/andrewhickman/fs-err/pull/48))
* Add `fs_err_try_exists` to `std::path::Path` via extension trait. This feature requires Rust 1.63 or later. ([#48](https://github.com/andrewhickman/fs-err/pull/48))

## 2.9.0

Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ exclude = [".github", ".gitignore", "README.tpl"]
[dependencies]
tokio = { version = "1.21", optional = true, default_features = false, features = ["fs"] }

[build-dependencies]
autocfg = "1"

[dev-dependencies]
serde_json = "1.0.64"

[features]
# Adds `fs_err_try_exists` to `Path`, introduced in Rust 1.6.3
path_try_exists = []
# Adds I/O safety traits, introduced in Rust 1.63
io_safety = []

Expand Down
7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern crate autocfg;

fn main() {
let ac = autocfg::new();
// Allows `#[cfg(rustc_1_63)]` to be used in code
ac.emit_rustc_version(1, 63);
}
4 changes: 2 additions & 2 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
// Because no one else can implement it, we can add methods backwards-compatibly.
pub trait PathExt: crate::Sealed {
/// Wrapper for [`Path::try_exists`](https://doc.rust-lang.org/std/path/struct.Path.html#method.try_exists).
#[cfg(feature = "path_try_exists")]
#[cfg(rustc_1_63)]
fn fs_err_try_exists(&self) -> io::Result<bool>;
/// Wrapper for [`crate::metadata`].
fn fs_err_metadata(&self) -> io::Result<fs::Metadata>;
Expand All @@ -26,7 +26,7 @@ pub trait PathExt: crate::Sealed {
}

impl PathExt for Path {
#[cfg(feature = "path_try_exists")]
#[cfg(rustc_1_63)]
fn fs_err_try_exists(&self) -> io::Result<bool> {
self.try_exists()
.map_err(|source| Error::build(source, ErrorKind::FileExists, self))
Expand Down

0 comments on commit 4afa6f8

Please sign in to comment.