help-rust/README.md

2.3 KiB
Raw Blame History

setup

In module common, I have:

pub enum Value {
  Bool(bool),
  Str(String),
  Int(isize),
  Date(DateTime<chrono::Utc>)
}

pub type Record<'a> = HashMap<&'a str, Value>;

(in a future version, Value will probably become recursive, with a variant Vec<Value> for example)

In module modules, I have:

pub trait Module {
  fn run(&self, record: &mut Record) -> Result<bool, ()>;
}

In module filters, I have:

pub struct Equals {  }

impl Equals {
  pub fn from_args(mut args: ModuleArgs) -> Equals {
    Equals {  }
  }
}

impl Module for Equals {
  fn run(&self, record: &mut Record) -> Result<bool, ()> {  }
}

goal, “naive-style”

What I really want (for a start) is that, in module modules:

struct Available {
  name: String,
  cons: Box<fn(ModuleArgs) -> dyn Module>
}

const available: &[Available] = &[
  Available { name: String::from("equals"), cons: Box::new(move |a| filters::Equals::from_args(a)) }
];

with available being an array of available modules, each having a name and a constructor.

However, I rather fail to see the reason for:

  • Box: In my case, it will be set forever, and be unmutable; and fn is a pointer anyway, so fixed-size…
  • &[…] notation: In this case, available is a constant, and I suppose its size is known at compile-time, so there shouldnt be the need for a reference.

problem

The above code gives me these errors:

  • first:

    error[E0277]: the size for values of type `dyn modules::Module` cannot be known at compilation time
      --> src/modules.rs:10:69
      |
    10 |   Available { name: String::from("equals"), cons: Box::new(move |a| filters::Equals::from_args(a)) }
      |                                                                     ^ doesn't have a size known at compile-time
      |
      = help: the trait `std::marker::Sized` is not implemented for `dyn modules::Module`
    
  • second:

    error[E0308]: mismatched types
      --> src/modules.rs:10:69
      |
    10 |   Available { name: String::from("equals"), cons: Box::new(move |a| filters::Equals::from_args(a)) }
      |                                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait modules::Module, found struct `filters::Equals`
      |
      = note: expected type `dyn modules::Module`
                  found type `filters::Equals`