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>,
impl<'js, C> Class<'js, C>where
C: JsClass<'js>,
Sourcepub fn instance(ctx: Ctx<'js>, value: C) -> Result<Class<'js, C>, Error>
pub fn instance(ctx: Ctx<'js>, value: C) -> Result<Class<'js, C>, Error>
Create a class from a Rust object.
Sourcepub fn instance_proto(
value: C,
proto: Object<'js>,
) -> Result<Class<'js, C>, Error>
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.
Sourcepub fn prototype(ctx: &Ctx<'js>) -> Result<Option<Object<'js>>, Error>
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.
Sourcepub fn create_constructor(
ctx: &Ctx<'js>,
) -> Result<Option<Constructor<'js>>, Error>
pub fn create_constructor( ctx: &Ctx<'js>, ) -> Result<Option<Constructor<'js>>, Error>
Create a constructor for the current class using its definition.
Sourcepub fn define(object: &Object<'js>) -> Result<(), Error>
pub fn define(object: &Object<'js>) -> Result<(), Error>
Defines the predefined constructor of this class, if there is one, onto the given object.
Sourcepub fn get_cell<'a>(&self) -> &'a JsCell<'js, C>
pub fn get_cell<'a>(&self) -> &'a JsCell<'js, C>
Returns a reference to the underlying object contained in a cell.
Sourcepub fn borrow_mut<'a>(&'a self) -> BorrowMut<'a, 'js, C>
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.
Sourcepub fn try_borrow<'a>(&'a self) -> Result<Borrow<'a, 'js, C>, Error>
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.
Sourcepub fn try_borrow_mut<'a>(&'a self) -> Result<BorrowMut<'a, 'js, C>, Error>
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.
Sourcepub fn into_inner(self) -> Object<'js>
pub fn into_inner(self) -> Object<'js>
Turns the class back into a generic object.
Sourcepub fn into_value(self) -> Value<'js>
pub fn into_value(self) -> Value<'js>
Turn the class into a value.
Sourcepub fn from_object(object: &Object<'js>) -> Option<Class<'js, C>>
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>>§
Sourcepub fn prop<K, V, P>(&self, key: K, prop: V) -> Result<(), Error>where
K: IntoAtom<'js>,
V: AsProperty<'js, P>,
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();
Sourcepub fn contains_key<K>(&self, k: K) -> Result<bool, Error>where
K: IntoAtom<'js>,
pub fn contains_key<K>(&self, k: K) -> Result<bool, Error>where
K: IntoAtom<'js>,
check whether the object contains a certain key.
Sourcepub fn set<K, V>(&self, key: K, value: V) -> Result<(), Error>
pub fn set<K, V>(&self, key: K, value: V) -> Result<(), Error>
Set a member of an object to a certain value
Sourcepub fn remove<K>(&self, key: K) -> Result<(), Error>where
K: IntoAtom<'js>,
pub fn remove<K>(&self, key: K) -> Result<(), Error>where
K: IntoAtom<'js>,
Remove a member of an object
Sourcepub fn keys<K>(&self) -> ObjectKeysIter<'js, K> ⓘwhere
K: FromAtom<'js>,
pub fn keys<K>(&self) -> ObjectKeysIter<'js, K> ⓘwhere
K: FromAtom<'js>,
Get own string enumerable property names of an object
Sourcepub fn own_keys<K>(&self, filter: Filter) -> ObjectKeysIter<'js, K> ⓘwhere
K: FromAtom<'js>,
pub fn own_keys<K>(&self, filter: Filter) -> ObjectKeysIter<'js, K> ⓘwhere
K: FromAtom<'js>,
Get own property names of an object
Sourcepub fn props<K, V>(&self) -> ObjectIter<'js, K, V> ⓘ
pub fn props<K, V>(&self) -> ObjectIter<'js, K, V> ⓘ
Get own string enumerable properties of an object
Sourcepub fn own_props<K, V>(&self, filter: Filter) -> ObjectIter<'js, K, V> ⓘ
pub fn own_props<K, V>(&self, filter: Filter) -> ObjectIter<'js, K, V> ⓘ
Get own properties of an object
Sourcepub fn values<K>(&self) -> ObjectValuesIter<'js, K> ⓘwhere
K: FromAtom<'js>,
pub fn values<K>(&self) -> ObjectValuesIter<'js, K> ⓘwhere
K: FromAtom<'js>,
Get own string enumerable property values of an object
Sourcepub fn own_values<K>(&self, filter: Filter) -> ObjectValuesIter<'js, K> ⓘwhere
K: FromAtom<'js>,
pub fn own_values<K>(&self, filter: Filter) -> ObjectValuesIter<'js, K> ⓘwhere
K: FromAtom<'js>,
Get own property values of an object
Sourcepub fn get_prototype(&self) -> Option<Object<'js>>
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.
Sourcepub fn set_prototype(&self, proto: Option<&Object<'js>>) -> Result<(), Error>
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.
Sourcepub fn is_instance_of(&self, class: impl AsRef<Value<'js>>) -> bool
pub fn is_instance_of(&self, class: impl AsRef<Value<'js>>) -> bool
Check instance of object
Sourcepub fn is_array_buffer(&self) -> bool
pub fn is_array_buffer(&self) -> bool
Returns whether the object is an instance of ArrayBuffer
.
Sourcepub unsafe fn ref_array_buffer(&self) -> &ArrayBuffer<'_>
pub unsafe fn ref_array_buffer(&self) -> &ArrayBuffer<'_>
Sourcepub fn as_array_buffer(&self) -> Option<&ArrayBuffer<'_>>
pub fn as_array_buffer(&self) -> Option<&ArrayBuffer<'_>>
Turn the object into an array buffer if the object is an instance of ArrayBuffer
.
pub fn is_typed_array<T>(&self) -> boolwhere
T: TypedArrayItem,
Sourcepub unsafe fn ref_typed_array<'a, T>(&'a self) -> &'a TypedArray<'js, T>where
T: TypedArrayItem,
pub unsafe fn ref_typed_array<'a, T>(&'a self) -> &'a TypedArray<'js, T>where
T: TypedArrayItem,
pub fn as_typed_array<T>(&self) -> Option<&TypedArray<'js, T>>where
T: TypedArrayItem,
Sourcepub fn instance_of<C>(&self) -> boolwhere
C: JsClass<'js>,
pub fn instance_of<C>(&self) -> boolwhere
C: JsClass<'js>,
Returns if the object is of a certain Rust class.
Methods from Deref<Target = Value<'js>>§
Sourcepub fn is_undefined(&self) -> bool
pub fn is_undefined(&self) -> bool
Returns if the value is the JavaScript undefined value.
Sourcepub fn is_function(&self) -> bool
pub fn is_function(&self) -> bool
Check if the value is a function
Sourcepub fn is_constructor(&self) -> bool
pub fn is_constructor(&self) -> bool
Check if the value is a constructor function
Sourcepub fn is_promise(&self) -> bool
pub fn is_promise(&self) -> bool
Check if the value is a promise.
Sourcepub fn is_exception(&self) -> bool
pub fn is_exception(&self) -> bool
Check if the value is an exception
Sourcepub fn get<T>(&self) -> Result<T, Error>where
T: FromJs<'js>,
pub fn get<T>(&self) -> Result<T, Error>where
T: FromJs<'js>,
Convert from value to specified type
Sourcepub unsafe fn ref_string(&self) -> &String<'js>
pub unsafe fn ref_string(&self) -> &String<'js>
Sourcepub unsafe fn ref_symbol(&self) -> &Symbol<'js>
pub unsafe fn ref_symbol(&self) -> &Symbol<'js>
Sourcepub unsafe fn ref_object(&self) -> &Object<'js>
pub unsafe fn ref_object(&self) -> &Object<'js>
Sourcepub unsafe fn ref_function(&self) -> &Function<'js>
pub unsafe fn ref_function(&self) -> &Function<'js>
Sourcepub fn as_function(&self) -> Option<&Function<'js>>
pub fn as_function(&self) -> Option<&Function<'js>>
Try reinterpret as Function
Sourcepub unsafe fn ref_constructor(&self) -> &Constructor<'js>
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.
Sourcepub fn as_constructor(&self) -> Option<&Constructor<'js>>
pub fn as_constructor(&self) -> Option<&Constructor<'js>>
Try reinterpret as Constructor
Sourcepub unsafe fn ref_promise(&self) -> &Promise<'js>
pub unsafe fn ref_promise(&self) -> &Promise<'js>
Sourcepub fn as_promise(&self) -> Option<&Promise<'js>>
pub fn as_promise(&self) -> Option<&Promise<'js>>
Try reinterpret as Promise
Sourcepub unsafe fn ref_exception(&self) -> &Exception<'js>
pub unsafe fn ref_exception(&self) -> &Exception<'js>
Sourcepub fn as_exception(&self) -> Option<&Exception<'js>>
pub fn as_exception(&self) -> Option<&Exception<'js>>
Try reinterpret as Exception
Sourcepub unsafe fn ref_big_int(&self) -> &BigInt<'js>
pub unsafe fn ref_big_int(&self) -> &BigInt<'js>
Sourcepub fn as_big_int(&self) -> Option<&BigInt<'js>>
pub fn as_big_int(&self) -> Option<&BigInt<'js>>
Try reinterpret as BigInt
Trait Implementations§
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>,
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>,
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<'js, T> AsProperty<'js, T> for Twhere
T: IntoJs<'js>,
impl<'js, T> AsProperty<'js, T> for Twhere
T: IntoJs<'js>,
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§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<'js, T> FromParam<'js> for Twhere
T: FromJs<'js>,
impl<'js, T> FromParam<'js> for Twhere
T: FromJs<'js>,
Source§fn param_requirement() -> ParamRequirement
fn param_requirement() -> ParamRequirement
Source§fn from_param<'a>(params: &mut ParamsAccessor<'a, 'js>) -> Result<T, Error>
fn from_param<'a>(params: &mut ParamsAccessor<'a, 'js>) -> Result<T, Error>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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