common/rust: add Path::parents helper method

This commit is contained in:
Xiretza 2022-12-07 19:16:15 +01:00
parent 23418f98be
commit 5d846eb88c

View file

@ -2,7 +2,7 @@
mod section_range; mod section_range;
use std::mem; use std::{mem, path::Path};
pub use section_range::{EmptyRange, InvalidSectionString, SectionRange}; pub use section_range::{EmptyRange, InvalidSectionString, SectionRange};
@ -61,3 +61,18 @@ impl<T> Iterator for UpToTwoIter<T> {
ret ret
} }
} }
pub trait PathExt {
type Iter<'a>: Iterator<Item = &'a Path>
where
Self: 'a;
fn parents(&self) -> Self::Iter<'_>;
}
impl PathExt for Path {
type Iter<'a> = std::iter::Successors<&'a Path, for<'p> fn(&&'p Path) -> Option<&'p Path>>;
fn parents(&self) -> Self::Iter<'_> {
std::iter::successors(Some(self), |dir| dir.parent())
}
}