Skip to content

Commit 03ce599

Browse files
authored
Make unicode-width an optional default dependency (#133)
* Make unicode-width an optional default dependency * Fix warning on current rustc versions * Remove unicode-width from rustc-dep-of-std Libtest will disable the unicode support. * Test with unicode feature disabled
1 parent 57b183a commit 03ce599

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
with:
2525
key: ${{ matrix.target }}
2626
- run: cargo test
27+
- run: cargo test --no-default-features
2728

2829
doc_fmt:
2930
name: Document and check formatting

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ edition = "2021"
1010
rust-version = "1.66"
1111

1212
[dependencies]
13-
unicode-width = "0.2.0"
13+
unicode-width = { version = "0.2.0", optional = true }
1414
std = { version = "1.0", package = "rustc-std-workspace-std", optional = true }
1515
core = { version = "1.0", package = "rustc-std-workspace-core", optional = true }
1616

1717
[dev-dependencies]
1818
log = "0.4"
1919

2020
[features]
21-
rustc-dep-of-std = ["unicode-width/rustc-dep-of-std", "std", "core"]
21+
default = ["unicode"]
22+
rustc-dep-of-std = ["std", "core"]
23+
unicode = ["dep:unicode-width"]

src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
#[cfg(test)]
105105
#[macro_use]
106106
extern crate log;
107-
extern crate unicode_width;
108107

109108
use self::Fail::*;
110109
use self::HasArg::*;
@@ -119,8 +118,21 @@ use std::iter::{repeat, IntoIterator};
119118
use std::result;
120119
use std::str::FromStr;
121120

121+
#[cfg(feature = "unicode")]
122122
use unicode_width::UnicodeWidthStr;
123123

124+
#[cfg(not(feature = "unicode"))]
125+
trait UnicodeWidthStr {
126+
fn width(&self) -> usize;
127+
}
128+
129+
#[cfg(not(feature = "unicode"))]
130+
impl UnicodeWidthStr for str {
131+
fn width(&self) -> usize {
132+
self.len()
133+
}
134+
}
135+
124136
#[cfg(test)]
125137
mod tests;
126138

src/tests/mod.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ fn test_reqopt() {
5050
let short_args = vec!["-t".to_string(), "20".to_string()];
5151
match opts.parse(&short_args) {
5252
Ok(ref m) => {
53-
assert!((m.opt_present("test")));
53+
assert!(m.opt_present("test"));
5454
assert_eq!(m.opt_str("test").unwrap(), "20");
55-
assert!((m.opt_present("t")));
55+
assert!(m.opt_present("t"));
5656
assert_eq!(m.opt_str("t").unwrap(), "20");
5757
}
5858
_ => {
@@ -111,17 +111,17 @@ fn test_optopt() {
111111
Ok(ref m) => {
112112
assert!(m.opt_present("test"));
113113
assert_eq!(m.opt_str("test").unwrap(), "20");
114-
assert!((m.opt_present("t")));
114+
assert!(m.opt_present("t"));
115115
assert_eq!(m.opt_str("t").unwrap(), "20");
116116
}
117117
_ => panic!(),
118118
}
119119
let short_args = vec!["-t".to_string(), "20".to_string()];
120120
match opts.parse(&short_args) {
121121
Ok(ref m) => {
122-
assert!((m.opt_present("test")));
122+
assert!(m.opt_present("test"));
123123
assert_eq!(m.opt_str("test").unwrap(), "20");
124-
assert!((m.opt_present("t")));
124+
assert!(m.opt_present("t"));
125125
assert_eq!(m.opt_str("t").unwrap(), "20");
126126
}
127127
_ => panic!(),
@@ -443,19 +443,19 @@ fn test_optmulti() {
443443
opts.optmulti("t", "test", "testing", "TEST");
444444
match opts.parse(&long_args) {
445445
Ok(ref m) => {
446-
assert!((m.opt_present("test")));
446+
assert!(m.opt_present("test"));
447447
assert_eq!(m.opt_str("test").unwrap(), "20");
448-
assert!((m.opt_present("t")));
448+
assert!(m.opt_present("t"));
449449
assert_eq!(m.opt_str("t").unwrap(), "20");
450450
}
451451
_ => panic!(),
452452
}
453453
let short_args = vec!["-t".to_string(), "20".to_string()];
454454
match opts.parse(&short_args) {
455455
Ok(ref m) => {
456-
assert!((m.opt_present("test")));
456+
assert!(m.opt_present("test"));
457457
assert_eq!(m.opt_str("test").unwrap(), "20");
458-
assert!((m.opt_present("t")));
458+
assert!(m.opt_present("t"));
459459
assert_eq!(m.opt_str("t").unwrap(), "20");
460460
}
461461
_ => panic!(),
@@ -576,16 +576,16 @@ fn test_combined() {
576576
assert!(m.free[1] == "free1");
577577
assert_eq!(m.opt_str("s").unwrap(), "20");
578578
assert!(m.free[2] == "free2");
579-
assert!((m.opt_present("flag")));
579+
assert!(m.opt_present("flag"));
580580
assert_eq!(m.opt_str("long").unwrap(), "30");
581-
assert!((m.opt_present("f")));
581+
assert!(m.opt_present("f"));
582582
let pair = m.opt_strs("m");
583583
assert!(pair[0] == "40");
584584
assert!(pair[1] == "50");
585585
let pair = m.opt_strs("n");
586586
assert!(pair[0] == "-A B");
587587
assert!(pair[1] == "-60 70");
588-
assert!((!m.opt_present("notpresent")));
588+
assert!(!m.opt_present("notpresent"));
589589
}
590590
_ => panic!(),
591591
}
@@ -888,6 +888,7 @@ Options:
888888
}
889889

890890
#[test]
891+
#[cfg(feature = "unicode")]
891892
fn test_usage_description_multibyte_handling() {
892893
let mut opts = Options::new();
893894
opts.optflag(
@@ -919,6 +920,7 @@ Options:
919920
}
920921

921922
#[test]
923+
#[cfg(feature = "unicode")]
922924
fn test_usage_description_newline_handling() {
923925
let mut opts = Options::new();
924926
opts.optflag(
@@ -950,6 +952,7 @@ Options:
950952
}
951953

952954
#[test]
955+
#[cfg(feature = "unicode")]
953956
fn test_usage_multiwidth() {
954957
let mut opts = Options::new();
955958
opts.optflag("a", "apple", "apple description");

0 commit comments

Comments
 (0)