Box and array

master
Y 2019-11-17 16:16:25 +01:00
parent 74cd2c570c
commit b2adb834d4
1 changed files with 28 additions and 51 deletions

View File

@ -1,28 +1,3 @@
Intermediate problem:
```rust
pub struct Equals<'a> {
field: &'a str,
value: Value
}
impl Equals<'_> {
pub fn from_args(mut args: ModuleArgs) -> Equals {
Equals {
field: match args.remove("field") {
Some(Value::Str(s)) => s,
_ => panic!("The Equals filter needs a field to filter in “field”")
},
value: args.remove("value").expect("The Equals filter needs a reference value in “value”")
}
}
}
```
Error on `Some(Value::Str(s)) => s` even though `args` is mutable and trashed at the end of the function.
<hr>
## setup ## setup
In module `modules`, I have: In module `modules`, I have:
@ -36,15 +11,15 @@ pub trait Module {
In module `filters`, I have: In module `filters`, I have:
```rust ```rust
pub struct Equals<'a> { … } pub struct Equals { … }
impl Equals<'_> { impl Equals {
pub fn from_args(mut args: ModuleArgs) -> Equals { pub fn from_args(mut args: ModuleArgs) -> Equals {
Equals { … } Equals { … }
} }
} }
impl Module for Equals<'_> { impl Module for Equals {
fn run(&self, record: &mut Record) -> Result<bool, ()> { … } fn run(&self, record: &mut Record) -> Result<bool, ()> { … }
} }
``` ```
@ -55,37 +30,39 @@ What I really want (for a start) is that, in module `modules`:
```rust ```rust
struct Available { struct Available {
name: &'static str, name: String,
cons: fn(ModuleArgs) -> dyn Module cons: Box<fn(ModuleArgs) -> dyn Module>
} }
const available: (Available) = ( const available: [Available] = [
Available{ name: "equals", cons: move |a| filters::Equals::from_args(a) } Available { name: "equals", cons: Box.new(filters::Equals::from_args) }
); ];
``` ```
with `available` being a tuple of available modules, each having a name and a constructor. with `available` being an array of available modules, each having a name and a constructor.
## problem ## problem
The above code gives me two errors: The above code gives me these errors (which I suspect are hiding the next error about `"equals"` probably being `&str` instead of `String`…):
1. one I somewhat understand: * first:
``` ```
error[E0277]: the size for values of type `(dyn modules::Module + 'static)` cannot be known at compilation time error[E0423]: expected value, found struct `Box`
--> src/modules.rs:10:45 --> src/modules.rs:10:37
| |
10 | Available{ name: "equals", cons: move |a| filters::Equals::from_args(a) } 10 | Available { name: "equals", cons: Box.new(filters::Equals::from_args) }
| ^ doesn't have a size known at compile-time | ^^^ constructor is not visible here due to private fields
``` ```
2. and one I dont understand: * second:
``` ```
error[E0308]: mismatched types error[E0277]: the size for values of type `[modules::Available]` cannot be known at compilation time
--> src/modules.rs:10:45 --> src/modules.rs:9:18
| |
10 | Available{ name: "equals", cons: move |a| filters::Equals::from_args(a) } 9 | const available: [Available] = [
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait modules::Module, found struct `filters::Equals` | ^^^^^^^^^^^ doesn't have a size known at compile-time
``` |
= help: the trait `std::marker::Sized` is not implemented for `[modules::Available]`
```