From 51413606f0947b8e7cab3b9a993d18abd013eb5b Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 22 Aug 2025 10:20:44 +0000 Subject: [PATCH 1/4] Make unicode-width an optional default dependency --- Cargo.toml | 4 +++- src/lib.rs | 14 +++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 67c0e628..f0877c7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ edition = "2021" rust-version = "1.66" [dependencies] -unicode-width = "0.2.0" +unicode-width = { version = "0.2.0", optional = true } std = { version = "1.0", package = "rustc-std-workspace-std", optional = true } core = { version = "1.0", package = "rustc-std-workspace-core", optional = true } @@ -18,4 +18,6 @@ core = { version = "1.0", package = "rustc-std-workspace-core", optional = true log = "0.4" [features] +default = ["unicode"] rustc-dep-of-std = ["unicode-width/rustc-dep-of-std", "std", "core"] +unicode = ["dep:unicode-width"] diff --git a/src/lib.rs b/src/lib.rs index 0c47ce61..f5c8e3cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,7 +104,6 @@ #[cfg(test)] #[macro_use] extern crate log; -extern crate unicode_width; use self::Fail::*; use self::HasArg::*; @@ -119,8 +118,21 @@ use std::iter::{repeat, IntoIterator}; use std::result; use std::str::FromStr; +#[cfg(feature = "unicode")] use unicode_width::UnicodeWidthStr; +#[cfg(not(feature = "unicode"))] +trait UnicodeWidthStr { + fn width(&self) -> usize; +} + +#[cfg(not(feature = "unicode"))] +impl UnicodeWidthStr for str { + fn width(&self) -> usize { + self.len() + } +} + #[cfg(test)] mod tests; From 38abcfcaaa252517f5b75f1258ec952747a1ade0 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 22 Aug 2025 10:25:36 +0000 Subject: [PATCH 2/4] Fix warning on current rustc versions --- src/tests/mod.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 4a040871..865c3280 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -50,9 +50,9 @@ fn test_reqopt() { let short_args = vec!["-t".to_string(), "20".to_string()]; match opts.parse(&short_args) { Ok(ref m) => { - assert!((m.opt_present("test"))); + assert!(m.opt_present("test")); assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); + assert!(m.opt_present("t")); assert_eq!(m.opt_str("t").unwrap(), "20"); } _ => { @@ -111,7 +111,7 @@ fn test_optopt() { Ok(ref m) => { assert!(m.opt_present("test")); assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); + assert!(m.opt_present("t")); assert_eq!(m.opt_str("t").unwrap(), "20"); } _ => panic!(), @@ -119,9 +119,9 @@ fn test_optopt() { let short_args = vec!["-t".to_string(), "20".to_string()]; match opts.parse(&short_args) { Ok(ref m) => { - assert!((m.opt_present("test"))); + assert!(m.opt_present("test")); assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); + assert!(m.opt_present("t")); assert_eq!(m.opt_str("t").unwrap(), "20"); } _ => panic!(), @@ -443,9 +443,9 @@ fn test_optmulti() { opts.optmulti("t", "test", "testing", "TEST"); match opts.parse(&long_args) { Ok(ref m) => { - assert!((m.opt_present("test"))); + assert!(m.opt_present("test")); assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); + assert!(m.opt_present("t")); assert_eq!(m.opt_str("t").unwrap(), "20"); } _ => panic!(), @@ -453,9 +453,9 @@ fn test_optmulti() { let short_args = vec!["-t".to_string(), "20".to_string()]; match opts.parse(&short_args) { Ok(ref m) => { - assert!((m.opt_present("test"))); + assert!(m.opt_present("test")); assert_eq!(m.opt_str("test").unwrap(), "20"); - assert!((m.opt_present("t"))); + assert!(m.opt_present("t")); assert_eq!(m.opt_str("t").unwrap(), "20"); } _ => panic!(), @@ -576,16 +576,16 @@ fn test_combined() { assert!(m.free[1] == "free1"); assert_eq!(m.opt_str("s").unwrap(), "20"); assert!(m.free[2] == "free2"); - assert!((m.opt_present("flag"))); + assert!(m.opt_present("flag")); assert_eq!(m.opt_str("long").unwrap(), "30"); - assert!((m.opt_present("f"))); + assert!(m.opt_present("f")); let pair = m.opt_strs("m"); assert!(pair[0] == "40"); assert!(pair[1] == "50"); let pair = m.opt_strs("n"); assert!(pair[0] == "-A B"); assert!(pair[1] == "-60 70"); - assert!((!m.opt_present("notpresent"))); + assert!(!m.opt_present("notpresent")); } _ => panic!(), } From 94789a93498d3b5c38e4ca0eab45cb9025ff1116 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 22 Aug 2025 10:27:08 +0000 Subject: [PATCH 3/4] Remove unicode-width from rustc-dep-of-std Libtest will disable the unicode support. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f0877c7b..44e79cdc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,5 +19,5 @@ log = "0.4" [features] default = ["unicode"] -rustc-dep-of-std = ["unicode-width/rustc-dep-of-std", "std", "core"] +rustc-dep-of-std = ["std", "core"] unicode = ["dep:unicode-width"] From e53e90135c229287631088e4c0810a25201c0dd7 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 22 Aug 2025 12:08:46 +0000 Subject: [PATCH 4/4] Test with unicode feature disabled --- .github/workflows/main.yaml | 1 + src/tests/mod.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index e212d2d1..aa9221d5 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -24,6 +24,7 @@ jobs: with: key: ${{ matrix.target }} - run: cargo test + - run: cargo test --no-default-features doc_fmt: name: Document and check formatting diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 865c3280..17e689c4 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -888,6 +888,7 @@ Options: } #[test] +#[cfg(feature = "unicode")] fn test_usage_description_multibyte_handling() { let mut opts = Options::new(); opts.optflag( @@ -919,6 +920,7 @@ Options: } #[test] +#[cfg(feature = "unicode")] fn test_usage_description_newline_handling() { let mut opts = Options::new(); opts.optflag( @@ -950,6 +952,7 @@ Options: } #[test] +#[cfg(feature = "unicode")] fn test_usage_multiwidth() { let mut opts = Options::new(); opts.optflag("a", "apple", "apple description");