Skip to content

Support for fns in collections? #154

@Keshyu

Description

@Keshyu

Hey! Really like this crate, especially the lack of unwraps (rest my soul (◡ ‿ ◡ .))

But while making a little toy game, I found that there's really no way to pass a collection of functions into the builder.

The following code:

use bon::Builder;
use std::collections::HashMap;

fn main() {
   let player_1 = Player::builder().skills(bon::map! {
      "slash": |field| { /* ... */ },
      "punch": |field| { /* ... */ },
   });
}

#[derive(Builder)]
struct Player {
   skills: HashMap<String, fn(Field)>,
}

struct Field(/* ... */);

Gives this error:

error[E0277]: the trait bound `for<'a> fn(Field<'a>): From<{closure@src/main.rs:7:16: 7:23}>` is not satisfied

I found that both fn pointers & dynamic dispatch break because of impl Into, so bon::map and the like don't work.
Maybe you can use as or other kind of cast but I imagine that'd be very awkward.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions