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
In module `modules`, I have:
@ -36,15 +11,15 @@ pub trait Module {
In module `filters`, I have:
```rust
pub struct Equals<'a> { … }
pub struct Equals { … }
impl Equals<'_> {
impl Equals {
pub fn from_args(mut args: ModuleArgs) -> Equals {
Equals { … }
}
}
impl Module for Equals<'_> {
impl Module for Equals {
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
struct Available {
name: &'static str,
cons: fn(ModuleArgs) -> dyn Module
name: String,
cons: Box<fn(ModuleArgs) -> dyn Module>
}
const available: (Available) = (
Available{ name: "equals", cons: move |a| filters::Equals::from_args(a) }
);
const available: [Available] = [
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
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
--> src/modules.rs:10:45
|
10 | Available{ name: "equals", cons: move |a| filters::Equals::from_args(a) }
| ^ doesn't have a size known at compile-time
```
```
error[E0423]: expected value, found struct `Box`
--> src/modules.rs:10:37
|
10 | Available { name: "equals", cons: Box.new(filters::Equals::from_args) }
| ^^^ constructor is not visible here due to private fields
```
2. and one I dont understand:
* second:
```
error[E0308]: mismatched types
--> src/modules.rs:10:45
|
10 | Available{ name: "equals", cons: move |a| filters::Equals::from_args(a) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait modules::Module, found struct `filters::Equals`
```
```
error[E0277]: the size for values of type `[modules::Available]` cannot be known at compilation time
--> src/modules.rs:9:18
|
9 | const available: [Available] = [
| ^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `[modules::Available]`
```