rust_3d/skip_empty_string.rs
1/*
2Copyright 2020 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! Iterator that skips empty strings
24
25/// Iterator that skips empty strings
26#[derive(Clone)]
27pub struct SkipEmptyString<'a, I>
28where
29 I: Iterator<Item = &'a str>,
30{
31 iterator: I,
32}
33
34impl<'a, I> SkipEmptyString<'a, I>
35where
36 I: Iterator<Item = &'a str>,
37{
38 pub fn new(iterator: I) -> Self {
39 Self { iterator }
40 }
41}
42
43impl<'a, I> Iterator for SkipEmptyString<'a, I>
44where
45 I: Iterator<Item = &'a str>,
46{
47 type Item = &'a str;
48
49 fn next(&mut self) -> Option<Self::Item> {
50 loop {
51 match self.iterator.next() {
52 None => return None,
53 Some("") => continue,
54 x @ Some(_) => return x,
55 }
56 }
57 }
58}
59
60//------------------------------------------------------------------------------
61
62/// Utility trait to easily spawn the SkipEmptyString iterator
63pub trait IsSkipEmptyStringProducer<'a>: Iterator<Item = &'a str>
64where
65 Self: Sized,
66{
67 fn skip_empty_string(self) -> SkipEmptyString<'a, Self>;
68}
69
70impl<'a> IsSkipEmptyStringProducer<'a> for std::str::Split<'a, &str> {
71 fn skip_empty_string(self) -> SkipEmptyString<'a, Self> {
72 SkipEmptyString::new(self)
73 }
74}