Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Aug 2, 2024
1 parent 669e048 commit c7eb55f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ panduza-proc = { path = "lib/panduza-proc" }

tokio = { version = "1", features = ["full"] }

futures = "0.3.30"

[dev-dependencies]


Expand Down
16 changes: 11 additions & 5 deletions src/asyncv/msg/att_bool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod inner_msg_att_bool;
use inner_msg_att_bool::InnerAttBool;

use std::future::Future;
use std::sync::Arc;
use tokio::sync::Mutex;

Expand All @@ -11,17 +12,22 @@ pub use super::CoreMembers;
pub use super::OnMessageHandler;
pub use super::ReactorData;

pub trait OnChangeHandler: Send + Sync {
fn on_change(&self, new_value: bool);
}
// pub trait OnChangeHandler: Send + Sync {
// fn on_change(&self, new_value: bool);
// }

pub use inner_msg_att_bool::OnChangeHandlerFunction;

pub struct AttBool {
inner: Arc<Mutex<InnerAttBool>>,
}

impl AttBool {
pub async fn set_on_change_handler(&self, handler: Box<dyn OnChangeHandler>) {
self.inner.lock().await.set_on_change_handler(handler);
// pub async fn set_on_change_handler(&self, handler: Box<dyn OnChangeHandler>) {
// self.inner.lock().await.set_on_change_handler(handler);
// }
pub async fn on_change(&self, handler: OnChangeHandlerFunction) {
self.inner.lock().await.on_change(handler);
}

/// Set the value of the attribute
Expand Down
40 changes: 36 additions & 4 deletions src/asyncv/msg/att_bool/inner_msg_att_bool.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use futures::future::BoxFuture;
use futures::FutureExt;
use tokio::sync::Mutex;

use bytes::Bytes;

use crate::AttributeError;

use super::CoreMembers;
use super::OnChangeHandler;
// use super::OnChangeHandler;
use super::OnMessageHandler;

use monitor::Monitor;

// pub type OnChangeHandlerFunction = Arc<Box<dyn Future<Output = bool> + Send + Sync>>;
pub type OnChangeHandlerFunction = Pin<Box<dyn Future<Output = bool> + Send + Sync>>;
// pub type OnChangeHandlerFunction = BoxFuture<'static, ()>;

// type OnChangeHandlerFunction = Arc<dyn 'static + Send + Sync + Fn(bool) -> BoxFuture<'static, ()>>;

/// Inner implementation of the boolean message attribute
///
pub struct InnerAttBool {
Expand All @@ -21,8 +32,8 @@ pub struct InnerAttBool {
/// Requested value of the attribute (set by the user)
requested_value: Option<bool>,
/// Handler to call when the value change
on_change_handler: Option<Box<dyn OnChangeHandler>>,

// on_change_handler: Option<Box<dyn OnChangeHandler>>,
on_change_handler: Option<OnChangeHandlerFunction>,
set_ensure_lock: Arc<Monitor<bool>>,
}

Expand Down Expand Up @@ -76,7 +87,7 @@ impl InnerAttBool {
return self.value;
}

pub fn set_on_change_handler(&mut self, handler: Box<dyn OnChangeHandler>) {
pub fn on_change(&mut self, handler: OnChangeHandlerFunction) {
self.on_change_handler = Some(handler);
}

Expand All @@ -93,6 +104,27 @@ impl InnerAttBool {
impl OnMessageHandler for InnerAttBool {
fn on_message(&mut self, data: &Bytes) {
println!("boolean");

// OnChangeHandlerFunction

// let a = async { true };
// let b = || a;

// self.on_change_handler = Some(Arc::new(|| a));
// tokio::spawn(b.clone()());
// tokio::spawn(b());
// tokio::spawn(b());
// tokio::spawn(pp55);
// tokio::spawn(pp55);

// let pp: Pin<Box<dyn Future<Output = bool> + Send>> = async move { true }.boxed();
// tokio::spawn(pp);
// tokio::spawn(pp);
// tokio::spawn(pp);

// if let Some(handler) = self.on_change_handler.as_ref() {
// tokio::spawn(*(handler.clone()));
// }
if data.len() == 1 {
match data[0] {
b'1' => {
Expand Down

0 comments on commit c7eb55f

Please sign in to comment.