#how to fix "the type parameter `FunctionArgs` is not constrained by the impl trait, self"

18 messages · Page 1 of 1 (latest)

pure dove
#

this is hte part of code:

impl<Func, FunctionArgs> From<Func> for Function
where
    Func: Fn(FunctionArgs) + 'static,
    FunctionArgs: JsonSchema,
{
    fn from(function: Func) -> Self {
        let schema = schema_for!(FunctionArgs);
        let fn_type_name = type_name_of_val(&function);
        let parameters = serde_json::to_value(schema)
            .unwrap_or_else(|_| panic!("Failed to serialize schema for function {}", fn_type_name));

        let fn_name = fn_type_name.split("::").last().unwrap_or("");
        Self {
            name: fn_name.to_string(),
            description: match parameters.get("description") {
                Some(Value::String(s)) => Some(s.clone()),
                _ => None,
            },
            parameters,
        }
    }
}

i cant understand what this compiler message means, ive tried just having a from function without implementing the From trait and that worked but doing it in a trait doesnt seem to work

wild pier
#

-errors

low oliveBOT
#

If you're getting large or confusing errors please post the full error message from cargo check in a code block instead of the errors in your IDE so that we can understand your problem better:

```rust
// error from cargo check here
```

pure dove
#

error[E0207]: the type parameter FunctionArgs is not constrained by the impl trait, self type, or predicates
--> src/lib.rs:48:12
|
48 | impl<Func, FunctionArgs> From<Func> for Function
| ^^^^^^^^^^^^ unconstrained type parameter

For more information about this error, try rustc --explain E0207.
error: could not compile ai-utils (lib) due to previous error
robertas@DESKTOP-N7S0U3U:/mnt/c/Users/_robertas/Documents/Projects/

#

@wild pier sorry forgot to post the full error at the end 🙂

tulip vault
#

it's possible for a single function type to accept different argument types

#

so there is no single type for FunctionArgs to be

#

for example, fn foo(&str) {} can accept both &'a str, and &'b str
and in future Rust versions it might be possible to outright have two impl Fn(...) for MyFunc implementations

pure dove
#

im sorry i dont think i fully understood what you meant

tulip vault
#

your impl is assuming that for a given function there will be exactly one, unique, parameter type list for it

#

that is not true

pure dove
#

oh, is there a way to tell it it can be any type that implements not just one?

tulip vault
#

no because the caller of the function has to pick one

#

and you can't do that with the From trait

#

you can write your own function that is not a From impl, instead

pure dove
#

yeah it seems like that worked last time, well thanks for telling me this, didnt know from impls have to take one type and cant take in generics, which when saying it out loud sounds like it was obvious

#

hope you have a great day!

tulip vault
#

I'd put it like this: for any two types T and U, there can be only one From<T> for U (or none)