From 9029b18fc06533b6ee100c35ba00f9e676f66417 Mon Sep 17 00:00:00 2001 From: Y Date: Sun, 17 Nov 2019 16:50:48 +0100 Subject: [PATCH] =?UTF-8?q?Box::new=20+=20&[=E2=80=A6]=20notation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 1bb63ba..8f9cd29 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,20 @@ ## setup +In module `common`, I have: + +```rust +pub enum Value { + Bool(bool), + Str(String), + Int(isize), + Date(DateTime) +} + +pub type Record<'a> = HashMap<&'a str, Value>; +``` + +(in a future version, `Value` will probably become recursive, with a variant `Vec` for example) + In module `modules`, I have: ```rust @@ -34,35 +49,43 @@ struct Available { cons: Box dyn Module> } -const available: [Available] = [ - Available { name: "equals", cons: Box.new(filters::Equals::from_args) } +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 shouldn’t be the need for a reference. + ## problem -The above code gives me these errors (which I suspect are hiding the next error about `"equals"` probably being `&str` instead of `String`…): +The above code gives me these errors: * first: ``` - 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 + 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[E0277]: the size for values of type `[modules::Available]` cannot be known at compilation time - --> src/modules.rs:9:18 + error[E0308]: mismatched types + --> src/modules.rs:10:69 | - 9 | const available: [Available] = [ - | ^^^^^^^^^^^ doesn't have a size known at compile-time + 10 | Available { name: String::from("equals"), cons: Box::new(move |a| filters::Equals::from_args(a)) } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait modules::Module, found struct `filters::Equals` | - = help: the trait `std::marker::Sized` is not implemented for `[modules::Available]` + = note: expected type `dyn modules::Module` + found type `filters::Equals` ```