Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser: auto_node macro #77

Merged
merged 1 commit into from
Nov 13, 2024
Merged

Parser: auto_node macro #77

merged 1 commit into from
Nov 13, 2024

Conversation

191220029
Copy link
Collaborator

@191220029 191220029 commented Nov 12, 2024

auto_node macro

The macro auto_node generates essential fields and implementation of traits for structs intended to represent Node in Dagrs. By applying this macro to a struct, it appends fields including id: dagrs::NodeId, name: dagrs::NodeName, input_channels: dagrs::InChannels, output_channels: dagrs::OutChannels, and action: dagrs::Action, and implements the dagrs::Node trait.

auto_node can only be marked on named struct or unit struct.

Examples

  • Mark auto_node on a named struct.
use dagrs::auto_node;
#[auto_node]
struct MyNode {/*Put your customized fields here.*/}

This will generate the following codes:

struct MyNode {
    id: dagrs::NodeId,
    name: String,
    input_channels: dagrs::InChannels,
    output_channels: dagrs::OutChannels,
    action: Box<dyn dagrs::Action>,
}
impl dagrs::Node for MyNode {
    fn id(&self) -> dagrs::NodeId {
        self.id
    }
    fn name(&self) -> dagrs::NodeName {
        self.name.clone()
    }
    fn input_channels(&mut self) -> &mut dagrs::InChannels {
        &mut self.input_channels
    }
    fn output_channels(&mut self) -> &mut dagrs::OutChannels {
        &mut self.output_channels
    }
    fn run(&mut self, env: std::sync::Arc<dagrs::EnvVar>) -> dagrs::Output {
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(async {
                self.action
                    .run(&mut self.input_channels, &self.output_channels, env)
                    .await
            })
    }
}
unsafe impl Send for MyNode {}
unsafe impl Sync for MyNode {}
  • Mark auto_node on a struct with generic & lifetime params.
use dagrs::auto_node;
#[auto_node]
struct MyNode<T, 'a> {
    my_field: Vec<T>,
    my_name: &'a str,
}

This will generate the following codes:

struct _MyNodeGeneric<'a, T> {
    my_field: Vec<T>,
    my_name: &'a str,
    id: dagrs::NodeId,
    name: String,
    input_channels: dagrs::InChannels,
    output_channels: dagrs::OutChannels,
    action: Box<dyn dagrs::Action>,
}
impl<'a, T> dagrs::Node for _MyNodeGeneric<'a, T> {
    fn id(&self) -> dagrs::NodeId {
        self.id
    }
    fn name(&self) -> dagrs::NodeName {
        self.name.clone()
    }
    fn input_channels(&mut self) -> &mut dagrs::InChannels {
        &mut self.input_channels
    }
    fn output_channels(&mut self) -> &mut dagrs::OutChannels {
        &mut self.output_channels
    }
    fn run(&mut self, env: std::sync::Arc<dagrs::EnvVar>) -> dagrs::Output {
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(async {
                self.action
                    .run(&mut self.input_channels, &self.output_channels, env)
                    .await
            })
    }
}
unsafe impl<'a, T> Send for _MyNodeGeneric<'a, T> {}
unsafe impl<'a, T> Sync for _MyNodeGeneric<'a, T> {}
  • Mark auto_node on a unit struct.
use dagrs::auto_node;
#[auto_node]
struct MyNode()

This will generate the following codes:

struct _MyUnitNode {
    id: dagrs::NodeId,
    name: String,
    input_channels: dagrs::InChannels,
    output_channels: dagrs::OutChannels,
    action: Box<dyn dagrs::Action>,
}
impl dagrs::Node for _MyUnitNode {
    fn id(&self) -> dagrs::NodeId {
        self.id
    }
    fn name(&self) -> dagrs::NodeName {
        self.name.clone()
    }
    fn input_channels(&mut self) -> &mut dagrs::InChannels {
        &mut self.input_channels
    }
    fn output_channels(&mut self) -> &mut dagrs::OutChannels {
        &mut self.output_channels
    }
    fn run(&mut self, env: std::sync::Arc<dagrs::EnvVar>) -> dagrs::Output {
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(async {
                self.action
                    .run(&mut self.input_channels, &self.output_channels, env)
                    .await
            })
    }
}
unsafe impl Send for _MyUnitNode {}
unsafe impl Sync for _MyUnitNode {}

Related issue

#74

@191220029 191220029 self-assigned this Nov 12, 2024
@191220029 191220029 requested a review from genedna November 12, 2024 02:43
@benjamin-747 benjamin-747 self-requested a review November 13, 2024 10:00
@genedna genedna merged commit a96fd4a into dagrs-dev:main Nov 13, 2024
3 checks passed
@191220029 191220029 deleted the dev branch December 24, 2024 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants