Skip to content

Commit

Permalink
default
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian committed Sep 16, 2024
1 parent 30c6630 commit 4b2706a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
3 changes: 3 additions & 0 deletions utils/pattern/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ pub trait PatternBackend: crate::private::Sealed + 'static + core::fmt::Debug {
/// Iterates over the pattern items in a store.
#[doc(hidden)] // TODO(#4467): Should be internal
fn iter_items(store: &Self::Store) -> Self::Iter<'_>;

/// The store for the empty pattern, used to implement `Default`
fn empty() -> &'static Self::Store;
}

/// Default annotation for the literal portion of a pattern.
Expand Down
4 changes: 4 additions & 0 deletions utils/pattern/src/double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ impl PatternBackend for DoublePlaceholder {

Ok(result.into_boxed_str())
}

fn empty() -> &'static Self::Store {
"\0\u{1}"
}
}

#[doc(hidden)] // TODO(#4467): Should be internal
Expand Down
32 changes: 32 additions & 0 deletions utils/pattern/src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ impl<B: PatternBackend> core::fmt::Debug for Pattern<B> {
}
}

impl<B: PatternBackend> Default for &'static Pattern<B> {
fn default() -> Self {
Pattern::from_ref_store_unchecked(B::empty())
}
}

#[cfg(feature = "alloc")]
impl<B: PatternBackend> Default for Box<Pattern<B>>
where
Box<B::Store>: for<'a> From<&'a B::Store>,
{
fn default() -> Self {
Pattern::from_ref_store_unchecked(B::empty()).to_owned()
}
}

#[test]
fn test_defaults() {
assert_eq!(
Box::<Pattern::<crate::SinglePlaceholder>>::default(),
Pattern::try_from_items(core::iter::empty()).unwrap()
);
assert_eq!(
Box::<Pattern::<crate::DoublePlaceholder>>::default(),
Pattern::try_from_items(core::iter::empty()).unwrap()
);
assert_eq!(
Box::<Pattern::<crate::MultiNamedPlaceholder>>::default(),
Pattern::try_from_items(core::iter::empty()).unwrap()
);
}

#[cfg(feature = "alloc")]
impl<B: PatternBackend> ToOwned for Pattern<B>
where
Expand Down
6 changes: 2 additions & 4 deletions utils/pattern/src/frontend/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ impl<P: PatternBackend> core::ops::Deref for PatternString<P> {
impl<B: PatternBackend> Default for PatternString<B>
where
B: PatternBackend,
for<'a> B::PlaceholderKeyCow<'a>: core::str::FromStr,
for<'a> <B::PlaceholderKeyCow<'a> as core::str::FromStr>::Err: core::fmt::Debug,
Box<B::Store>: for<'a> From<&'a B::Store>,
{
fn default() -> Self {
#[allow(clippy::unwrap_used)] // todo does this work?
Self(Pattern::<B>::try_from_str("", Default::default()).unwrap())
Self(Box::<Pattern<B>>::default())
}
}

Expand Down
4 changes: 4 additions & 0 deletions utils/pattern/src/multi_named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ impl PatternBackend for MultiNamedPlaceholder {
}
Ok(string.into_boxed_str())
}

fn empty() -> &'static Self::Store {
""
}
}

#[derive(Debug)]
Expand Down
4 changes: 4 additions & 0 deletions utils/pattern/src/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ impl PatternBackend for SinglePlaceholder {
}
Ok(result.into_boxed_str())
}

fn empty() -> &'static Self::Store {
"\0"
}
}

#[doc(hidden)] // TODO(#4467): Should be internal
Expand Down

0 comments on commit 4b2706a

Please sign in to comment.