pub trait System<Registry, Args: FromRegistry<Registry>> {
type Return;
// Required method
fn apply(&self, registry: &Registry) -> Self::Return;
}
Expand description
A system is a trait that allows a function to be called giving only a registry and letting the compiler figure out what needs to be extracted from that registry in order to call the function
use aanyx::system::System;
use std::rc::Rc;
struct Data<T>{ data: Rc<T> }
// Simulate other data
struct Location {}
struct Person{ name: String, age: u8 }
fn old_enough( person: Data<Person> ) -> bool { person.data.age > 30 }
let registry0 = ( Rc::new(Person{ name: String::from("Alice"), age: 24u8 }), Location { /* Location data */ }) ;
let registry1 = ( Rc::new(Person{ name: String::from("Bob"), age: 31u8 }), Location { /* Location data */ });
let registry2 = ( Rc::new(Person{ name: String::from("Charlie"), age: 1u8 }), Location { /* Location data */ });
assert_eq!( old_enough.apply( ®istry0 ), false );
assert_eq!( old_enough.apply( ®istry1 ), true );
assert_eq!( old_enough.apply( ®istry2 ), false );
Closures
A closure can be made into a System
only if the closure accepts no arguments
use aanyx::system::System;
fn always_true() -> bool { true }
let always_true_wrapper = ||{ always_true.apply( &()) };
// The &() is required because the `System.apply` expects an argument although the closure has 0 arguments
assert!( always_true_wrapper.apply( &() ) );
use aanyx::system::System;
use std::rc::Rc;
let registry = Rc::new(());
let registry_copy = Rc::clone( ®istry );
fn always_true() -> bool { true }
let always_true_wrapper = move ||{ always_true.apply( ®istry_copy ) };
assert!( always_true_wrapper.apply( ®istry ))
Performance analysis
Benchamrks performed suggests that the overhead of calling a system insted of the original function is negligible.
Async programming
Actually the system supports async functions, but arguments extraction from registry is not async.
Safety
Calling a unsafe function through the System
trait will not generate any new unsafeties, but the function will still be unsafe.