Skip to content

Commit 0a4384e

Browse files
committed
api(arg_matches.rs): Added a Default implementation for Values and OsValues iterators.
1 parent 448ba6a commit 0a4384e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/args/arg_matches.rs

+32
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,22 @@ impl<'a> DoubleEndedIterator for Values<'a> {
567567

568568
impl<'a> ExactSizeIterator for Values<'a> {}
569569

570+
/// Creates an empty iterator.
571+
impl Default for Values<'static> {
572+
fn default() -> Self {
573+
static EMPTY: [OsString; 0] = [];
574+
// This is never called because the iterator is empty:
575+
fn to_str_slice(_: &OsString) -> &str { unreachable!() };
576+
Values { iter: EMPTY[..].iter().map(to_str_slice) }
577+
}
578+
}
579+
580+
#[test]
581+
fn test_default_values() {
582+
let mut values: Values = Values::default();
583+
assert_eq!(values.next(), None);
584+
}
585+
570586
/// An iterator for getting multiple values out of an argument via the [`ArgMatches::values_of_os`]
571587
/// method. Usage of this iterator allows values which contain invalid UTF-8 code points unlike
572588
/// [`Values`].
@@ -604,3 +620,19 @@ impl<'a> Iterator for OsValues<'a> {
604620
impl<'a> DoubleEndedIterator for OsValues<'a> {
605621
fn next_back(&mut self) -> Option<&'a OsStr> { self.iter.next_back() }
606622
}
623+
624+
/// Creates an empty iterator.
625+
impl Default for OsValues<'static> {
626+
fn default() -> Self {
627+
static EMPTY: [OsString; 0] = [];
628+
// This is never called because the iterator is empty:
629+
fn to_str_slice(_: &OsString) -> &OsStr { unreachable!() };
630+
OsValues { iter: EMPTY[..].iter().map(to_str_slice) }
631+
}
632+
}
633+
634+
#[test]
635+
fn test_default_osvalues() {
636+
let mut values: OsValues = OsValues::default();
637+
assert_eq!(values.next(), None);
638+
}

0 commit comments

Comments
 (0)