Skip to content

Commit c3e03fd

Browse files
authored
Fix unexpected_cfgs warnings. (#358)
* Fix unexpected_cfgs warnings. Fix warnings about cfgs that aren't declared, by declaring the cfgs we do use in build.rs files. And fix a bug this turned up, around the usage of unix_file_vectored_at. * Fix an unexpected_cfg warning. * Use `cargo:` instead of `cargo::` for broader compatibility. * Fix `cfg(feature = "async_std")` in cap-fs-ext. * Fix the test to be independent of the expected error message.
1 parent 68cefe7 commit c3e03fd

File tree

13 files changed

+87
-50
lines changed

13 files changed

+87
-50
lines changed

build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ fn main() {
1717
// https://doc.rust-lang.org/unstable-book/library-features/windows-file-type-ext.html
1818
use_feature_or_nothing("windows_file_type_ext");
1919

20+
// Cfgs that users may set.
21+
println!("cargo:rustc-check-cfg=cfg(racy_asserts)");
22+
2023
// Don't rerun this on changes other than build.rs, as we only depend on
2124
// the rustc version.
2225
println!("cargo:rerun-if-changed=build.rs");
@@ -26,6 +29,7 @@ fn use_feature_or_nothing(feature: &str) {
2629
if has_feature(feature) {
2730
use_feature(feature);
2831
}
32+
println!("cargo:rustc-check-cfg=cfg({})", feature);
2933
}
3034

3135
fn use_feature(feature: &str) {

cap-fs-ext/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ default = ["std"]
2828
fs_utf8 = ["cap-std/fs_utf8", "camino"]
2929
arf_strings = ["cap-std/arf_strings", "fs_utf8", "arf-strings"]
3030
std = ["cap-std"]
31+
# Temporarily disable cap-async-std.
3132
#async_std = ["cap-async-std", "async-std", "io-lifetimes/async-std", "async-trait"]
3233
#async_std_fs_utf8 = ["cap-async-std/fs_utf8", "camino"]
3334
#async_std_arf_strings = ["cap-async-std/arf_strings", "async_std_fs_utf8", "arf-strings"]

cap-fs-ext/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn use_feature_or_nothing(feature: &str) {
1313
if has_feature(feature) {
1414
use_feature(feature);
1515
}
16+
println!("cargo:rustc-check-cfg=cfg({})", feature);
1617
}
1718

1819
fn use_feature(feature: &str) {

cap-fs-ext/src/dir_ext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,13 +1393,13 @@ impl AsyncDirExtUtf8 for cap_async_std::fs_utf8::Dir {
13931393

13941394
#[cfg(all(any(feature = "std", feature = "async_std"), feature = "fs_utf8"))]
13951395
#[cfg(not(feature = "arf_strings"))]
1396-
fn from_utf8<'a>(path: &'a Utf8Path) -> std::io::Result<&'a std::path::Path> {
1396+
fn from_utf8<'a>(path: &'a Utf8Path) -> io::Result<&'a std::path::Path> {
13971397
Ok(path.as_std_path())
13981398
}
13991399

14001400
#[cfg(all(any(feature = "std", feature = "async_std"), feature = "fs_utf8"))]
14011401
#[cfg(feature = "arf_strings")]
1402-
fn from_utf8<'a>(path: &'a Utf8Path) -> std::io::Result<std::path::PathBuf> {
1402+
fn from_utf8<'a>(path: &'a Utf8Path) -> io::Result<std::path::PathBuf> {
14031403
#[cfg(not(windows))]
14041404
let path = {
14051405
#[cfg(unix)]

cap-fs-ext/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#![doc(
1010
html_favicon_url = "https://raw.githubusercontent.com/bytecodealliance/cap-std/main/media/cap-std.ico"
1111
)]
12+
// Allow cfg(feature = "async_std") even though it isn't a feature. async_std
13+
// support is temporarily disabled.
14+
#![allow(unexpected_cfgs)]
1215

1316
mod dir_entry_ext;
1417
mod dir_ext;

cap-primitives/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ fn main() {
99
use_feature_or_nothing("io_error_more"); // https://github.com/rust-lang/rust/issues/86442
1010
use_feature_or_nothing("io_error_uncategorized");
1111

12+
// Cfgs that users may set.
13+
println!("cargo:rustc-check-cfg=cfg(racy_asserts)");
14+
println!("cargo:rustc-check-cfg=cfg(emulate_second_only_system)");
15+
println!("cargo:rustc-check-cfg=cfg(io_lifetimes_use_std)");
16+
1217
// Don't rerun this on changes other than build.rs, as we only depend on
1318
// the rustc version.
1419
println!("cargo:rerun-if-changed=build.rs");
@@ -18,6 +23,7 @@ fn use_feature_or_nothing(feature: &str) {
1823
if has_feature(feature) {
1924
use_feature(feature);
2025
}
26+
println!("cargo:rustc-check-cfg=cfg({})", feature);
2127
}
2228

2329
fn use_feature(feature: &str) {

cap-primitives/src/fs/file.rs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ pub trait FileExt {
77
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>;
88

99
/// Like `read_at`, except that it reads into a slice of buffers.
10-
#[cfg(feature = "unix_file_vectored_at")]
11-
fn read_vectored_at(
12-
&self,
13-
bufs: &mut [std::io::IoSliceMut<'_>],
14-
offset: u64,
15-
) -> io::Result<usize> {
16-
io::default_read_vectored(|b| self.read_at(b, offset), bufs)
10+
#[cfg(unix_file_vectored_at)]
11+
fn read_vectored_at(&self, bufs: &mut [io::IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
12+
default_read_vectored(|b| self.read_at(b, offset), bufs)
1713
}
1814

1915
/// Reads the exact number of bytes required to fill `buf` from the given offset.
@@ -44,9 +40,9 @@ pub trait FileExt {
4440
fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
4541

4642
/// Like `write_at`, except that it writes from a slice of buffers.
47-
#[cfg(feature = "unix_file_vectored_at")]
48-
fn write_vectored_at(&self, bufs: &[std::io::IoSlice<'_>], offset: u64) -> io::Result<usize> {
49-
io::default_write_vectored(|b| self.write_at(b, offset), bufs)
43+
#[cfg(unix_file_vectored_at)]
44+
fn write_vectored_at(&self, bufs: &[io::IoSlice<'_>], offset: u64) -> io::Result<usize> {
45+
default_write_vectored(|b| self.write_at(b, offset), bufs)
5046
}
5147

5248
/// Attempts to write an entire buffer starting from a given offset.
@@ -71,21 +67,41 @@ pub trait FileExt {
7167
}
7268
}
7369

70+
#[cfg(unix_file_vectored_at)]
71+
fn default_read_vectored<F>(read: F, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize>
72+
where
73+
F: FnOnce(&mut [u8]) -> io::Result<usize>,
74+
{
75+
let buf = bufs
76+
.iter_mut()
77+
.find(|b| !b.is_empty())
78+
.map_or(&mut [][..], |b| &mut **b);
79+
read(buf)
80+
}
81+
82+
#[cfg(unix_file_vectored_at)]
83+
fn default_write_vectored<F>(write: F, bufs: &[io::IoSlice<'_>]) -> io::Result<usize>
84+
where
85+
F: FnOnce(&[u8]) -> io::Result<usize>,
86+
{
87+
let buf = bufs
88+
.iter()
89+
.find(|b| !b.is_empty())
90+
.map_or(&[][..], |b| &**b);
91+
write(buf)
92+
}
93+
7494
/// WASI-specific extensions to [`fs::File`].
7595
#[cfg(target_os = "wasi")]
7696
pub trait FileExt {
7797
/// Reads a number of bytes starting from a given offset.
7898
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
79-
let bufs = &mut [std::io::IoSliceMut::new(buf)];
99+
let bufs = &mut [io::IoSliceMut::new(buf)];
80100
self.read_vectored_at(bufs, offset)
81101
}
82102

83103
/// Reads a number of bytes starting from a given offset.
84-
fn read_vectored_at(
85-
&self,
86-
bufs: &mut [std::io::IoSliceMut<'_>],
87-
offset: u64,
88-
) -> io::Result<usize>;
104+
fn read_vectored_at(&self, bufs: &mut [io::IoSliceMut<'_>], offset: u64) -> io::Result<usize>;
89105

90106
/// Reads the exact number of byte required to fill `buf` from the given offset.
91107
fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> {
@@ -113,12 +129,12 @@ pub trait FileExt {
113129

114130
/// Writes a number of bytes starting from a given offset.
115131
fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
116-
let bufs = &[std::io::IoSlice::new(buf)];
132+
let bufs = &[io::IoSlice::new(buf)];
117133
self.write_vectored_at(bufs, offset)
118134
}
119135

120136
/// Writes a number of bytes starting from a given offset.
121-
fn write_vectored_at(&self, bufs: &[std::io::IoSlice<'_>], offset: u64) -> io::Result<usize>;
137+
fn write_vectored_at(&self, bufs: &[io::IoSlice<'_>], offset: u64) -> io::Result<usize>;
122138

123139
/// Attempts to write an entire buffer starting from a given offset.
124140
fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> {
@@ -145,19 +161,19 @@ pub trait FileExt {
145161
fn tell(&self) -> io::Result<u64>;
146162

147163
/// Adjust the flags associated with this file.
148-
fn fdstat_set_flags(&self, flags: u16) -> std::io::Result<()>;
164+
fn fdstat_set_flags(&self, flags: u16) -> io::Result<()>;
149165

150166
/// Adjust the rights associated with this file.
151-
fn fdstat_set_rights(&self, rights: u64, inheriting: u64) -> std::io::Result<()>;
167+
fn fdstat_set_rights(&self, rights: u64, inheriting: u64) -> io::Result<()>;
152168

153169
/// Provide file advisory information on a file descriptor.
154-
fn advise(&self, offset: u64, len: u64, advice: u8) -> std::io::Result<()>;
170+
fn advise(&self, offset: u64, len: u64, advice: u8) -> io::Result<()>;
155171

156172
/// Force the allocation of space in a file.
157-
fn allocate(&self, offset: u64, len: u64) -> std::io::Result<()>;
173+
fn allocate(&self, offset: u64, len: u64) -> io::Result<()>;
158174

159175
/// Create a directory.
160-
fn create_directory<P: AsRef<std::path::Path>>(&self, dir: P) -> std::io::Result<()>;
176+
fn create_directory<P: AsRef<std::path::Path>>(&self, dir: P) -> io::Result<()>;
161177

162178
/// Read the contents of a symbolic link.
163179
fn read_link<P: AsRef<std::path::Path>>(&self, path: P) -> io::Result<std::path::PathBuf>;

cap-std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ edition = "2021"
1414

1515
[package.metadata.docs.rs]
1616
all-features = true
17-
rustdoc-args = ["--cfg=doc_cfg"]
17+
rustdoc-args = ["--cfg=docsrs"]
1818

1919
[dependencies]
2020
arf-strings = { version = "0.7.0", optional = true }

cap-std/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use std::io::Write;
44
fn main() {
55
use_feature_or_nothing("can_vector"); // https://github.com/rust-lang/rust/issues/69941
66
use_feature_or_nothing("write_all_vectored"); // https://github.com/rust-lang/rust/issues/70436
7+
use_feature_or_nothing("windows_file_type_ext");
8+
9+
// Cfgs that users may set.
10+
println!("cargo:rustc-check-cfg=cfg(io_lifetimes_use_std)");
711

812
// Don't rerun this on changes other than build.rs, as we only depend on
913
// the rustc version.
@@ -14,6 +18,7 @@ fn use_feature_or_nothing(feature: &str) {
1418
if has_feature(feature) {
1519
use_feature(feature);
1620
}
21+
println!("cargo:rustc-check-cfg=cfg({})", feature);
1722
}
1823

1924
fn use_feature(feature: &str) {

cap-std/src/fs/file.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,12 @@ impl crate::fs::FileExt for File {
524524
}
525525

526526
#[inline]
527-
fn tell(&self) -> std::result::Result<u64, std::io::Error> {
527+
fn tell(&self) -> std::result::Result<u64, io::Error> {
528528
std::os::wasi::fs::FileExt::tell(&self.std)
529529
}
530530

531531
#[inline]
532-
fn fdstat_set_flags(&self, flags: u16) -> std::result::Result<(), std::io::Error> {
532+
fn fdstat_set_flags(&self, flags: u16) -> std::result::Result<(), io::Error> {
533533
std::os::wasi::fs::FileExt::fdstat_set_flags(&self.std, flags)
534534
}
535535

@@ -538,30 +538,30 @@ impl crate::fs::FileExt for File {
538538
&self,
539539
rights: u64,
540540
inheriting: u64,
541-
) -> std::result::Result<(), std::io::Error> {
541+
) -> std::result::Result<(), io::Error> {
542542
std::os::wasi::fs::FileExt::fdstat_set_rights(&self.std, rights, inheriting)
543543
}
544544

545545
#[inline]
546-
fn advise(&self, offset: u64, len: u64, advice: u8) -> std::result::Result<(), std::io::Error> {
546+
fn advise(&self, offset: u64, len: u64, advice: u8) -> std::result::Result<(), io::Error> {
547547
std::os::wasi::fs::FileExt::advise(&self.std, offset, len, advice)
548548
}
549549

550550
#[inline]
551-
fn allocate(&self, offset: u64, len: u64) -> std::result::Result<(), std::io::Error> {
551+
fn allocate(&self, offset: u64, len: u64) -> std::result::Result<(), io::Error> {
552552
std::os::wasi::fs::FileExt::allocate(&self.std, offset, len)
553553
}
554554

555555
#[inline]
556-
fn create_directory<P: AsRef<Path>>(&self, dir: P) -> std::result::Result<(), std::io::Error> {
556+
fn create_directory<P: AsRef<Path>>(&self, dir: P) -> std::result::Result<(), io::Error> {
557557
std::os::wasi::fs::FileExt::create_directory(&self.std, dir)
558558
}
559559

560560
#[inline]
561561
fn read_link<P: AsRef<Path>>(
562562
&self,
563563
path: P,
564-
) -> std::result::Result<std::path::PathBuf, std::io::Error> {
564+
) -> std::result::Result<std::path::PathBuf, io::Error> {
565565
std::os::wasi::fs::FileExt::read_link(&self.std, path)
566566
}
567567

@@ -570,17 +570,17 @@ impl crate::fs::FileExt for File {
570570
&self,
571571
lookup_flags: u32,
572572
path: P,
573-
) -> std::result::Result<std::fs::Metadata, std::io::Error> {
573+
) -> std::result::Result<std::fs::Metadata, io::Error> {
574574
std::os::wasi::fs::FileExt::metadata_at(&self.std, lookup_flags, path)
575575
}
576576

577577
#[inline]
578-
fn remove_file<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), std::io::Error> {
578+
fn remove_file<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), io::Error> {
579579
std::os::wasi::fs::FileExt::remove_file(&self.std, path)
580580
}
581581

582582
#[inline]
583-
fn remove_directory<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), std::io::Error> {
583+
fn remove_directory<P: AsRef<Path>>(&self, path: P) -> std::result::Result<(), io::Error> {
584584
std::os::wasi::fs::FileExt::remove_directory(&self.std, path)
585585
}
586586
}

0 commit comments

Comments
 (0)