Skip to content

Commit

Permalink
feat: impl Index and IndexMut for &String and &FastStr
Browse files Browse the repository at this point in the history
  • Loading branch information
Millione authored and liuq19 committed Nov 21, 2023
1 parent f2cd277 commit 314dc0b
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/value/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,37 @@ impl IndexMut for usize {
}
}

impl Index for &str {
fn value_index_into<'dom, 'v>(self, v: &'v Value<'dom>) -> Option<&'v Value<'dom>> {
v.get_key(self)
}
macro_rules! impl_index {
($($t: ty),*) => {
$(
impl Index for &$t {
fn value_index_into<'dom, 'v>(self, v: &'v Value<'dom>) -> Option<&'v Value<'dom>> {
v.get_key(self)
}

fn lazyvalue_index_into<'de>(self, v: &'de LazyValue<'de>) -> Option<LazyValue<'de>> {
v.get_key(self)
}
}
fn lazyvalue_index_into<'de>(self, v: &'de LazyValue<'de>) -> Option<LazyValue<'de>> {
v.get_key(self)
}
}

impl IndexMut for &str {
fn index_into_mut<'dom, 'v>(self, v: &'v mut Value<'dom>) -> Option<&'v mut Value<'dom>> {
v.get_key_mut(self)
}
impl IndexMut for &$t {
fn index_into_mut<'dom, 'v>(self, v: &'v mut Value<'dom>) -> Option<&'v mut Value<'dom>> {
v.get_key_mut(self)
}
}
)*
};
}

impl_index!(str, String, faststr::FastStr);

// Prevent users from implementing the Index trait.
mod private {
pub trait Sealed {}
impl Sealed for usize {}
impl Sealed for str {}
impl Sealed for std::string::String {}
impl Sealed for faststr::FastStr {}
impl<'a, T> Sealed for &'a T where T: ?Sized + Sealed {}
}

Expand Down

0 comments on commit 314dc0b

Please sign in to comment.