From b2adb834d4a20924e6a2cc959d1052139a3d31f9 Mon Sep 17 00:00:00 2001 From: Y Date: Sun, 17 Nov 2019 16:16:25 +0100 Subject: [PATCH] Box and array --- README.md | 79 ++++++++++++++++++++----------------------------------- 1 file changed, 28 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 82d40d8..1bb63ba 100644 --- a/README.md +++ b/README.md @@ -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. - -
- ## 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 { … } } ``` @@ -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 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 don’t 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]` + ```