[−][src]Struct rosy::mixin::Class
An instance of Ruby's Class
type.
Examples
Class inheritance can be expressed in terms of logical comparison operators:
use rosy::Class; assert!(Class::object() < Class::basic_object()); assert!(Class::exception() < Class::object()); assert!(Class::arg_error() < Class::exception());
This very closely resembles Ruby's syntax for subclassing:
class Mammal
def breathe
puts "inhale and exhale"
end
end
class Cat < Mammal
def speak
puts "meow"
end
end
Methods
impl Class
[src]
pub fn def(name: impl Into<SymbolId>) -> Result<Self, DefMixinError>
[src]
Defines a new top-level class with name
.
Examples
Defining a new class is straightforward:
let my_object = rosy::Class::def("MyObject").unwrap();
Attempting to define an existing class will result in an error:
use rosy::Class; let array = Class::def("Array").unwrap_err().existing_object(); assert_eq!(Class::array(), array.unwrap());
pub fn get(name: impl Into<SymbolId>) -> Option<Self>
[src]
Retrieves an existing top-level Class
defined by name
.
pub unsafe fn get_unchecked(name: impl Into<SymbolId>) -> Self
[src]
Retrieves an existing top-level Class
defined by name
.
Safety
This method does not:
- Check whether an item for
name
exists (an exception will be thrown if this is the case) - Check whether the returned item for
name
is actually aClass
pub fn get_or_def(name: impl Into<SymbolId>) -> Result<Self, DefMixinError>
[src]
Retrieves an existing top-level Class
defined by name
or defines one
if it doesn't exist.
pub fn new_instance(self) -> Result<AnyObject>
[src]
Creates a new instance without arguments.
pub unsafe fn new_instance_unchecked(self) -> AnyObject
[src]
Creates a new instance without arguments.
Safety
An exception may be thrown if the class expected arguments.
pub fn new_instance_with<O: Object>(self, args: &[O]) -> Result<AnyObject>
[src]
Creates a new instance from args
.
pub unsafe fn new_instance_with_unchecked<O: Object>(
self,
args: &[O]
) -> AnyObject
[src]
self,
args: &[O]
) -> AnyObject
Creates a new instance from args
.
Safety
An exception may be thrown if the class expected arguments.
pub fn superclass(self) -> Class
[src]
Returns the parent class of self
.
pub fn subclass(self, name: impl Into<SymbolId>) -> Result<Self, DefMixinError>
[src]
Defines a new subclass of self
with name
.
pub fn subclass_under(
self,
namespace: impl Mixin,
name: impl Into<SymbolId>
) -> Result<Self, DefMixinError>
[src]
self,
namespace: impl Mixin,
name: impl Into<SymbolId>
) -> Result<Self, DefMixinError>
Defines a subclass of self
under namespace
with name
.
pub fn inheritance(self, other: Class) -> Inheritance
[src]
Returns the inheritance relationship between self
and other
.
pub fn inherits(self, other: Class) -> bool
[src]
Returns whether the relationship between self
and other
is A < B
.
pub fn name(self) -> String
[src]
Returns the name of self
.
pub fn def_method<N, F>(self, name: N, f: F) -> Result where
N: Into<SymbolId>,
F: MethodFn,
[src]
N: Into<SymbolId>,
F: MethodFn,
Defines a method for name
on self
that calls f
when invoked.
Note: This method can be unwieldy to use and so it is recommended to
instead call the convenience macro def_method!
.
Note that MethodFn
functions can return any type that implements
the Object
trait.
Unfortunately, because MethodFn
is defined on extern "C" fn
types
and these function declarations don't implicitly resolve to those types,
an as
cast is required for f
.
Examples
Every method takes this
(equivalent of self
) as the first argument,
which can then be followed with up to 15 arguments:
use rosy::prelude::*; let class = Class::array(); let array = Array::from_slice(&[String::from("hello")]); extern "C" fn my_eq(this: AnyObject, that: AnyObject) -> AnyObject { AnyObject::from(this == that) } class.def_method("my_eq?", my_eq as extern fn(_, _) -> _).unwrap(); assert!(array.call_with("my_eq?", &[array]).unwrap().is_true());
Passing in the wrong number of arguments will result in an
ArgumentError
exception being raised:
assert!(array.call("my_eq?").unwrap_err().is_arg_error());
Variable Arguments
There are two ways of taking in a variable number of arguments.
The first is by taking a pointer and a length:
use std::os::raw::c_int; use std::slice::from_raw_parts; use rosy::prelude::*; unsafe extern "C" fn eq_all(this: AnyObject, len: c_int, ptr: *const AnyObject) -> AnyObject { let slice = from_raw_parts(ptr, len as usize); for &obj in slice { if obj != this { return false.into(); } } true.into() } let class = Class::string(); let string = String::from("hellooo"); class.def_method("eq_all?", eq_all as unsafe extern fn(_, _, _) -> _); let args = [string, String::from("byeee")]; assert!(string.call_with("eq_all?", &args).unwrap().is_false());
The second is by taking an Array
as an argument:
use rosy::prelude::*; unsafe extern "C" fn joining(this: AnyObject, args: Array) -> String { args.join(String::cast_unchecked(this)) } let joining: unsafe extern fn(_, _) -> _ = joining; let class = Class::string(); class.def_method("joining", joining).unwrap(); let string = String::from(", "); let output = string.call_with("joining", &[string, string]).unwrap(); assert_eq!(output, ", , , ");
pub unsafe fn def_method_unchecked<N, F>(self, name: N, f: F) where
N: Into<SymbolId>,
F: MethodFn,
[src]
N: Into<SymbolId>,
F: MethodFn,
Defines a method for name
on self
that calls f
when invoked.
Note: This method can be unwieldy to use and so it is recommended to
instead call the convenience macro def_method_unchecked!
.
See def_method
for usage info.
Safety
The caller must ensure that self
is not frozen or else a FrozenError
exception will be raised.
impl Class
[src]
Built-in classes.
pub fn rust_object() -> Self
[src]
Returns the RustObject
class.
This class can be used when simply wrapping Rust data.
pub fn basic_object() -> Self
[src]
The BasicObject
class.
pub fn object() -> Self
[src]
The Object
class.
pub fn array() -> Self
[src]
The Array
class.
pub fn binding() -> Self
[src]
The Binding
class.
pub fn class() -> Self
[src]
The Class
class.
pub fn cont() -> Self
[src]
The Cont
class.
pub fn data() -> Self
[src]
The Data
class.
pub fn dir() -> Self
[src]
The Dir
class.
pub fn encoding() -> Self
[src]
The Encoding
class.
pub fn enumerator() -> Self
[src]
The Enumerator
class.
pub fn false_class() -> Self
[src]
The FalseClass
class.
pub fn file() -> Self
[src]
The File
class.
pub fn complex() -> Self
[src]
The Complex
class.
pub fn float() -> Self
[src]
The Float
class.
pub fn hash() -> Self
[src]
The Hash
class.
pub fn io() -> Self
[src]
The IO
class.
pub fn integer() -> Self
[src]
The Integer
class.
pub fn mtch() -> Self
[src]
The Match
class.
pub fn method() -> Self
[src]
The Method
class.
pub fn module() -> Self
[src]
The Module
class.
pub fn name_error_msg() -> Self
[src]
The NameErrorMesg
class.
pub fn nil() -> Self
[src]
The NilClass
class.
pub fn numeric() -> Self
[src]
The Numeric
class.
pub fn proc() -> Self
[src]
The Proc
class.
pub fn random() -> Self
[src]
The Random
class.
pub fn range() -> Self
[src]
The Range
class.
pub fn rational() -> Self
[src]
The Rational
class.
pub fn regexp() -> Self
[src]
The Regexp
class.
pub fn stat() -> Self
[src]
The Stat
class.
pub fn string() -> Self
[src]
The String
class.
pub fn strukt() -> Self
[src]
The Struct
class.
pub fn symbol() -> Self
[src]
The Symbol
class.
pub fn thread() -> Self
[src]
The Thread
class.
pub fn time() -> Self
[src]
The Time
class.
pub fn true_class() -> Self
[src]
The TrueClass
class.
pub fn unbound_method() -> Self
[src]
The UnboundMethod
class.
pub fn ruby_vm() -> Self
[src]
The RubyVM
class.
pub fn instr_seq() -> Self
[src]
The RubyVM::InstructionSequence
class.
pub fn exception() -> Self
[src]
The Exception
class.
pub fn standard_error() -> Self
[src]
The StandardError
class.
pub fn system_exit() -> Self
[src]
The SystemExit
class.
pub fn interrupt() -> Self
[src]
The Interrupt
class.
pub fn signal() -> Self
[src]
The Signal
class.
pub fn fatal() -> Self
[src]
The Fatal
class.
pub fn arg_error() -> Self
[src]
The ArgumentError
class.
pub fn eof_error() -> Self
[src]
The EOFError
class.
pub fn index_error() -> Self
[src]
The IndexError
class.
pub fn stop_iteration() -> Self
[src]
The StopIteration
class.
pub fn key_error() -> Self
[src]
The KeyError
class.
pub fn range_error() -> Self
[src]
The RangeError
class.
pub fn io_error() -> Self
[src]
The IOError
class.
pub fn runtime_error() -> Self
[src]
The RuntimeError
class.
pub fn frozen_error() -> Self
[src]
The FrozenError
class.
pub fn security_error() -> Self
[src]
The SecurityError
class.
pub fn system_call_error() -> Self
[src]
The SystemCallError
class.
pub fn thread_error() -> Self
[src]
The ThreadError
class.
pub fn type_error() -> Self
[src]
The TypeError
class.
pub fn zero_div_error() -> Self
[src]
The ZeroDivError
class.
pub fn not_imp_error() -> Self
[src]
The NotImpError
class.
pub fn no_mem_error() -> Self
[src]
The NoMemError
class.
pub fn no_method_error() -> Self
[src]
The NoMethodError
class.
pub fn float_domain_error() -> Self
[src]
The FloatDomainError
class.
pub fn local_jump_error() -> Self
[src]
The LocalJumpError
class.
pub fn sys_stack_error() -> Self
[src]
The SysStackError
class.
pub fn regexp_error() -> Self
[src]
The RegexpError
class.
pub fn encoding_error() -> Self
[src]
The EncodingError
class.
pub fn enc_compat_error() -> Self
[src]
The EncCompatError
class.
pub fn script_error() -> Self
[src]
The ScriptError
class.
pub fn name_error() -> Self
[src]
The NameError
class.
pub fn syntax_error() -> Self
[src]
The SyntaxError
class.
pub fn load_error() -> Self
[src]
The LoadError
class.
pub fn math_domain_error() -> Self
[src]
The MathDomainError
class.
Trait Implementations
impl Mixin for Class
[src]
fn to_class(self) -> Result<Class, Module>
[src]
fn to_module(self) -> Result<Module, Class>
[src]
fn include(self, module: Module)
[src]
Embeds the contents of module
in self
.
#[must_use]
fn includes(self, module: Module) -> bool
[src]
Returns whether self
or one of its ancestors includes module
. Read more
fn included_modules(self) -> Array<Module>
[src]
Returns an array of the modules included in self
.
fn prepend(self, module: Module)
[src]
Prepends module
in self
.
fn def_class(self, name: impl Into<SymbolId>) -> Result<Class, DefMixinError>
[src]
Defines a new class under self
with name
.
fn def_subclass(
self,
superclass: Class,
name: impl Into<SymbolId>
) -> Result<Class, DefMixinError>
[src]
self,
superclass: Class,
name: impl Into<SymbolId>
) -> Result<Class, DefMixinError>
Defines a new subclass of superclass
under self
with name
.
fn get_class(self, name: impl Into<SymbolId>) -> Option<Class>
[src]
Returns the existing Class
with name
in self
.
unsafe fn get_class_unchecked(self, name: impl Into<SymbolId>) -> Class
[src]
Returns the existing Class
with name
in self
. Read more
fn def_module(self, name: impl Into<SymbolId>) -> Result<Module, DefMixinError>
[src]
Defines a new module under self
with name
.
fn get_module(self, name: impl Into<SymbolId>) -> Option<Module>
[src]
Returns the existing Module
with name
in self
.
unsafe fn get_module_unchecked(self, name: impl Into<SymbolId>) -> Module
[src]
Returns the existing Module
with name
in self
. Read more
fn has_const(self, name: impl Into<SymbolId>) -> bool
[src]
Returns whether a constant for name
is defined in self
, or in some parent class if not self
. Read more
fn get_const(self, name: impl Into<SymbolId>) -> AnyObject
[src]
Returns the constant value for name
in self
, or in some parent class if not self
. Read more
fn set_const(self, name: impl Into<SymbolId>, val: impl Object)
[src]
Sets the value a constant for name
in self
to val
.
fn remove_const(self, name: impl Into<SymbolId>) -> AnyObject
[src]
Removes the constant value for name
, returning it. Read more
fn has_class_var(self, var: impl Into<SymbolId>) -> bool
[src]
Returns whether the class-level var
is defined in self
.
fn get_class_var(self, var: impl Into<SymbolId>) -> AnyObject
[src]
Returns the class-level var
in self
. Read more
fn set_class_var<K, V>(self, key: K, val: V) -> Result where
K: Into<SymbolId>,
V: Into<AnyObject>,
[src]
K: Into<SymbolId>,
V: Into<AnyObject>,
Sets the class-level var
in self
to val
.
unsafe fn set_class_var_unchecked<K, V>(self, key: K, val: V) where
K: Into<SymbolId>,
V: Into<AnyObject>,
[src]
K: Into<SymbolId>,
V: Into<AnyObject>,
Sets the class-level var for key
in self
to val
. Read more
fn attr_reader<N: Into<SymbolId>>(self, name: N) -> Result
[src]
Defines an read-only attribute on self
with name
.
unsafe fn attr_reader_unchecked<N: Into<SymbolId>>(self, name: N)
[src]
Defines an read-only attribute on self
with name
. Read more
fn attr_writer<N: Into<SymbolId>>(self, name: N) -> Result
[src]
Defines a write-only attribute on self
with name
.
unsafe fn attr_writer_unchecked<N: Into<SymbolId>>(self, name: N)
[src]
Defines a write-only attribute on self
with name
. Read more
fn attr_accessor<N: Into<SymbolId>>(self, name: N) -> Result
[src]
Defines a read-write attribute on self
with name
.
unsafe fn attr_accessor_unchecked<N: Into<SymbolId>>(self, name: N)
[src]
Defines a read-write attribute on self
with name
. Read more
unsafe fn eval_unchecked(self, args: impl EvalArgs) -> AnyObject
[src]
Evaluates args
in the context of self
. Read more
fn eval(self, args: impl EvalArgs) -> Result<AnyObject>
[src]
Evaluates args
in the context of self
, returning any raised exceptions. Read more
impl Object for Class
[src]
fn unique_id() -> Option<u128>
[src]
fn cast<A: Object>(obj: A) -> Option<Self>
[src]
fn ty(self) -> Ty
[src]
fn is_ty(self, ty: Ty) -> bool
[src]
unsafe fn from_raw(raw: usize) -> Self
[src]
Creates a new object from raw
without checking. Read more
unsafe fn cast_unchecked(obj: impl Object) -> Self
[src]
Casts obj
to Self
without checking its type.
fn into_any_object(self) -> AnyObject
[src]
Returns self
as an AnyObject
.
fn as_any_object(&self) -> &AnyObject
[src]
Returns a reference to self
as an AnyObject
.
fn as_any_slice(&self) -> &[AnyObject]
[src]
Returns self
as a reference to a single-element slice.
fn raw(self) -> usize
[src]
Returns the raw object pointer.
unsafe fn as_unchecked<O: Object>(&self) -> &O
[src]
Casts self
to O
without checking whether it is one.
unsafe fn into_unchecked<O: Object>(self) -> O
[src]
Converts self
to O
without checking whether it is one.
fn id(self) -> u64
[src]
Returns the object's identifier.
fn class(self) -> Class
[src]
Returns the Class
for self
.
fn singleton_class(self) -> Class
[src]
Returns the singleton Class
of self
, creating one if it doesn't exist already. Read more
fn mark(self)
[src]
Marks self
for Ruby to avoid garbage collecting it.
unsafe fn force_recycle(self)
[src]
Forces the garbage collector to free the contents of self
. Read more
fn def_singleton_method<N, F>(self, name: N, f: F) -> Result where
N: Into<SymbolId>,
F: MethodFn,
[src]
N: Into<SymbolId>,
F: MethodFn,
Defines a method for name
on the singleton class of self
that calls f
when invoked. Read more
unsafe fn def_singleton_method_unchecked<N, F>(self, name: N, f: F) where
N: Into<SymbolId>,
F: MethodFn,
[src]
N: Into<SymbolId>,
F: MethodFn,
Defines a method for name
on the singleton class of self
that calls f
when invoked. Read more
fn call(self, method: impl Into<SymbolId>) -> Result<AnyObject>
[src]
Calls method
on self
and returns the result.
unsafe fn call_unchecked(self, method: impl Into<SymbolId>) -> AnyObject
[src]
Calls method
on self
and returns the result. Read more
fn call_with(
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> Result<AnyObject>
[src]
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> Result<AnyObject>
Calls method
on self
with args
and returns the result.
unsafe fn call_with_unchecked(
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> AnyObject
[src]
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> AnyObject
Calls method
on self
with args
and returns the result. Read more
fn call_public(self, method: impl Into<SymbolId>) -> Result<AnyObject>
[src]
Calls the public method
on self
and returns the result.
unsafe fn call_public_unchecked(self, method: impl Into<SymbolId>) -> AnyObject
[src]
Calls the public method
on self
and returns the result. Read more
fn call_public_with(
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> Result<AnyObject>
[src]
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> Result<AnyObject>
Calls the public method
on self
with args
and returns the result.
unsafe fn call_public_with_unchecked(
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> AnyObject
[src]
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> AnyObject
Calls the public method
on self
with args
and returns the result. Read more
fn inspect(self) -> String
[src]
Returns a printable string representation of self
. Read more
fn to_s(self) -> String
[src]
Returns the result of calling the to_s
method on self
.
fn is_frozen(self) -> bool
[src]
Returns whether modifications can be made to self
.
fn freeze(self)
[src]
Freezes self
, preventing any further mutations.
fn is_eql<O: Object>(self, other: &O) -> bool
[src]
Returns whether self
is equal to other
in terms of the eql?
method. Read more
impl AsRef<AnyObject> for Class
[src]
impl Clone for Class
[src]
fn clone(&self) -> Class
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl From<Class> for AnyObject
[src]
impl Eq for Class
[src]
impl Copy for Class
[src]
impl PartialOrd<Class> for Class
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
fn lt(&self, other: &Self) -> bool
[src]
fn le(&self, other: &Self) -> bool
[src]
fn gt(&self, other: &Self) -> bool
[src]
fn ge(&self, other: &Self) -> bool
[src]
impl<O: Object> PartialEq<O> for Class
[src]
fn eq(&self, other: &O) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl Debug for Class
[src]
impl Display for Class
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,