[Question]: E0228 - The lifetime bound for this object type cannot be deduced from context; please supply an explicit bound

Hello.
I am trying to store a Rust function inside my Built-In Function type. However, it complains that the lifetime bound for the object type cannot be deduced from context. (Here: &dyn Fn(HashMap<String, String>, context::Context) -> Type)

pub enum Value {
    NullType,
    IntType(i128),
    FloatType(f64),
    StrType(String),
    BoolType(bool),
    ArrayType(Vec<Type>, String),
    FuncType(HashMap<i32, (tokens::Token, String)>, String, Vec<nodes::Node>),
    BuiltInFunctionType(String, HashMap<i32, (String, String)>, &dyn Fn(HashMap<String, String>, context::Context) -> Type)
}

What can I do to fix this? If I can't please let me know of a good workaround.

Use a box instead.

Box<dyn Fn(HashMap<String, String>, context::Context) -> Type>

It still complains with the same error message. The &dyn to the -> Type. Not the Box.

Please post the error message in question.

error[E0106]: missing lifetime specifier
  --> src/version/types.rs:37:69
   |
37 |     BuiltInFunctionType(String, HashMap<i32, (String, String)>, Box<&dyn Fn(HashMap<String, String>, context::Context) -> Type>)
   |                                                                     ^ expected named lifetime parameter
   |
help: consider introducing a named lifetime parameter
   |
21 | pub enum Value<'a> {
22 |     NullType,
23 |     IntType(i128),
24 |     FloatType(f64),
25 |     StrType(String),
26 |     BoolType(bool),
 ...

error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound
  --> src/version/types.rs:37:70
   |
37 |     BuiltInFunctionType(String, HashMap<i32, (String, String)>, Box<&dyn Fn(HashMap<String, String>, context::Context) -> Type>)
   |                                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

There is no & in my suggested change.

ah. More problems:

error[E0277]: the trait bound `dyn std::ops::Fn(std::collections::HashMap<std::string::String, std::string::String>, version::context::Context) -> version::types::Type: std::clone::Clone` is not satisfied
  --> src/version/types.rs:37:65
   |
37 |     BuiltInFunctionType(String, HashMap<i32, (String, String)>, Box<dyn Fn(HashMap<String, String>, context::Context) -> Type>)
   |                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `dyn std::ops::Fn(std::collections::HashMap<std::string::String, std::string::String>, version::context::Context) -> version::types::Type`
   |
   = note: required because of the requirements on the impl of `std::clone::Clone` for `std::boxed::Box<dyn std::ops::Fn(std::collections::HashMap<std::string::String, std::string::String>, version::context::Context) -> version::types::Type>`
   = note: required by `std::clone::Clone::clone`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

If you need to be able to clone it, you can use an Arc instead of a Box.

I found this https://doc.rust-lang.org/std/sync/struct.Arc.html.
I can't seem to figure out how to use it as a type.

Arc<dyn Fn(HashMap<String, String>, context::Context) -> Type>

Was my first guess.

That's correct. You will need an import.

use std::sync::Arc;

Seems to have worked. But I have more important things to do right now so I'll let you know if that worked when I have time. Thank you.

1 Like