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,
impl<T> ClassBuilder<T>where
T: 'static,
sourcepub fn with_default() -> Self
pub fn with_default() -> Self
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();
sourcepub fn with_constructor<F, Args>(f: F) -> Self
pub fn with_constructor<F, Args>(f: F) -> Self
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();
sourcepub fn set_constructor<F, Args>(self, f: F) -> Self
pub fn set_constructor<F, Args>(self, f: F) -> Self
sourcepub fn set_equality_check<F>(self, f: F) -> Self
pub fn set_equality_check<F>(self, f: F) -> Self
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();
sourcepub fn set_into_iter<F, I, V>(self, f: F) -> Self
pub fn set_into_iter<F, I, V>(self, f: F) -> Self
Set a method to convert instances into iterators
sourcepub fn with_iter<V>(self) -> Selfwhere
T: IntoIterator<Item = V> + Clone,
<T as IntoIterator>::IntoIter: Clone + Send + Sync + 'static,
V: ToPolarResult,
pub fn with_iter<V>(self) -> Selfwhere
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
sourcepub fn with_equality_check(self) -> Selfwhere
T: PartialEq<T>,
pub fn with_equality_check(self) -> Selfwhere
T: PartialEq<T>,
sourcepub fn add_attribute_getter<F, R>(self, name: &'static str, f: F) -> Self
pub fn add_attribute_getter<F, R>(self, name: &'static str, f: F) -> Self
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();
sourcepub fn add_constant<V: ToPolar + Clone + Send + Sync + 'static>(
self,
value: V,
name: &'static str
) -> Self
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.
sourcepub fn add_method<F, Args, R>(self, name: &'static str, f: F) -> Selfwhere
Args: FromPolarList,
F: Method<T, Args, Result = R>,
R: ToPolarResult + 'static,
pub fn add_method<F, Args, R>(self, name: &'static str, f: F) -> Selfwhere
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)
sourcepub fn add_iterator_method<F, Args, I>(self, name: &'static str, f: F) -> Selfwhere
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,
pub fn add_iterator_method<F, Args, I>(self, name: &'static str, f: F) -> Selfwhere
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.
sourcepub fn add_class_method<F, Args, R>(self, name: &'static str, f: F) -> Selfwhere
F: Function<Args, Result = R>,
Args: FromPolarList,
R: ToPolarResult + 'static,
pub fn add_class_method<F, Args, R>(self, name: &'static str, f: F) -> Selfwhere
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
Trait Implementations§
source§impl<T: Clone> Clone for ClassBuilder<T>
impl<T: Clone> Clone for ClassBuilder<T>
source§fn clone(&self) -> ClassBuilder<T>
fn clone(&self) -> ClassBuilder<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more