Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PickList requires Copy trait, severely limiting its use #572

Closed
olson-dan opened this issue Oct 21, 2020 · 4 comments · Fixed by #2251
Closed

PickList requires Copy trait, severely limiting its use #572

olson-dan opened this issue Oct 21, 2020 · 4 comments · Fixed by #2251
Labels
feature New feature or request widget
Milestone

Comments

@olson-dan
Copy link

I have something like this:

#[derive(Debug, Clone, PartialEq, Eq)]
struct Item {
  id: u32,
  description: String,
}

impl std::fmt::Display for Item {
 // ...
}

struct State {
  items: Vec<Item>,
  selected_item: Option<Item>,
  dropdown: pick_list::State<Item>
}

// ...

fn view(&mut self) -> Element<Message> {
  // ...
  let dropdown = PickList::new(&mut self.dropdown,
    &self.items,
    self.selected_item,
    Message::ItemSelected);
  // ...
}

This does not work because the third parameter to PickList::new() takes an Option<T> and requires Copy to be implemented. This prevents any type that implements String from being used with PickList, which is highly limiting.

@twitchyliquid64
Copy link

twitchyliquid64 commented Oct 22, 2020

Have a look at #467 - basically just have the T in PickList be an index (ie: fixed-size newtype) into state at a higher scope, and implement impl std::fmt::Display to resolve that string.

@olson-dan
Copy link
Author

Have a look at #467 - basically just have the T in PickList be an index (ie: fixed-size newtype) into state at a higher scope, and implement impl std::fmt::Display to resolve that string.

Thanks for the suggestion. Pardon me if I'm not following along, but how would one pass state into a Display implementation without storing it in the struct or using a global?

@hecrj
Copy link
Member

hecrj commented Oct 23, 2020

We may be able to accept a reference as the third argument. I'll look into it!

For the time being, you should be able to circumvent the issue with an explicit clone: self.selected_item.clone().

@hecrj hecrj added this to the 0.2.0 milestone Oct 23, 2020
@hecrj hecrj added improvement An internal improvement feature New feature or request and removed improvement An internal improvement labels Oct 23, 2020
@olson-dan
Copy link
Author

For the time being, you should be able to circumvent the issue with an explicit clone: self.selected_item.clone().

Thanks, it was silly of me to not try that!

@hecrj hecrj modified the milestones: 0.2.0, 0.3.0 Nov 26, 2020
@hecrj hecrj modified the milestones: 0.3.0, 0.4.0 Mar 31, 2021
@hecrj hecrj added the widget label Jan 19, 2022
@hecrj hecrj modified the milestones: 0.4.0, 0.4.1, 0.5.0 May 2, 2022
@hecrj hecrj modified the milestones: 0.5.0, 0.6.0 Nov 9, 2022
@hecrj hecrj modified the milestones: 0.6.0, 0.7.0 Dec 7, 2022
@hecrj hecrj modified the milestones: 0.7.0, 0.8.0 Jan 14, 2023
@hecrj hecrj modified the milestones: 0.8.0, 0.9.0 Feb 18, 2023
@hecrj hecrj modified the milestones: 0.9.0, 0.10.0 Apr 13, 2023
@hecrj hecrj modified the milestones: 0.10.0, 0.11.0 Jul 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request widget
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants