Re: [PATCH v4 7/7] rust: workqueue: add examples

From: Konstantin Shelekhin
Date: Tue Oct 03 2023 - 16:13:34 EST


+//! #[pin_data]
+//! struct MyStruct {
+//! value: i32,
+//! #[pin]
+//! work: Work<MyStruct>,
+//! }
+//!
+//! impl_has_work! {
+//! impl HasWork<Self> for MyStruct { self.work }
+//! }
+//!
+//! impl MyStruct {
+//! fn new(value: i32) -> Result<Arc<Self>> {
+//! Arc::pin_init(pin_init!(MyStruct {
+//! value,
+//! work <- new_work!("MyStruct::work"),
+//! }))
+//! }
+//! }
+//!
+//! impl WorkItem for MyStruct {
+//! type Pointer = Arc<MyStruct>;
+//!
+//! fn run(this: Arc<MyStruct>) {
+//! pr_info!("The value is: {}", this.value);
+//! }
+//! }
+//!
+//! /// This method will enqueue the struct for execution on the system workqueue, where its value
+//! /// will be printed.
+//! fn print_later(val: Arc<MyStruct>) {
+//! let _ = workqueue::system().enqueue(val);
+//! }

I understand that this is highly opionated, but is it possible to make
the initialization less verbose?

Because the C version looks much, much cleaner and easier to grasp:

struct my_struct {
i32 value;
struct work_struct work;
};

void log_value(struct work_struct *work)
{
struct my_struct *s = container_of(work, struct my_struct, work);
pr_info("The value is: %d\n", s->value);
}

void print_later(struct my_struct &s)
{
INIT_WORK(&s->work, log_value);
schedule_work(&s->work);
}