Struct oso::ClassBuilder

source ·
pub struct ClassBuilder<T> { /* private fields */ }
Expand description

Builder for new Oso Class.

This helps you create a Class instance which holds metadata for your custom type. Using the builder, you can add attribute getters, class methods, instance methods, constants, iterator methods, override the class name, set the constructor or equality check.

You can create a new instance of ClassBuilder using PolarClass::get_polar_class_builder(), using Class::builder() or using one of the ClassBuilder::with_default() or ClassBuilder::with_constructor() methods.

Implementations§

source§

impl<T> ClassBuilder<T>
where T: 'static,

source

pub fn with_default() -> Self
where T: Default + Send + Sync,

Create a new class builder for a type that implements Default and use that as the constructor.

This is equivalent to setting the constructor to Default::default().

Examples

Basic usage:

use oso::ClassBuilder;

#[derive(Default)]
struct MyClass;

let class = ClassBuilder::<MyClass>::with_default().build();
source

pub fn with_constructor<F, Args>(f: F) -> Self
where F: Function<Args, Result = T>, T: Send + Sync, Args: FromPolarList,

Create a new class builder with a given constructor.

Examples

Basic usage:

use oso::ClassBuilder;

struct MyClass(u16);

let class = ClassBuilder::<MyClass>::with_constructor(|| MyClass(42)).build();
source

pub fn set_constructor<F, Args>(self, f: F) -> Self
where F: Function<Args, Result = T>, T: Send + Sync, Args: FromPolarList,

Set the constructor function to use for polar new statements.

Examples
use oso::ClassBuilder;
source

pub fn set_equality_check<F>(self, f: F) -> Self
where F: Fn(&T, &T) -> bool + Send + Sync + 'static,

Set an equality function to be used for polar == statements.

Examples

Basic usage:

use oso::ClassBuilder;

#[derive(Default)]
struct MyClass;

let class = ClassBuilder::<MyClass>::with_default()
    .set_equality_check(|left, right| true)
    .build();
source

pub fn set_into_iter<F, I, V>(self, f: F) -> Self
where F: Fn(&T) -> I + Send + Sync + 'static, I: Iterator<Item = V> + Clone + Send + Sync + 'static, V: ToPolarResult,

Set a method to convert instances into iterators

source

pub fn with_iter<V>(self) -> Self
where T: IntoIterator<Item = V> + Clone, <T as IntoIterator>::IntoIter: Clone + Send + Sync + 'static, V: ToPolarResult,

Use the existing IntoIterator implementation to convert instances into iterators

source

pub fn with_equality_check(self) -> Self
where T: PartialEq<T>,

Use PartialEq as the equality check for Polar == statements.

Examples

Basic usage:

use oso::ClassBuilder;

#[derive(Default, PartialEq)]
struct MyClass(u64);

let class = ClassBuilder::<MyClass>::with_default()
    .with_equality_check()
    .build();
source

pub fn add_attribute_getter<F, R>(self, name: &'static str, f: F) -> Self
where F: Fn(&T) -> R + Send + Sync + 'static, R: ToPolar, T: 'static,

Add an attribute getter.

An attribute getter allows you to write statements like foo.bar, where foo is a class instance and bar is an attribute.

Typically, if you use the PolarClass derive macro, you can use #[polar(attribute)] to generate this automatically.

Examples

Basic usage:

use oso::ClassBuilder;

#[derive(Default)]
struct MyClass {
    name: String,
    age: u32,
};

let class = ClassBuilder::<MyClass>::with_default()
    .add_attribute_getter("name", |instance| instance.name.clone())
    .add_attribute_getter("age", |instance| instance.age)
    .build();
source

pub fn name(self, name: &str) -> Self

Set the name of the polar class.

source

pub fn add_constant<V: ToPolar + Clone + Send + Sync + 'static>( self, value: V, name: &'static str ) -> Self

Add a RegisterHook on the class that will register the given constant once the class is registered.

source

pub fn add_method<F, Args, R>(self, name: &'static str, f: F) -> Self
where Args: FromPolarList, F: Method<T, Args, Result = R>, R: ToPolarResult + 'static,

Add a method for polar method calls like foo.plus(1) class.add_attribute_getter(“bar”, |instance, n| instance.foo + n)

source

pub fn add_iterator_method<F, Args, I>(self, name: &'static str, f: F) -> Self
where Args: FromPolarList, F: Method<T, Args>, F::Result: IntoIterator<Item = I>, I: ToPolarResult + 'static, <<F as Method<T, Args>>::Result as IntoIterator>::IntoIter: Iterator<Item = I> + Clone + Send + Sync + 'static, T: 'static,

A method that returns multiple values. Every element in the iterator returned by the method will be a separate polar return value.

source

pub fn add_class_method<F, Args, R>(self, name: &'static str, f: F) -> Self
where F: Function<Args, Result = R>, Args: FromPolarList, R: ToPolarResult + 'static,

A method that’s called on the type instead of an instance. eg Foo.pi

source

pub fn build(self) -> Class

Finish building a build the class

Trait Implementations§

source§

impl<T: Clone> Clone for ClassBuilder<T>

source§

fn clone(&self) -> ClassBuilder<T>

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for ClassBuilder<T>

§

impl<T> Send for ClassBuilder<T>
where T: Send,

§

impl<T> Sync for ClassBuilder<T>
where T: Sync,

§

impl<T> Unpin for ClassBuilder<T>
where T: Unpin,

§

impl<T> !UnwindSafe for ClassBuilder<T>

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<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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<T> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more