tremor_value

Struct KnownKey

source
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>

source

pub fn key(&self) -> &str

The known key

source

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);
source

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);
}
source

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);
source

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);
source

pub fn lookup_or_insert_mut<'target, 'value, F>( &self, target: &'target mut Value<'value>, with: F, ) -> Result<&'target mut Value<'value>, Error>
where F: FnOnce() -> Value<'value>, 'key: 'value, 'value: 'target,

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);
source

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>
where F: FnOnce() -> Value<'value>, 'key: 'value, 'value: 'target,

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);
source

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);
source

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);
source§

impl<'script> KnownKey<'script>

source

pub fn into_static(self) -> KnownKey<'static>

turns the key into one with static lifetime

Trait Implementations§

source§

impl<'key> Clone for KnownKey<'key>

source§

fn clone(&self) -> KnownKey<'key>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'key> Debug for KnownKey<'key>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'key> Display for KnownKey<'key>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'key, S> From<S> for KnownKey<'key>
where Cow<'key, str>: From<S>,

source§

fn from(key: S) -> Self

Converts to this type from the input type.
source§

impl<'key> PartialEq for KnownKey<'key>

source§

fn eq(&self, other: &KnownKey<'key>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'key> Eq for KnownKey<'key>

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.