pub struct NativeFunction { /* private fields */ }
Expand description
Starlark representation of native (Rust) functions.
Almost always created with #[starlark_module]
.
Implementations
sourceimpl NativeFunction
impl NativeFunction
sourcepub fn new_direct<F>(function: F, name: String) -> Self where
F: for<'v> Fn(&mut Evaluator<'v, '_>, &Arguments<'v, '_>) -> Result<Value<'v>> + Send + Sync + 'static,
pub fn new_direct<F>(function: F, name: String) -> Self where
F: for<'v> Fn(&mut Evaluator<'v, '_>, &Arguments<'v, '_>) -> Result<Value<'v>> + Send + Sync + 'static,
Create a new NativeFunction
from the Rust function which works directly on the parameters.
The called function is responsible for validating the parameters are correct.
sourcepub fn new<F>(
function: F,
name: String,
parameters: ParametersSpec<FrozenValue>
) -> Self where
F: for<'v> Fn(&mut Evaluator<'v, '_>, ParametersParser<'v, '_>) -> Result<Value<'v>> + Send + Sync + 'static,
pub fn new<F>(
function: F,
name: String,
parameters: ParametersSpec<FrozenValue>
) -> Self where
F: for<'v> Fn(&mut Evaluator<'v, '_>, ParametersParser<'v, '_>) -> Result<Value<'v>> + Send + Sync + 'static,
Create a new NativeFunction
from the Rust function, plus the parameter specification.
sourcepub fn set_type(&mut self, typ: FrozenValue)
pub fn set_type(&mut self, typ: FrozenValue)
A .type
value, if one exists. Specified using #[starlark(type("the_type"))]
.
Trait Implementations
sourceimpl AllocFrozenValue for NativeFunction
impl AllocFrozenValue for NativeFunction
sourcefn alloc_frozen_value(self, heap: &FrozenHeap) -> FrozenValue
fn alloc_frozen_value(self, heap: &FrozenHeap) -> FrozenValue
Allocate a value in the frozen heap and return a reference to the allocated value.
sourceimpl<'v> AllocValue<'v> for NativeFunction
impl<'v> AllocValue<'v> for NativeFunction
sourcefn alloc_value(self, heap: &'v Heap) -> Value<'v>
fn alloc_value(self, heap: &'v Heap) -> Value<'v>
Allocate the value on a heap and return a reference to the allocated value. Read more
sourceimpl Debug for NativeFunction
impl Debug for NativeFunction
sourceimpl Display for NativeFunction
impl Display for NativeFunction
sourceimpl ProvidesStaticType for NativeFunction
impl ProvidesStaticType for NativeFunction
type StaticType = NativeFunction
type StaticType = NativeFunction
Same type as Self
but with lifetimes dropped to 'static
. Read more
sourceimpl<'v> Serialize for NativeFunction where
Self: StarlarkValue<'v>,
impl<'v> Serialize for NativeFunction where
Self: StarlarkValue<'v>,
sourceimpl<'v> StarlarkValue<'v> for NativeFunction
impl<'v> StarlarkValue<'v> for NativeFunction
Define the function type
sourcefn get_type(&self) -> &'static str
fn get_type(&self) -> &'static str
Return a string describing the type of self, as returned by the type() function. Read more
sourcefn get_type_value_static() -> FrozenStringValue
fn get_type_value_static() -> FrozenStringValue
Like get_type
, but returns a reusable FrozenStringValue
pointer to it. This function deliberately doesn’t take a heap,
as it would not be performant to allocate a new value each time. Read more
sourcefn invoke(
&self,
_me: Value<'v>,
args: &Arguments<'v, '_>,
eval: &mut Evaluator<'v, '_>
) -> Result<Value<'v>>
fn invoke(
&self,
_me: Value<'v>,
args: &Arguments<'v, '_>,
eval: &mut Evaluator<'v, '_>
) -> Result<Value<'v>>
Directly invoke a function.
The number of named
and names
arguments are guaranteed to be equal. Read more
sourcefn extra_memory(&self) -> usize
fn extra_memory(&self) -> usize
sourcefn get_attr(&self, attribute: &str, _heap: &'v Heap) -> Option<Value<'v>>
fn get_attr(&self, attribute: &str, _heap: &'v Heap) -> Option<Value<'v>>
Get an attribute for the current value as would be returned by dotted
expression (i.e. a.attribute
). Read more
sourcefn dir_attr(&self) -> Vec<String>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
fn dir_attr(&self) -> Vec<String>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Return a vector of string listing all attribute of the current value. Read more
sourcefn documentation(&self) -> Option<DocItem>
fn documentation(&self) -> Option<DocItem>
Return structured documentation for self, if available.
sourcefn matches_type(&self, ty: &str) -> bool
fn matches_type(&self, ty: &str) -> bool
Is this value a match for a named type. Usually returns true
for
values matching get_type
, but might also work for subtypes it implements. Read more
sourcefn get_methods(&self) -> Option<&'static Methods>
fn get_methods(&self) -> Option<&'static Methods>
Get the members associated with this type, accessible via this_type.x
.
These members will have dir
/getattr
/hasattr
properly implemented,
so it is the preferred way to go if possible. See
MethodsStatic
for an example of how
to define this method. Read more
sourcefn collect_repr(&self, collector: &mut String)
fn collect_repr(&self, collector: &mut String)
Return a string representation of self, as returned by the repr()
function.
Defaults to the Display
instance - which should be fine for nearly all types.
In many cases the repr()
representation will also be a Starlark expression
for creating the value. Read more
sourcefn collect_repr_cycle(&self, collector: &mut String)
fn collect_repr_cycle(&self, collector: &mut String)
Invoked to print repr
when a cycle is the object stack is detected.
sourcefn to_int(&self) -> Result<i32>
fn to_int(&self) -> Result<i32>
Convert self to a integer value, as returned by the int() function if the type is numeric (not for string). Works for int and bool (0 = false, 1 = true). Read more
sourcefn write_hash(&self, hasher: &mut StarlarkHasher) -> Result<()>
fn write_hash(&self, hasher: &mut StarlarkHasher) -> Result<()>
sourcefn equals(&self, _other: Value<'v>) -> Result<bool>
fn equals(&self, _other: Value<'v>) -> Result<bool>
Compare self
with other
for equality.
Should only return an error on excessive recursion. Read more
sourcefn at(&self, index: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
fn at(&self, index: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
Return the result of a[index]
if a
is indexable.
sourcefn slice(
&self,
_start: Option<Value<'v>>,
_stop: Option<Value<'v>>,
_stride: Option<Value<'v>>,
_heap: &'v Heap
) -> Result<Value<'v>>
fn slice(
&self,
_start: Option<Value<'v>>,
_stop: Option<Value<'v>>,
_stride: Option<Value<'v>>,
_heap: &'v Heap
) -> Result<Value<'v>>
Extract a slice of the underlying object if the object is indexable. The
result will be object between start
and stop
(both of them are
added length() if negative and then clamped between 0 and length()).
stride
indicates the direction. Read more
sourcefn iterate<'a>(
&'a self,
_heap: &'v Heap
) -> Result<Box<dyn Iterator<Item = Value<'v>> + 'a>> where
'v: 'a,
fn iterate<'a>(
&'a self,
_heap: &'v Heap
) -> Result<Box<dyn Iterator<Item = Value<'v>> + 'a>> where
'v: 'a,
Returns an iterable over the value of this container if this value holds an iterable container. Read more
sourcefn with_iterator(
&self,
heap: &'v Heap,
f: &mut dyn FnMut(&mut dyn Iterator<Item = Value<'v>>) -> Result<()>
) -> Result<()>
fn with_iterator(
&self,
heap: &'v Heap,
f: &mut dyn FnMut(&mut dyn Iterator<Item = Value<'v>>) -> Result<()>
) -> Result<()>
sourcefn has_attr(&self, _attribute: &str) -> bool
fn has_attr(&self, _attribute: &str) -> bool
Return true if an attribute of name attribute
exists for the current
value. Read more
sourcefn is_in(&self, other: Value<'v>) -> Result<bool>
fn is_in(&self, other: Value<'v>) -> Result<bool>
Tell wether other
is in the current value, if it is a container. Read more
sourcefn plus(&self, _heap: &'v Heap) -> Result<Value<'v>>
fn plus(&self, _heap: &'v Heap) -> Result<Value<'v>>
Apply the +
unary operator to the current value. Read more
sourcefn minus(&self, _heap: &'v Heap) -> Result<Value<'v>>
fn minus(&self, _heap: &'v Heap) -> Result<Value<'v>>
Apply the -
unary operator to the current value. Read more
sourcefn add(&self, rhs: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
fn add(&self, rhs: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
Add other
to the current value. Pass both self and
the Value form of self as original. Read more
sourcefn sub(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
fn sub(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
Substract other
from the current value. Read more
sourcefn mul(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
fn mul(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
Multiply the current value with other
. Read more
sourcefn div(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
fn div(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
Divide the current value by other
. Always results in a float value. Read more
sourcefn percent(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
fn percent(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
Apply the percent operator between the current value and other
. Usually used on
strings, as per
the Starlark spec. Read more
sourcefn floor_div(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
fn floor_div(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
Floor division between the current value and other
. Read more
sourcefn bit_or(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
fn bit_or(&self, other: Value<'v>, _heap: &'v Heap) -> Result<Value<'v>>
Bitwise |
operator. Read more
sourcefn left_shift(&self, other: Value<'v>) -> Result<Value<'v>>
fn left_shift(&self, other: Value<'v>) -> Result<Value<'v>>
Bitwise <<
operator.
sourcefn right_shift(&self, other: Value<'v>) -> Result<Value<'v>>
fn right_shift(&self, other: Value<'v>) -> Result<Value<'v>>
Bitwise >>
operator.
sourcefn export_as(&self, _variable_name: &str, _eval: &mut Evaluator<'v, '_>)
fn export_as(&self, _variable_name: &str, _eval: &mut Evaluator<'v, '_>)
Called when exporting a value under a specific name,
Auto Trait Implementations
impl !RefUnwindSafe for NativeFunction
impl Send for NativeFunction
impl Sync for NativeFunction
impl Unpin for NativeFunction
impl !UnwindSafe for NativeFunction
Blanket Implementations
sourceimpl<'a, T> AnyLifetime<'a> for T where
T: 'a + ProvidesStaticType + ?Sized,
impl<'a, T> AnyLifetime<'a> for T where
T: 'a + ProvidesStaticType + ?Sized,
sourcefn static_type_id() -> TypeId
fn static_type_id() -> TypeId
Must return the TypeId
of Self
but where the lifetimes are changed
to 'static
. Must be consistent with static_type_of
. Read more
sourcefn static_type_of(&self) -> TypeId
fn static_type_of(&self) -> TypeId
Must return the TypeId
of Self
but where the lifetimes are changed
to 'static
. Must be consistent with static_type_id
. Must not
consult the self
parameter in any way. Read more
sourceimpl<T> AsARef<T> for T where
T: ?Sized,
impl<T> AsARef<T> for T where
T: ?Sized,
sourcefn try_as_aref(this: &T) -> Result<ARef<'_, T>, BorrowError>
fn try_as_aref(this: &T) -> Result<ARef<'_, T>, BorrowError>
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more