Skip to content

Commit

Permalink
move empty to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian committed Sep 11, 2024
1 parent d0bfb21 commit 4691344
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
3 changes: 3 additions & 0 deletions utils/pattern/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ pub trait PatternBackend: crate::private::Sealed + 'static + core::fmt::Debug {
/// Writes the placeholder into the pattern string, the inverse of `Self::PlaceholderKey::from_str`
#[cfg(feature = "alloc")]
fn escape_placeholder(s: &mut String, placeholder: Self::PlaceholderKey<'_>);

/// 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 @@ -389,6 +389,10 @@ impl PatternBackend for DoublePlaceholder {
DoublePlaceholderKey::Place1 => "{1}",
});
}

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

#[doc(hidden)] // TODO(#4467): Should be internal
Expand Down
27 changes: 24 additions & 3 deletions utils/pattern/src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,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>: Default,
Box<B::Store>: for<'a> From<&'a B::Store>,
{
fn default() -> Self {
#[allow(clippy::unwrap_used)]
Pattern::try_from_items(core::iter::empty()).unwrap()
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
4 changes: 4 additions & 0 deletions utils/pattern/src/multi_named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ impl PatternBackend for MultiNamedPlaceholder {
s.push_str(placeholder.0);
s.push('}');
}

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 @@ -249,6 +249,10 @@ impl PatternBackend for SinglePlaceholder {
fn escape_placeholder(s: &mut String, _: SinglePlaceholderKey) {
s.push_str("{0}");
}

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

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

0 comments on commit 4691344

Please sign in to comment.