Struct Class

Source
pub struct Class<'js, C>(/* private fields */)
where
    C: JsClass<'js>;
Expand description

A object which is instance of a Rust class.

Implementations§

Source§

impl<'js, C> Class<'js, C>
where C: JsClass<'js>,

Source

pub fn instance(ctx: Ctx<'js>, value: C) -> Result<Class<'js, C>, Error>

Create a class from a Rust object.

Source

pub fn instance_proto( value: C, proto: Object<'js>, ) -> Result<Class<'js, C>, Error>

Create a class from a Rust object with a given prototype.

Source

pub fn prototype(ctx: &Ctx<'js>) -> Result<Option<Object<'js>>, Error>

Returns the prototype for the class.

Returns None if the class is not yet registered or if the class doesn’t have a prototype.

Source

pub fn create_constructor( ctx: &Ctx<'js>, ) -> Result<Option<Constructor<'js>>, Error>

Create a constructor for the current class using its definition.

Source

pub fn define(object: &Object<'js>) -> Result<(), Error>

Defines the predefined constructor of this class, if there is one, onto the given object.

Source

pub fn get_cell<'a>(&self) -> &'a JsCell<'js, C>

Returns a reference to the underlying object contained in a cell.

Source

pub fn borrow<'a>(&'a self) -> Borrow<'a, 'js, C>

Borrow the Rust class type.

JavaScript classes behave similar to Rc in Rust, you can essentially think of a class object as a Rc<RefCell<C>> and with similar borrowing functionality.

§Panic

This function panics if the class is already borrowed mutably.

Source

pub fn borrow_mut<'a>(&'a self) -> BorrowMut<'a, 'js, C>

Borrow the Rust class type mutably.

JavaScript classes behave similar to Rc in Rust, you can essentially think of a class object as a Rc<RefCell<C>> and with similar borrowing functionality.

§Panic

This function panics if the class is already borrowed mutably or immutably, or the Class can’t be borrowed mutably.

Source

pub fn try_borrow<'a>(&'a self) -> Result<Borrow<'a, 'js, C>, Error>

Try to borrow the Rust class type.

JavaScript classes behave similar to Rc in Rust, you can essentially think of a class object as a Rc<RefCell<C>> and with similar borrowing functionality.

This returns an error when the class is already borrowed mutably.

Source

pub fn try_borrow_mut<'a>(&'a self) -> Result<BorrowMut<'a, 'js, C>, Error>

Try to borrow the Rust class type mutably.

JavaScript classes behave similar to Rc in Rust, you can essentially think of a class object as a Rc<RefCell<C>> and with similar borrowing functionality.

This returns an error when the class is already borrowed mutably, immutably or the class can’t be borrowed mutably.

Source

pub fn into_inner(self) -> Object<'js>

Turns the class back into a generic object.

Source

pub fn as_inner(&self) -> &Object<'js>

Turns the class back into a generic object.

Source

pub fn from_value(value: &Value<'js>) -> Result<Class<'js, C>, Error>

Convert from value.

Source

pub fn into_value(self) -> Value<'js>

Turn the class into a value.

Source

pub fn from_object(object: &Object<'js>) -> Option<Class<'js, C>>

Converts a generic object into a class if the object is of the right class.

Methods from Deref<Target = Object<'js>>§

Source

pub fn prop<K, V, P>(&self, key: K, prop: V) -> Result<(), Error>
where K: IntoAtom<'js>, V: AsProperty<'js, P>,

Define a property of an object

// Define readonly property without value
obj.prop("no_val", ()).unwrap();
// Define readonly property with value
obj.prop("ro_str", "Some const text").unwrap();
// Define readonly property with value and make it to be writable
obj.prop("ro_str2", Property::from("Some const text").writable()).unwrap();
// Define readonly property using getter and make it to be enumerable
obj.prop("ro_str_get", Accessor::from(|| "Some readable text").enumerable()).unwrap();
// Define readonly property using getter and setter
obj.prop("ro_str_get_set",
    Accessor::from(|| "Some text")
        .set(|new_val: String| { /* do something */ })
).unwrap();
Source

pub fn get<K, V>(&self, k: K) -> Result<V, Error>
where K: IntoAtom<'js>, V: FromJs<'js>,

Get a new value

Source

pub fn contains_key<K>(&self, k: K) -> Result<bool, Error>
where K: IntoAtom<'js>,

check whether the object contains a certain key.

Source

pub fn set<K, V>(&self, key: K, value: V) -> Result<(), Error>
where K: IntoAtom<'js>, V: IntoJs<'js>,

Set a member of an object to a certain value

Source

pub fn remove<K>(&self, key: K) -> Result<(), Error>
where K: IntoAtom<'js>,

Remove a member of an object

Source

pub fn is_empty(&self) -> bool

Check the object for empty

Source

pub fn len(&self) -> usize

Get the number of properties

Source

pub fn keys<K>(&self) -> ObjectKeysIter<'js, K>
where K: FromAtom<'js>,

Get own string enumerable property names of an object

Source

pub fn own_keys<K>(&self, filter: Filter) -> ObjectKeysIter<'js, K>
where K: FromAtom<'js>,

Get own property names of an object

Source

pub fn props<K, V>(&self) -> ObjectIter<'js, K, V>
where K: FromAtom<'js>, V: FromJs<'js>,

Get own string enumerable properties of an object

Source

pub fn own_props<K, V>(&self, filter: Filter) -> ObjectIter<'js, K, V>
where K: FromAtom<'js>, V: FromJs<'js>,

Get own properties of an object

Source

pub fn values<K>(&self) -> ObjectValuesIter<'js, K>
where K: FromAtom<'js>,

Get own string enumerable property values of an object

Source

pub fn own_values<K>(&self, filter: Filter) -> ObjectValuesIter<'js, K>
where K: FromAtom<'js>,

Get own property values of an object

Source

pub fn get_prototype(&self) -> Option<Object<'js>>

Get an object prototype

Objects can have no prototype, in this case this function will return null.

Source

pub fn set_prototype(&self, proto: Option<&Object<'js>>) -> Result<(), Error>

Set an object prototype

If called with None the function will set the prototype of the object to null.

This function will error if setting the prototype causes a cycle in the prototype chain.

Source

pub fn is_instance_of(&self, class: impl AsRef<Value<'js>>) -> bool

Check instance of object

Source

pub fn is_array_buffer(&self) -> bool

Returns whether the object is an instance of ArrayBuffer.

Source

pub unsafe fn ref_array_buffer(&self) -> &ArrayBuffer<'_>

Interpret as ArrayBuffer

§Safety

You should be sure that the object actually is the required type.

Source

pub fn as_array_buffer(&self) -> Option<&ArrayBuffer<'_>>

Turn the object into an array buffer if the object is an instance of ArrayBuffer.

Source

pub fn is_typed_array<T>(&self) -> bool
where T: TypedArrayItem,

Source

pub unsafe fn ref_typed_array<'a, T>(&'a self) -> &'a TypedArray<'js, T>
where T: TypedArrayItem,

Interpret as TypedArray

§Safety

You should be sure that the object actually is the required type.

Source

pub fn as_typed_array<T>(&self) -> Option<&TypedArray<'js, T>>
where T: TypedArrayItem,

Source

pub fn as_value(&self) -> &Value<'js>

Reference to value

Source

pub fn as_inner(&self) -> &Value<'js>

Returns a reference to the underlying super type.

Source

pub fn ctx(&self) -> &Ctx<'js>

Returns the Ctx object associated with this value

Source

pub fn instance_of<C>(&self) -> bool
where C: JsClass<'js>,

Returns if the object is of a certain Rust class.

Source

pub fn into_class<C>(&self) -> Result<Class<'js, C>, &Object<'js>>
where C: JsClass<'js>,

Turn the object into the class if it is an instance of that class.

Source

pub fn as_class<C>(&self) -> Option<&Class<'js, C>>
where C: JsClass<'js>,

Turn the object into the class if it is an instance of that class.

Methods from Deref<Target = Value<'js>>§

Source

pub fn ctx(&self) -> &Ctx<'js>

Returns the Ctx object associated with this value.

Source

pub fn as_bool(&self) -> Option<bool>

Try get bool from value

Source

pub fn as_int(&self) -> Option<i32>

Try get int from value

Source

pub fn as_float(&self) -> Option<f64>

Try get float from value

Source

pub fn as_number(&self) -> Option<f64>

Try get any number from value

Source

pub fn is_null(&self) -> bool

Returns if the value is the JavaScript null value.

Source

pub fn is_undefined(&self) -> bool

Returns if the value is the JavaScript undefined value.

Source

pub fn is_bool(&self) -> bool

Check if the value is a bool

Source

pub fn is_int(&self) -> bool

Check if the value is an int

Source

pub fn is_float(&self) -> bool

Check if the value is a float

Source

pub fn is_number(&self) -> bool

Check if the value is an any number

Source

pub fn is_string(&self) -> bool

Check if the value is a string

Source

pub fn is_symbol(&self) -> bool

Check if the value is a symbol

Source

pub fn is_object(&self) -> bool

Check if the value is an object

Source

pub fn is_module(&self) -> bool

Check if the value is a module

Source

pub fn is_array(&self) -> bool

Check if the value is an array

Source

pub fn is_function(&self) -> bool

Check if the value is a function

Source

pub fn is_constructor(&self) -> bool

Check if the value is a constructor function

Source

pub fn is_promise(&self) -> bool

Check if the value is a promise.

Source

pub fn is_exception(&self) -> bool

Check if the value is an exception

Source

pub fn is_error(&self) -> bool

Check if the value is an error

Source

pub fn as_value(&self) -> &Value<'js>

Reference as value

Source

pub fn get<T>(&self) -> Result<T, Error>
where T: FromJs<'js>,

Convert from value to specified type

Source

pub fn as_raw(&self) -> JSValue

Returns the raw C library JavaScript value.

Source

pub fn type_of(&self) -> Type

Get the type of value

Source

pub fn type_name(&self) -> &'static str

Get the name of type

Source

pub unsafe fn ref_string(&self) -> &String<'js>

Interpret as String

§Safety

You should be sure that the value already is of required type before to do it.

Source

pub fn as_string(&self) -> Option<&String<'js>>

Try reinterpret as String

Source

pub unsafe fn ref_symbol(&self) -> &Symbol<'js>

Interpret as Symbol

§Safety

You should be sure that the value already is of required type before to do it.

Source

pub fn as_symbol(&self) -> Option<&Symbol<'js>>

Try reinterpret as Symbol

Source

pub unsafe fn ref_object(&self) -> &Object<'js>

Interpret as Object

§Safety

You should be sure that the value already is of required type before to do it.

Source

pub fn as_object(&self) -> Option<&Object<'js>>

Try reinterpret as Object

Source

pub unsafe fn ref_function(&self) -> &Function<'js>

Interpret as Function

§Safety

You should be sure that the value already is of required type before to do it.

Source

pub fn as_function(&self) -> Option<&Function<'js>>

Try reinterpret as Function

Source

pub unsafe fn ref_constructor(&self) -> &Constructor<'js>

Interpret as Constructor

§Safety

You should be sure that the value already is of required type before to do it.

Source

pub fn as_constructor(&self) -> Option<&Constructor<'js>>

Try reinterpret as Constructor

Source

pub unsafe fn ref_promise(&self) -> &Promise<'js>

Interpret as Promise

§Safety

You should be sure that the value already is of required type before to do it.

Source

pub fn as_promise(&self) -> Option<&Promise<'js>>

Try reinterpret as Promise

Source

pub unsafe fn ref_array(&self) -> &Array<'js>

Interpret as Array

§Safety

You should be sure that the value already is of required type before to do it.

Source

pub fn as_array(&self) -> Option<&Array<'js>>

Try reinterpret as Array

Source

pub unsafe fn ref_exception(&self) -> &Exception<'js>

Interpret as Exception

§Safety

You should be sure that the value already is of required type before to do it.

Source

pub fn as_exception(&self) -> Option<&Exception<'js>>

Try reinterpret as Exception

Source

pub unsafe fn ref_big_int(&self) -> &BigInt<'js>

Interpret as BigInt

§Safety

You should be sure that the value already is of required type before to do it.

Source

pub fn as_big_int(&self) -> Option<&BigInt<'js>>

Try reinterpret as BigInt

Trait Implementations§

Source§

impl<'js, C> Clone for Class<'js, C>
where C: JsClass<'js>,

Source§

fn clone(&self) -> Class<'js, C>

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<'js, C> Deref for Class<'js, C>
where C: JsClass<'js>,

Source§

type Target = Object<'js>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Class<'js, C> as Deref>::Target

Dereferences the value.
Source§

impl<'js, C> FromJs<'js> for Class<'js, C>
where C: JsClass<'js>,

Source§

fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> Result<Class<'js, C>, Error>

Source§

impl<'js, C> Hash for Class<'js, C>
where C: JsClass<'js>,

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'js, C> IntoJs<'js> for Class<'js, C>
where C: JsClass<'js>,

Source§

fn into_js(self, _ctx: &Ctx<'js>) -> Result<Value<'js>, Error>

Source§

impl<'js, C> JsLifetime<'js> for Class<'js, C>
where C: JsClass<'js> + JsLifetime<'js>, <C as JsLifetime<'js>>::Changed<'to>: for<'to> JsClass<'to>,

Source§

type Changed<'to> = Class<'to, <C as JsLifetime<'js>>::Changed<'to>>

The target which has the same type as a Self but with another lifetime 't
Source§

impl<'js, C> PartialEq for Class<'js, C>
where C: JsClass<'js>,

Source§

fn eq(&self, other: &Class<'js, C>) -> 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<'js, T> Trace<'js> for Class<'js, T>
where T: JsClass<'js>,

Source§

fn trace<'a>(&self, tracer: Tracer<'a, 'js>)

Source§

impl<'js, C> Eq for Class<'js, C>
where C: JsClass<'js>,

Auto Trait Implementations§

§

impl<'js, C> Freeze for Class<'js, C>

§

impl<'js, C> RefUnwindSafe for Class<'js, C>
where C: RefUnwindSafe,

§

impl<'js, C> !Send for Class<'js, C>

§

impl<'js, C> !Sync for Class<'js, C>

§

impl<'js, C> Unpin for Class<'js, C>
where C: Unpin,

§

impl<'js, C> !UnwindSafe for Class<'js, C>

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<'js, T> AsProperty<'js, T> for T
where T: IntoJs<'js>,

Source§

fn config( self, ctx: &Ctx<'js>, ) -> Result<(i32, Value<'js>, Value<'js>, Value<'js>), Error>

Property configuration 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 u8)

🔬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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

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

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'js, T> FromParam<'js> for T
where T: FromJs<'js>,

Source§

fn param_requirement() -> ParamRequirement

The parameters requirements this value requires.
Source§

fn from_param<'a>(params: &mut ParamsAccessor<'a, 'js>) -> Result<T, Error>

Convert from a parameter value.
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<'js, T> IntoArg<'js> for T
where T: IntoJs<'js>,

Source§

fn num_args(&self) -> usize

The number of arguments this value produces.
Source§

fn into_arg(self, args: &mut Args<'js>) -> Result<(), Error>

Convert the value into an argument.
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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, 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.