pub trait ToPolar {
// Required method
fn to_polar(self) -> PolarValue;
}
Expand description
Convert Rust types to Polar types.
This trait is automatically implemented for any type that implements the PolarClass
marker
trait, which should be preferred. You can use the PolarClass
derive
macro to derive that trait.
For non-primitive types, the instance will be stored on the provided Host
.
Trait bounds
The default implementation for PolarClass
requires types to be Send
and Sync
, since
it is possible to store a ToPolar
value on an Oso
instance which can be
shared between threads.
ToPolar
implementors must also be concrete, sized types without any borrows.
Examples
Convert a primitive type into a polar value:
use oso::{PolarValue, ToPolar};
let string = "This is a string";
let value = string.to_polar();
assert_eq!(value, PolarValue::String(string.into()));
Convert a custom type into a polar value:
use oso::{PolarValue, PolarClass, ToPolar};
#[derive(PolarClass)]
struct MyClass;
let class = MyClass;
let value = class.to_polar();
assert!(matches!(value, PolarValue::Instance(_)));
Required Methods§
sourcefn to_polar(self) -> PolarValue
fn to_polar(self) -> PolarValue
Convert this value into a Polar value.