Skip to content

The ui.data() requires mutable borrow to get_temp()? #2741

@IS2511

Description

@IS2511

Describe the bug

It seems that inside the ui.data() lambda I can't get_temp() anything from the IdTypeMap because the reference to it passed to the lambda apparently needs to be mutable. Why?

To Reproduce
Steps to reproduce the behavior:

  1. Create an eframe project and plop down some widget
  2. Inside the lambda that accepts &mut egui::Ui do
let x_id = ui.id().with("x");
let x = ui.data(|d| d.get_temp::<u32>(x_id).unwrap_or_default());
error[E0596]: cannot borrow `*d` as mutable, as it is behind a `&` reference
  --> [DATA EXPUNGED].rs:41:33
   |
41 |             let x = ui.data(|d| d.get_temp::<u32>(x_id).unwrap_or_default());
   |                              -  ^^^^^^^^^^^^^^^^^^^^^^^ `d` is a `&` reference, so the data it refers to cannot be borrowed as mutable
   |                              |
   |                              consider changing this binding's type to be: `&mut IdTypeMap`

Expected behavior

Since get_temp() clones the value, the IdTypeMap is not required to be mutable. Applies to all "read" methods.

Screenshots

Desktop (please complete the following information):

  • OS: Linux 5.18.0-14parrot1-amd64 #1 SMP PREEMPT_DYNAMIC Debian 5.18.14-1parrot1 (2022-08-07) x86_64 GNU/Linux
  • Browser
  • Version master (7215fdf)

Additional context

Am I missing something? This seems very weird.

Possibly related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething is broken

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions