pub struct KnownKey<'key> { /* private fields */ }
Expand description
Well known key that can be looked up in a Value
faster.
It achives this by memorizing the hash.
Implementations§
source§impl<'key> KnownKey<'key>
impl<'key> KnownKey<'key>
sourcepub fn lookup<'target, 'value>(
&self,
target: &'target Value<'value>,
) -> Option<&'target Value<'value>>where
'value: 'target,
pub fn lookup<'target, 'value>(
&self,
target: &'target Value<'value>,
) -> Option<&'target Value<'value>>where
'value: 'target,
Looks up this key in a Value
, returns None if the
key wasn’t present or target
isn’t an object
use tremor_value::prelude::*;
let object = literal!({
"answer": 42,
"key": 7
});
let known_key = KnownKey::from("answer");
assert_eq!(known_key.lookup(&object).unwrap(), &42);
sourcepub fn map_lookup<'target, 'value>(
&self,
map: &'target HashMap<Cow<'value, str>, Value<'value>, ObjectHasher>,
) -> Option<&'target Value<'value>>where
'value: 'target,
pub fn map_lookup<'target, 'value>(
&self,
map: &'target HashMap<Cow<'value, str>, Value<'value>, ObjectHasher>,
) -> Option<&'target Value<'value>>where
'value: 'target,
Looks up this key in a HashMap<<Cow<'value, str>, Value<'value>>
the inner representation of an object Value
, returns None if the
key wasn’t present.
use tremor_value::prelude::*;
let object = literal!({
"answer": 42,
"key": 7
});
let known_key = KnownKey::from("answer");
if let Some(inner) = object.as_object() {
assert_eq!(known_key.map_lookup(inner).unwrap(), &42);
}
sourcepub fn lookup_mut<'target, 'value>(
&self,
target: &'target mut Value<'value>,
) -> Option<&'target mut Value<'value>>where
'key: 'value,
'value: 'target,
pub fn lookup_mut<'target, 'value>(
&self,
target: &'target mut Value<'value>,
) -> Option<&'target mut Value<'value>>where
'key: 'value,
'value: 'target,
Looks up this key in a Value
, returns None if the
key wasn’t present or target
isn’t an object
use tremor_value::prelude::*;
let mut object = literal!({
"answer": 23,
"key": 7
});
let known_key = KnownKey::from("answer");
assert_eq!(object["answer"], 23);
if let Some(answer) = known_key.lookup_mut(&mut object) {
*answer = Value::from(42);
}
assert_eq!(object["answer"], 42);
sourcepub fn map_lookup_mut<'target, 'value>(
&self,
map: &'target mut HashMap<Cow<'value, str>, Value<'value>, ObjectHasher>,
) -> Option<&'target mut Value<'value>>where
'key: 'value,
'value: 'target,
pub fn map_lookup_mut<'target, 'value>(
&self,
map: &'target mut HashMap<Cow<'value, str>, Value<'value>, ObjectHasher>,
) -> Option<&'target mut Value<'value>>where
'key: 'value,
'value: 'target,
Looks up this key in a HashMap<Cow<'value, str>, Value<'value>>
, the inner representation of an object value.
returns None if the key wasn’t present.
use tremor_value::prelude::*;
let mut object = literal!({
"answer": 23,
"key": 7
});
assert_eq!(object["answer"], 23);
let known_key = KnownKey::from("answer");
if let Some(inner) = object.as_object_mut() {
if let Some(answer) = known_key.map_lookup_mut(inner) {
*answer = Value::from(42);
}
}
assert_eq!(object["answer"], 42);
sourcepub fn lookup_or_insert_mut<'target, 'value, F>(
&self,
target: &'target mut Value<'value>,
with: F,
) -> Result<&'target mut Value<'value>, Error>
pub fn lookup_or_insert_mut<'target, 'value, F>( &self, target: &'target mut Value<'value>, with: F, ) -> Result<&'target mut Value<'value>, Error>
Looks up this key in a Value
, inserts with
when the key
when wasn’t present returns None if the target
isn’t an object
§Errors
- if the value isn’t an object
use tremor_value::prelude::*;
let mut object = literal!({
"answer": 23,
"key": 7
});
let known_key = KnownKey::from("answer");
assert_eq!(object["answer"], 23);
if let Ok(answer) = known_key.lookup_or_insert_mut(&mut object, || 17.into()) {
assert_eq!(*answer, 23);
*answer = Value::from(42);
}
assert_eq!(object["answer"], 42);
let known_key2 = KnownKey::from("also the answer");
if let Ok(answer) = known_key2.lookup_or_insert_mut(&mut object, || 8.into()) {
assert_eq!(*answer, 8);
*answer = Value::from(42);
}
assert_eq!(object["also the answer"], 42);
sourcepub fn map_lookup_or_insert_mut<'target, 'value, F>(
&self,
map: &'target mut HashMap<Cow<'value, str>, Value<'value>, ObjectHasher>,
with: F,
) -> &'target mut Value<'value>
pub fn map_lookup_or_insert_mut<'target, 'value, F>( &self, map: &'target mut HashMap<Cow<'value, str>, Value<'value>, ObjectHasher>, with: F, ) -> &'target mut Value<'value>
Looks up this key in a HashMap<Cow<'value, str>, Value<'value>>
, the inner representation of an object Value
.
Inserts with
when the key when wasn’t present.
use tremor_value::prelude::*;
let mut object = literal!({
"answer": 23,
"key": 7
});
let known_key = KnownKey::from("answer");
assert_eq!(object["answer"], 23);
if let Some(inner) = object.as_object_mut() {
let answer = known_key.map_lookup_or_insert_mut(inner, || 17.into());
assert_eq!(*answer, 23);
*answer = Value::from(42);
}
assert_eq!(object["answer"], 42);
let known_key2 = KnownKey::from("also the answer");
if let Some(inner) = object.as_object_mut() {
let answer = known_key2.map_lookup_or_insert_mut(inner, || 8.into());
assert_eq!(*answer, 8);
*answer = Value::from(42);
}
assert_eq!(object["also the answer"], 42);
sourcepub fn insert<'target, 'value>(
&self,
target: &'target mut Value<'value>,
value: Value<'value>,
) -> Result<Option<Value<'value>>, Error>where
'key: 'value,
'value: 'target,
pub fn insert<'target, 'value>(
&self,
target: &'target mut Value<'value>,
value: Value<'value>,
) -> Result<Option<Value<'value>>, Error>where
'key: 'value,
'value: 'target,
Inserts a value key into Value
, returns None if the
key wasn’t present otherwise Some(old value
).
§Errors
- if
target
isn’t an object
use tremor_value::prelude::*;
let mut object = literal!({
"answer": 23,
"key": 7
});
let known_key = KnownKey::from("answer");
assert_eq!(object["answer"], 23);
assert!(known_key.insert(&mut object, Value::from(42)).is_ok());
assert_eq!(object["answer"], 42);
let known_key2 = KnownKey::from("also the answer");
assert!(known_key2.insert(&mut object, Value::from(42)).is_ok());
assert_eq!(object["also the answer"], 42);
sourcepub fn map_insert<'target, 'value>(
&self,
map: &'target mut HashMap<Cow<'value, str>, Value<'value>, ObjectHasher>,
value: Value<'value>,
) -> Option<Value<'value>>where
'key: 'value,
'value: 'target,
pub fn map_insert<'target, 'value>(
&self,
map: &'target mut HashMap<Cow<'value, str>, Value<'value>, ObjectHasher>,
value: Value<'value>,
) -> Option<Value<'value>>where
'key: 'value,
'value: 'target,
Inserts a value key into map
, returns None if the
key wasn’t present otherwise Some(old value
).
use tremor_value::prelude::*;
let mut object = literal!({
"answer": 23,
"key": 7
});
let known_key = KnownKey::from("answer");
assert_eq!(object["answer"], 23);
if let Some(inner) = object.as_object_mut() {
assert!(known_key.map_insert(inner, Value::from(42)).is_some());
}
assert_eq!(object["answer"], 42);
let known_key2 = KnownKey::from("also the answer");
if let Some(inner) = object.as_object_mut() {
assert!(known_key2.map_insert(inner, Value::from(42)).is_none());
}
assert_eq!(object["also the answer"], 42);
Trait Implementations§
impl<'key> Eq for KnownKey<'key>
impl<'key> StructuralPartialEq for KnownKey<'key>
Auto Trait Implementations§
impl<'key> Freeze for KnownKey<'key>
impl<'key> RefUnwindSafe for KnownKey<'key>
impl<'key> Send for KnownKey<'key>
impl<'key> Sync for KnownKey<'key>
impl<'key> Unpin for KnownKey<'key>
impl<'key> UnwindSafe for KnownKey<'key>
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)