Skip to content

Please explain why subscribers doesn't catch data, if the corresponding structs doesn't hold them #9

Description

@Guelakais

following example:

pub struct ParamSubscriptionNode {
    #[allow(unused)]
    subscriber: WorkerSubscription<StringMsg, Option<String>>,
    worker: Worker<Option<String>>,
}
impl ParamSubscriptionNode {
    fn new(context: &Executor) -> Result<Self, RclrsError> {
        let node = context.create_node("simple_subscription")?;
        let worker = node.create_worker(None);
        let subscriber = worker
            .create_subscription::<StringMsg, _>(
                &node
                    .declare_parameter("topic_name")
                    .default(Arc::from("publish_hello"))
                    .description("The name of the topic to be subscribed on. Has a default value.")
                    .mandatory()
                    .unwrap()
                    .get(),
                move |data: &mut Option<String>, msg: StringMsg| {
                    *data = Some(msg.data);
                },
            )
            .unwrap();
        Ok(Self { subscriber, worker })
    }
}

As you can see, the struct has an entry with the #[allow(unused)] macro above it, which tells that the entry isn't used. Normally, entry's that aren't used can be dropped, because they're not needed. In this particular case, the entry is needed, because without the subscriber entry, the logger from:

fn main() -> Result<(), RclrsError> {
    let mut executor = Context::default_from_env()?.create_basic_executor();
    let node = ParamSubscriptionNode::new(&executor)?;
    thread::spawn(move || {
        loop {
            thread::sleep(Duration::from_secs(1));
            drop(node.worker.run(|data: &mut Option<String>| {
                println!(
                    "{}",
                    match data {
                        Some(data) => data,
                        None => "No message available yet.",
                    }
                )
            }));
        }
    });
    executor.spin(SpinOptions::default()).first_error()
}

would always tell "No message available yet." even if they're catched succesfully by the worker. Why is that?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions