pub struct This<T>(pub T);
Expand description

A function argument abstraction enabling dynamic method invocation on a target instance or on the first argument if the function is not called as a method. This is similar to how methods can be called as functions using the fully-qualified syntax.

§Using This

use cel_interpreter::extractors::This;

/// Notice how `This` refers to the target value when called as a method,
/// but the first argument when called as a function.
let program1 = "'foobar'.startsWith('foo') == true";
let program2 = "startsWith('foobar', 'foo') == true";

fn starts_with(This(this): This<Arc<String>>, prefix: Arc<String>) -> bool {
    this.starts_with(prefix.as_str())
}

§Type of This

This also accepts a type T which determines the specific type that’s extracted. Any type that supports [FromValue] can be used. In the previous example, the method startsWith is only ever called on a string, so we can use This<Rc<String>> to extract the string automatically prior to our method actually being called.

In some cases, you may want access to the raw Value instead, for example, the contains method works for several different types. In these cases, you can use This<Value> to extract the raw value.

pub fn contains(This(this): This<Value>, arg: Value) -> Result<Value> {
    Ok(match this {
        Value::List(v) => v.contains(&arg),
        ...
    }
}

Tuple Fields§

§0: T

Auto Trait Implementations§

§

impl<T> Freeze for This<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for This<T>
where T: RefUnwindSafe,

§

impl<T> Send for This<T>
where T: Send,

§

impl<T> Sync for This<T>
where T: Sync,

§

impl<T> Unpin for This<T>
where T: Unpin,

§

impl<T> UnwindSafe for This<T>
where T: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.