new-home-application 0.1.3

New Home iot application framework. Meant to build application for the New Home Core
Documentation
use crate::method::method_structure::{MethodArguments, MethodResult};

/// Gives the option to create a `MethodCallable` from a closure
/// Stores the closure internally
/// When the `call` method is called, it will forward the `MethodArguments` to the closure
/// Returns the closures `MethodResult` when done
pub struct MethodCallableClosure<'a> {
    /// The closure that should be executed when the method is called
    closure: Box<dyn Fn(MethodArguments) -> MethodResult + 'a>
}

/// The handler trait for methods
/// When implemented it can be used in for the `MethodManager` as the handler for a registered method
pub trait MethodCallable {
    /// Executes the internal logic of the method
    fn call(&self, arguments: MethodArguments) -> MethodResult;
}

impl<'a> MethodCallableClosure<'a> {
    pub fn new<C>(closure: C) -> Self where C: Fn(MethodArguments) -> MethodResult + 'a {
        Self {
            closure: Box::new(closure)
        }
    }
}

impl MethodCallable for MethodCallableClosure<'_> {
    fn call(&self, arguments: MethodArguments) -> MethodResult {
        (self.closure)(arguments)
    }
}

unsafe impl Send for MethodCallableClosure<'_> {}

unsafe impl Sync for MethodCallableClosure<'_> {}