Trait oso::PolarClass

source ·
pub trait PolarClass: Sized + 'static {
    // Provided methods
    fn get_polar_class() -> Class { ... }
    fn get_polar_class_builder() -> ClassBuilder<Self> { ... }
}
Expand description

Classes that can be used as types in Polar policies.

Implementing this trait and Clone automatically makes the type FromPolar and ToPolar, so it can be used with Oso::is_allowed() calls.

The default implementation creates a class definition with no attributes or methods registered. Either use get_polar_class_builder() or the PolarClass proc macro to register attributes and methods.

Note: the returned Class still must be registered on an Oso instance using Oso::register_class().

Examples

Register polar class:

use oso::{Oso, PolarClass};

#[derive(PolarClass)]
struct MyClass {
    #[polar(attribute)]
    name: String,
}

let mut oso = Oso::new();
oso.register_class(MyClass::get_polar_class());

Register polar class with customisations:

use oso::{Oso, PolarClass};

#[derive(PolarClass, Default, PartialEq)]
struct MyClass {
    name: String,
}

let class = MyClass::get_polar_class_builder()
    .add_attribute_getter("name", |value| value.name.clone())
    .with_equality_check()
    .build();

let mut oso = Oso::new();
oso.register_class(class);

Provided Methods§

source

fn get_polar_class() -> Class

Returns the Class ready for registration

source

fn get_polar_class_builder() -> ClassBuilder<Self>

Returns the partially defined Class for this type.

Can still have methods added to it with add_method, and attributes with add_attribute_getter. Use Class::build to finish defining the type.

Object Safety§

This trait is not object safe.

Implementors§