From 5d846eb88cf2ebe120923aef974df8d8216f4e59 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Wed, 7 Dec 2022 19:16:15 +0100 Subject: [PATCH] common/rust: add Path::parents helper method --- common/rust/src/lib.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/common/rust/src/lib.rs b/common/rust/src/lib.rs index 457791d..8be2e20 100644 --- a/common/rust/src/lib.rs +++ b/common/rust/src/lib.rs @@ -2,7 +2,7 @@ mod section_range; -use std::mem; +use std::{mem, path::Path}; pub use section_range::{EmptyRange, InvalidSectionString, SectionRange}; @@ -61,3 +61,18 @@ impl Iterator for UpToTwoIter { ret } } + +pub trait PathExt { + type Iter<'a>: Iterator + 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()) + } +}