help-rust/README.md

92 lines
2.3 KiB
Markdown
Raw Normal View History

2019-11-17 15:30:48 +01:00
## setup
2019-11-17 16:50:48 +01:00
In module `common`, I have:
```rust
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)
2019-11-17 15:30:48 +01:00
In module `modules`, I have:
```rust
pub trait Module {
fn run(&self, record: &mut Record) -> Result<bool, ()>;
}
```
In module `filters`, I have:
```rust
2019-11-17 16:16:25 +01:00
pub struct Equals { … }
2019-11-17 15:30:48 +01:00
2019-11-17 16:16:25 +01:00
impl Equals {
2019-11-17 15:30:48 +01:00
pub fn from_args(mut args: ModuleArgs) -> Equals {
Equals { … }
}
}
2019-11-17 16:16:25 +01:00
impl Module for Equals {
2019-11-17 15:30:48 +01:00
fn run(&self, record: &mut Record) -> Result<bool, ()> { … }
}
```
## goal, “naive-style”
What I really want (for a start) is that, in module `modules`:
```rust
struct Available {
2019-11-17 16:16:25 +01:00
name: String,
cons: Box<fn(ModuleArgs) -> dyn Module>
2019-11-17 15:30:48 +01:00
}
2019-11-17 16:50:48 +01:00
const available: &[Available] = &[
Available { name: String::from("equals"), cons: Box::new(move |a| filters::Equals::from_args(a)) }
2019-11-17 16:16:25 +01:00
];
2019-11-17 15:30:48 +01:00
```
2019-11-17 16:16:25 +01:00
with `available` being an array of available modules, each having a name and a constructor.
2019-11-17 15:30:48 +01:00
2019-11-17 16:50:48 +01:00
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.
2019-11-17 15:30:48 +01:00
## problem
2019-11-17 16:50:48 +01:00
The above code gives me these errors:
2019-11-17 16:16:25 +01:00
* first:
```
2019-11-17 16:50:48 +01:00
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`
2019-11-17 16:16:25 +01:00
```
* second:
```
2019-11-17 16:50:48 +01:00
error[E0308]: mismatched types
--> src/modules.rs:10:69
2019-11-17 16:16:25 +01:00
|
2019-11-17 16:50:48 +01:00
10 | Available { name: String::from("equals"), cons: Box::new(move |a| filters::Equals::from_args(a)) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait modules::Module, found struct `filters::Equals`
2019-11-17 16:16:25 +01:00
|
2019-11-17 16:50:48 +01:00
= note: expected type `dyn modules::Module`
found type `filters::Equals`
2019-11-17 16:16:25 +01:00
```