[][src]Struct rosy::Class

#[repr(transparent)]
pub struct Class<O = AnyObject> { /* fields omitted */ }

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 of<O: Classify>() -> Class<O>[src]

Returns the typed class of some type that implements Classify.

Examples

We can see from getting the untyped version of the Array class that they both point to the same class:

use rosy::{Array, Class};

let class = Class::of::<Array>();
assert_eq!(class, Class::array());

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 a Class

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.

impl<O: Object> Class<O>[src]

pub fn into_any_class(self) -> Class[src]

Converts self into an untyped class.

pub fn new_instance(self) -> Result<O>[src]

Creates a new instance without arguments.

pub unsafe fn new_instance_unchecked(self) -> O[src]

Creates a new instance without arguments.

Safety

An exception may be thrown if the class expected arguments.

pub fn new_instance_with<A: Object>(self, args: &[A]) -> Result<O>[src]

Creates a new instance from args.

pub unsafe fn new_instance_with_unchecked<A: Object>(self, args: &[A]) -> O[src]

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<Class, DefMixinError>[src]

Defines a new subclass of self with name.

pub fn subclass_under(
    self,
    namespace: impl Mixin,
    name: impl Into<SymbolId>
) -> Result<Class, DefMixinError>
[src]

Defines a subclass of self under namespace with name.

pub fn inheritance<P: Object>(self, other: Class<P>) -> Inheritance[src]

Returns the inheritance relationship between self and other.

pub fn inherits<P: Object>(self, other: Class<P>) -> 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<O>, 
[src]

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!.

About MethodFn

  • The first argument is always of type O. This means that if self is a typed class, then no casting is required within the method.

  • Up to 15 arguments may be passed. If more or a variable amount is needed, see the below examples.

  • They 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 may be followed by up to 15 arguments.

use rosy::prelude::*;

extern "C" fn my_eql(this: Array, that: AnyObject) -> AnyObject {
    AnyObject::from(this == that)
}
let my_eql: extern fn(_, _) -> _ = my_eql;

Class::of::<Array>()
    .def_method("my_eql?", my_eql)
    .unwrap();

let array: Array = (0..10).collect();
let value = unsafe { array.call_with("my_eql?", &[array]) };

assert!(value.is_true());

Passing in the wrong number of arguments will result in an ArgumentError exception being raised:

let result = unsafe { array.call_protected("my_eql?") };

assert!(result.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")];
let value = unsafe { string.call_with("eq_all?", &args) };

assert!(value.is_false());

The second is by taking an Array as an argument:

use rosy::prelude::*;

unsafe extern "C" fn joining(this: String, args: Array) -> String {
    args.join(this)
}
let joining: unsafe extern fn(_, _) -> _ = joining;

let class = Class::of::<String>();
class.def_method("joining", joining).unwrap();

let string = String::from(", ");
let joined = unsafe { string.call_with("joining", &[string, string]) };

assert_eq!(joined, ", , , ");

pub unsafe fn def_method_unchecked<N, F>(self, name: N, f: F) where
    N: Into<SymbolId>,
    F: MethodFn<O>, 
[src]

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 Classify for Class[src]

impl Mixin for 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<S: Object>(
    self,
    superclass: Class<S>,
    name: impl Into<SymbolId>
) -> Result<Class, DefMixinError>
[src]

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 Into<AnyObject>)[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]

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]

Sets the class-level var for key in self to val. Read more

fn def_attr_reader<N: Into<SymbolId>>(self, name: N) -> Result[src]

Defines an read-only attribute on self with name.

unsafe fn def_attr_reader_unchecked<N: Into<SymbolId>>(self, name: N)[src]

Defines an read-only attribute on self with name. Read more

fn def_attr_writer<N: Into<SymbolId>>(self, name: N) -> Result[src]

Defines a write-only attribute on self with name.

unsafe fn def_attr_writer_unchecked<N: Into<SymbolId>>(self, name: N)[src]

Defines a write-only attribute on self with name. Read more

fn def_attr_accessor<N: Into<SymbolId>>(self, name: N) -> Result[src]

Defines a read-write attribute on self with name.

unsafe fn def_attr_accessor_unchecked<N: Into<SymbolId>>(self, name: N)[src]

Defines a read-write attribute on self with name. Read more

unsafe fn eval(self, args: impl EvalArgs) -> AnyObject[src]

Evaluates args in the context of self. Read more

unsafe fn eval_protected(self, args: impl EvalArgs) -> Result<AnyObject>[src]

Evaluates args in the context of self, returning any raised exceptions. Read more

impl<O: Object> Object for Class<O>[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<Self>[src]

Returns the Class for self. Read more

fn singleton_class(self) -> Class<Self>[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<Self>, 
[src]

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<Self>, 
[src]

Defines a method for name on the singleton class of self that calls f when invoked. Read more

unsafe fn call(self, method: impl Into<SymbolId>) -> AnyObject[src]

Calls method on self and returns its output. Read more

unsafe fn call_protected(self, method: impl Into<SymbolId>) -> Result<AnyObject>[src]

Calls method on self and returns its output, or an exception if one is raised. Read more

unsafe fn call_with(
    self,
    method: impl Into<SymbolId>,
    args: &[impl Object]
) -> AnyObject
[src]

Calls method on self with args and returns its output. Read more

unsafe fn call_with_protected(
    self,
    method: impl Into<SymbolId>,
    args: &[impl Object]
) -> Result<AnyObject>
[src]

Calls method on self with args and returns its output, or an exception if one is raised. Read more

unsafe fn call_public(self, method: impl Into<SymbolId>) -> AnyObject[src]

Calls the public method on self and returns its output. Read more

unsafe fn call_public_protected(
    self,
    method: impl Into<SymbolId>
) -> Result<AnyObject>
[src]

Calls the public method on self and returns its output, or an exception if one is raised. Read more

unsafe fn call_public_with(
    self,
    method: impl Into<SymbolId>,
    args: &[impl Object]
) -> AnyObject
[src]

Calls the public method on self with args and returns its output. Read more

unsafe fn call_public_with_protected(
    self,
    method: impl Into<SymbolId>,
    args: &[impl Object]
) -> Result<AnyObject>
[src]

Calls the public method on self with args and returns its output, or an exception if one is raised. 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

fn get_attr<N: Into<SymbolId>>(self, name: N) -> AnyObject[src]

Returns the value for the attribute of self associated with name.

unsafe fn eval(self, args: impl EvalArgs) -> AnyObject[src]

Evaluates args in the context of self. Read more

unsafe fn eval_protected(self, args: impl EvalArgs) -> Result<AnyObject>[src]

Evaluates args in the context of self, returning any raised exceptions. Read more

impl<O: Object> Eq for Class<O>[src]

impl<O: Object, P: Object> PartialEq<P> for Class<O>[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl<O> Copy for Class<O>[src]

impl<O> Clone for Class<O>[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<O: Object, P: Object> PartialOrd<Class<P>> for Class<O>[src]

impl<O: Object> AsRef<AnyObject> for Class<O>[src]

impl<O: Object> From<Class<O>> for AnyObject[src]

impl<O: Object> Debug for Class<O>[src]

impl<O: Object> Display for Class<O>[src]

Auto Trait Implementations

impl<O = AnyObject> !Send for Class<O>

impl<O = AnyObject> !Sync for Class<O>

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]