pub enum DirChildSingleOpt<T, F: Filter<P>, P: PathType + ?Sized = Path> {
None,
Some(DirChildSingle<T, F, P>),
}Expand description
A similar idea to DirChildSingle, but allows for the absence of a matching entry.
Variants§
Implementations§
Source§impl<T, F: Filter<P>, P: PathType + ?Sized> DirChildSingleOpt<T, F, P>
impl<T, F: Filter<P>, P: PathType + ?Sized> DirChildSingleOpt<T, F, P>
Sourcepub fn new(file_name: impl Into<P::PathSegmentOwned>, value: T) -> Self
pub fn new(file_name: impl Into<P::PathSegmentOwned>, value: T) -> Self
Creates a new DirChildSingleOpt with the specified file name and value.
§Examples
use std::ffi::OsString;
use dir_structure::dir_children::DirChildSingleOpt;
use dir_structure::NoFilter;
let DirChildSingleOpt::Some(d) = DirChildSingleOpt::<_, NoFilter>::new("file.txt", "file".to_owned()) else {
panic!("Expected Some variant");
};
assert_eq!(d.file_name(), &OsString::from("file.txt"));
assert_eq!(d.value(), &"file".to_owned());Sourcepub fn is_some(&self) -> bool
pub fn is_some(&self) -> bool
Returns true if this is a DirChildSingleOpt::Some.
§Examples
use dir_structure::dir_children::DirChildSingleOpt;
use dir_structure::dir_children::DirChildSingle;
use dir_structure::NoFilter;
let opt = DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "file".to_owned()));
assert!(opt.is_some());
let opt = DirChildSingleOpt::<String, NoFilter>::None;
assert!(!opt.is_some());Sourcepub fn is_none(&self) -> bool
pub fn is_none(&self) -> bool
Returns true if this is a DirChildSingleOpt::None.
§Examples
use dir_structure::dir_children::DirChildSingleOpt;
use dir_structure::dir_children::DirChildSingle;
use dir_structure::NoFilter;
let opt = DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "file".to_owned()));
assert!(!opt.is_none());
let opt = DirChildSingleOpt::<String, NoFilter>::None;
assert!(opt.is_none());Sourcepub fn as_ref(&self) -> DirChildSingleOpt<&T, F, P>
pub fn as_ref(&self) -> DirChildSingleOpt<&T, F, P>
Converts a &DirChildSingleOpt<T, F> into a DirChildSingleOpt<&T, F>.
§Examples
use dir_structure::dir_children::DirChildSingleOpt;
use dir_structure::dir_children::DirChildSingle;
use dir_structure::NoFilter;
let opt = DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "file".to_owned()));
let opt_ref = opt.as_ref();
assert_eq!(opt_ref, DirChildSingleOpt::Some(DirChildSingle::new("file.txt", &"file".to_owned())));
let opt = DirChildSingleOpt::<String, NoFilter>::None;
let opt_ref = opt.as_ref();
assert_eq!(opt_ref, DirChildSingleOpt::None);Sourcepub fn as_mut(&mut self) -> DirChildSingleOpt<&mut T, F, P>
pub fn as_mut(&mut self) -> DirChildSingleOpt<&mut T, F, P>
Converts a &mut DirChildSingleOpt<T, F> into a DirChildSingleOpt<&mut T, F>.
This clones the internal OsString and PathBuf used for the name and path.
§Examples
use dir_structure::dir_children::DirChildSingleOpt;
use dir_structure::dir_children::DirChildSingle;
use dir_structure::NoFilter;
let mut opt = DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "file".to_owned()));
let mut opt_mut = opt.as_mut();
if let DirChildSingleOpt::Some(child) = &mut opt_mut {
child.value_mut().push_str("_modified");
}
assert_eq!(opt, DirChildSingleOpt::Some(DirChildSingle::new("file.txt", "file_modified".to_owned())));
let mut opt = DirChildSingleOpt::<String, NoFilter>::None;
let mut opt_mut = opt.as_mut();
assert_eq!(opt_mut, DirChildSingleOpt::None);Sourcepub fn map<U>(
self,
f: impl FnOnce(DirChildSingle<T, F, P>) -> DirChildSingle<U, F, P>,
) -> DirChildSingleOpt<U, F, P>
pub fn map<U>( self, f: impl FnOnce(DirChildSingle<T, F, P>) -> DirChildSingle<U, F, P>, ) -> DirChildSingleOpt<U, F, P>
Maps the value inside the DirChildSingleOpt if it exists.
§Examples
use dir_structure::dir_children::DirChildSingleOpt;
use dir_structure::dir_children::DirChildSingle;
use dir_structure::NoFilter;
let opt = DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "file".to_owned()));
let opt_mapped = opt.map(|child| child.map(|v| v.to_uppercase()));
assert_eq!(opt_mapped, DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "FILE".to_owned())));
let opt = DirChildSingleOpt::<String, NoFilter>::None;
let opt_mapped = opt.map(|child| child.map(|v| v.to_uppercase()));
assert_eq!(opt_mapped, DirChildSingleOpt::None);Sourcepub fn and_then<U, F2: Filter<P2>, P2: PathType + ?Sized>(
self,
f: impl FnOnce(DirChildSingle<T, F, P>) -> DirChildSingleOpt<U, F2, P2>,
) -> DirChildSingleOpt<U, F2, P2>
pub fn and_then<U, F2: Filter<P2>, P2: PathType + ?Sized>( self, f: impl FnOnce(DirChildSingle<T, F, P>) -> DirChildSingleOpt<U, F2, P2>, ) -> DirChildSingleOpt<U, F2, P2>
Returns a new DirChildSingleOpt by applying the function f to the value inside
the DirChildSingleOpt if it exists.
§Examples
use dir_structure::dir_children::DirChildSingleOpt;
use dir_structure::dir_children::DirChildSingle;
use dir_structure::NoFilter;
let opt = DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "file".to_owned()));
let opt_and_then = opt.and_then(|child| {
DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new(child.file_name(), child.value().to_uppercase()))
});
assert_eq!(opt_and_then, DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "FILE".to_owned())));
let opt = DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "file".to_owned()));
let opt_and_then = opt.and_then(|child| DirChildSingleOpt::<String, NoFilter>::None);
assert_eq!(opt_and_then, DirChildSingleOpt::None);
let opt = DirChildSingleOpt::<String, NoFilter>::None;
let opt_and_then = opt.and_then(|child| {
DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new(child.file_name(), child.value().to_uppercase()))
});
assert_eq!(opt_and_then, DirChildSingleOpt::<_, NoFilter>::None);Sourcepub fn or_else<F2: Filter<P>>(
self,
f: impl FnOnce() -> DirChildSingleOpt<T, F2, P>,
) -> DirChildSingleOpt<T, F2, P>
pub fn or_else<F2: Filter<P>>( self, f: impl FnOnce() -> DirChildSingleOpt<T, F2, P>, ) -> DirChildSingleOpt<T, F2, P>
Returns a new DirChildSingleOpt by applying the function f to the value inside
the DirChildSingleOpt if it exists.
§Examples
use dir_structure::dir_children::DirChildSingleOpt;
use dir_structure::dir_children::DirChildSingle;
use dir_structure::NoFilter;
let opt = DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "file".to_owned()));
let opt_or_else = opt.or_else(|| {
DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "FILE".to_owned()))
});
assert_eq!(opt_or_else, DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "file".to_owned())));
let opt = DirChildSingleOpt::<String, NoFilter>::None;
let opt_or_else = opt.or_else(|| {
DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "FILE".to_owned()))
});
assert_eq!(opt_or_else, DirChildSingleOpt::Some(DirChildSingle::<_, NoFilter>::new("file.txt", "FILE".to_owned())));
let opt = DirChildSingleOpt::<String, NoFilter>::None;
let opt_or_else = opt.or_else(|| DirChildSingleOpt::<String, NoFilter>::None);
assert_eq!(opt_or_else, DirChildSingleOpt::None);Sourcepub fn to_option(self) -> Option<DirChildSingle<T, F, P>>
pub fn to_option(self) -> Option<DirChildSingle<T, F, P>>
Converts the DirChildSingleOpt into an Option.
§Examples
use dir_structure::dir_children::DirChildSingleOpt;
use dir_structure::dir_children::DirChildSingle;
use dir_structure::NoFilter;
let opt = DirChildSingleOpt::Some(DirChildSingle::new("file.txt", "file".to_owned()));
let option = opt.to_option();
assert_eq!(option, Some(DirChildSingle::<String, NoFilter>::new("file.txt", "file".to_owned())));
let opt = DirChildSingleOpt::<String, NoFilter>::None;
let option = opt.to_option();
assert_eq!(option, None);Sourcepub fn take_if(
&mut self,
pred: impl FnOnce(&DirChildSingle<T, F, P>) -> bool,
) -> DirChildSingleOpt<T, F, P>
pub fn take_if( &mut self, pred: impl FnOnce(&DirChildSingle<T, F, P>) -> bool, ) -> DirChildSingleOpt<T, F, P>
Takes the value out of the DirChildSingleOpt if the predicate pred returns true.
If the predicate returns false, or if the DirChildSingleOpt is None, this returns None.
§Examples
use dir_structure::dir_children::DirChildSingleOpt;
use dir_structure::dir_children::DirChildSingle;
use dir_structure::NoFilter;
let mut opt = DirChildSingleOpt::<String, NoFilter>::Some(DirChildSingle::new("file.txt", "file".to_owned()));
let taken = opt.take_if(|child| child.value() == "file");
assert_eq!(taken, DirChildSingleOpt::Some(DirChildSingle::new("file.txt", "file".to_owned())));
assert_eq!(opt, DirChildSingleOpt::None);
let mut opt = DirChildSingleOpt::<String, NoFilter>::Some(DirChildSingle::new("file.txt", "file".to_owned()));
let taken = opt.take_if(|child| child.value() == "other");
assert_eq!(taken, DirChildSingleOpt::None);
assert_eq!(opt, DirChildSingleOpt::Some(DirChildSingle::new("file.txt", "file".to_owned())));
let mut opt = DirChildSingleOpt::<String, NoFilter>::None;
let taken = opt.take_if(|child| child.value() == "file");
assert_eq!(taken, DirChildSingleOpt::None);
assert_eq!(opt, DirChildSingleOpt::None);Trait Implementations§
Source§impl<T: Clone, F: Clone + Filter<P>, P: Clone + PathType + ?Sized> Clone for DirChildSingleOpt<T, F, P>
impl<T: Clone, F: Clone + Filter<P>, P: Clone + PathType + ?Sized> Clone for DirChildSingleOpt<T, F, P>
Source§fn clone(&self) -> DirChildSingleOpt<T, F, P>
fn clone(&self) -> DirChildSingleOpt<T, F, P>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: PartialEq, F: PartialEq + Filter<P>, P: PartialEq + PathType + ?Sized> PartialEq for DirChildSingleOpt<T, F, P>
impl<T: PartialEq, F: PartialEq + Filter<P>, P: PartialEq + PathType + ?Sized> PartialEq for DirChildSingleOpt<T, F, P>
Source§impl<'a, T, F, Vfs: WriteSupportingVfs<'a>> WriteTo<'a, Vfs> for DirChildSingleOpt<T, F, Vfs::Path>
impl<'a, T, F, Vfs: WriteSupportingVfs<'a>> WriteTo<'a, Vfs> for DirChildSingleOpt<T, F, Vfs::Path>
impl<T: Eq, F: Eq + Filter<P>, P: Eq + PathType + ?Sized> Eq for DirChildSingleOpt<T, F, P>
impl<T, F: Filter<P>, P: PathType + ?Sized> StructuralPartialEq for DirChildSingleOpt<T, F, P>
Auto Trait Implementations§
impl<T, F, P> Freeze for DirChildSingleOpt<T, F, P>
impl<T, F, P> RefUnwindSafe for DirChildSingleOpt<T, F, P>where
<P as PathType>::PathSegmentOwned: RefUnwindSafe,
T: RefUnwindSafe,
F: RefUnwindSafe,
P: RefUnwindSafe + ?Sized,
impl<T, F, P> Send for DirChildSingleOpt<T, F, P>
impl<T, F, P> Sync for DirChildSingleOpt<T, F, P>
impl<T, F, P> Unpin for DirChildSingleOpt<T, F, P>
impl<T, F, P> UnwindSafe for DirChildSingleOpt<T, F, P>where
<P as PathType>::PathSegmentOwned: UnwindSafe,
T: UnwindSafe,
F: UnwindSafe,
P: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DirStructureItem for T
impl<T> DirStructureItem for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more