Skip to content

Commit 6e287ff

Browse files
committed
fix bugs and docs
1 parent 5e0186f commit 6e287ff

File tree

8 files changed

+33
-29
lines changed

8 files changed

+33
-29
lines changed

egui/src/any/any_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::any::element::{AnyMapElement, AnyMapTrait};
22
use std::any::TypeId;
33
use std::collections::HashMap;
44

5-
/// Stores object with any type.
5+
/// Stores object of any type.
66
#[derive(Clone, Debug, Default)]
77
pub struct AnyMap(HashMap<TypeId, AnyMapElement>);
88

egui/src/any/id_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl AnyMapId {
7171

7272
pub fn reset<T: AnyMapTrait>(&mut self) {
7373
let id = TypeId::of::<T>();
74-
self.0.retain(|_, v| v.type_id() == id);
74+
self.0.retain(|_, v| v.type_id() != id);
7575
}
7676

7777
pub fn reset_all(&mut self) {

egui/src/any/mod.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
1-
//! Any type storages for [`crate::Memory`].
1+
//! Any-type storages for [`Memory`].
22
//!
3-
//! This module contains structs to store arbitrary type using [`std::any::Any`] trait. Also, they can be cloned, and structs in [`serializable`] can be de/serialized.
3+
//! This module contains structs to store arbitrary types using [`Any`] trait. Also, they can be cloned, and structs in [`serializable`] can be de/serialized.
44
//!
5-
//! Basically, all this is just `HashMap<TypeId, Box<dyn Any + static>>` and `HashMap<Id, Box<dyn Any + static>>`, but with helper functions and hacks for cloning and de/serialization.
5+
//! All this is just `HashMap<TypeId, Box<dyn Any + static>>` and `HashMap<Id, Box<dyn Any + static>>`, but with helper functions and hacks for cloning and de/serialization.
66
//!
77
//! # Trait requirements
88
//!
9-
//! If you want to store your type here, it must implement `Clone` and `Any` and be `'static`, which means it must not contain references. If you want to store your data in serializable storage, it must implement `serde::Serialize` and `serde::Deserialize` under `persistent` feature.
9+
//! If you want to store your type here, it must implement `Clone` and `Any` and be `'static`, which means it must not contain references. If you want to store your data in serializable storage, it must implement `serde::Serialize` and `serde::Deserialize` under the `persistent` feature.
1010
//!
1111
//! # `AnyMap`
1212
//!
13-
//! It stores everything by just type. You should use this map for your widget, when all instances of your widgets can have only one state. E.g. for popup windows, for color picker.
13+
//! It stores everything by just type. You should use this map for your widget when all instances of your widgets can have only one state. E.g. for popup windows, for color picker.
1414
//!
15-
//! In order to not have intersections, you should create newtype for anything you try to store here, like:
15+
//! To not have intersections, you should create newtype for anything you try to store here, like:
1616
//! ```rust
1717
//! struct MyEditBool(pub bool);
1818
//! ```
1919
//!
2020
//! # `AnyMapId`
2121
//!
22-
//! [`AnyMap`] and [`AnyMapId`] has quite similar interface, except for [`AnyMapId`] you should pass [`crate::Id`] to get and insert things.
22+
//! [`AnyMap`] and [`AnyMapId`] has a quite similar interface, except for [`AnyMapId`] you should pass [`Id`] to get and insert things.
2323
//!
24-
//! It stores everything by [`crate::Id`], this should be used when your widget can have different data for different instances of the widget.
24+
//! It stores everything by [`Id`], this should be used when your widget can have different data for different instances of the widget.
2525
//!
2626
//! # `serializable`
2727
//!
2828
//! [`AnyMap`] and [`serializable::AnyMap`] has exactly the same interface, but [`serializable::AnyMap`] only requires serde traits for stored object under `persistent` feature. Same thing for [`AnyMapId`] and [`serializable::AnyMapId`].
2929
//!
30-
//! # What could broke
30+
//! # What could break
3131
//!
32-
//! Things here could broke only when you trying to load this from file.
32+
//! Things here could break only when you trying to load this from file.
3333
//!
34-
//! First, serialized `TypeId` in [`serializable::AnyMap`] could broke, if you updated version of Rust compiler between runs.
34+
//! First, serialized `TypeId` in [`serializable::AnyMap`] could broke if you updated the version of the Rust compiler between runs.
3535
//!
36-
//! Second, count and reset all instances of type in [`serializable::AnyMapId`] could return incorrect value for the same reason.
36+
//! Second, count and reset all instances of a type in [`serializable::AnyMapId`] could return an incorrect value for the same reason.
3737
//!
38-
//! Deserialization errors of loaded elements of these storages can be determined only when you call `get_...` functions, they not logged and not provided to user, on this errors value is just replaced with `or_insert()`/default value.
38+
//! Deserialization errors of loaded elements of these storages can be determined only when you call `get_...` functions, they not logged and not provided to a user, on this errors value is just replaced with `or_insert()`/default value.
3939
//!
4040
//! # When not to use this
4141
//!
42-
//! Basically, this is not for important widget data. Some errors just ignored and correct value of type is inserted when you call. This is done in order to simple interface.
42+
//! This is not for important widget data. Some errors are just ignored and the correct value of type is inserted when you call. This is done to more simple interface.
4343
//!
44-
//! You shouldn't use any map here when you need very reliable state storage with rich error-handling. For this purposes you should create your own `Memory` struct and pass it everywhere you need it. Then, you should de/serialize it by yourself, handling all serialization or other errors as you wish.
44+
//! You shouldn't use any map here when you need very reliable state storage with rich error-handling. For this purpose you should create your own `Memory` struct and pass it everywhere you need it. Then, you should de/serialize it by yourself, handling all serialization or other errors as you wish.
45+
//!
46+
//! [`Id`]: crate::Id
47+
//! [`Memory`]: crate::Memory
48+
//! [`Any`]: std::any::Any
4549
4650
mod any_map;
4751
mod element;

egui/src/any/serializable/any_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use {
1212
std::any::TypeId,
1313
};
1414

15-
/// Stores object with any type, and can be de/serialized.
15+
/// Stores object of any type and can be de/serialized.
1616
#[derive(Clone, Debug, Default)]
1717
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
1818
pub struct AnyMap(HashMap<TypeId, AnyMapElement>);

egui/src/any/serializable/id_map.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl AnyMapId {
7272
}
7373

7474
impl AnyMapId {
75-
/// You could use this function to find is there some leak or misusage. Note, that result of this function could broke between runs, if you upgraded Rust version or for other reasons.
75+
/// You could use this function to find is there some leak or misusage. Note, that result of this function could break between runs, if you upgraded the Rust version or for other reasons.
7676
pub fn count<T: AnyMapTrait>(&mut self) -> usize {
7777
let id = TypeId::of::<T>();
7878
self.0.iter().filter(|(_, v)| v.1 == id).count()
@@ -82,10 +82,10 @@ impl AnyMapId {
8282
self.0.len()
8383
}
8484

85-
/// Note that this function could not reset all needed types between runs because, if you upgraded Rust version or for other reasons.
85+
/// Note that this function could not reset all needed types between runs because if you upgraded the Rust version or for other reasons.
8686
pub fn reset<T: AnyMapTrait>(&mut self) {
8787
let id = TypeId::of::<T>();
88-
self.0.retain(|_, v| v.1 == id);
88+
self.0.retain(|_, v| v.1 != id);
8989
}
9090

9191
pub fn reset_all(&mut self) {

egui/src/any/serializable/type_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::any::Any;
22

3-
/// We need this because `TypeId` can't be deserialized or serialized directly, but this can be done using hashing. However, there is small possibility that different types will have intersection by hashes of their type ids.
3+
/// We need this because `TypeId` can't be deserialized or serialized directly, but this can be done using hashing. However, there is a small possibility that different types will have intersection by hashes of their type ids.
44
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
55
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
66
pub struct TypeId(u64);

egui/src/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ impl Context {
830830
));
831831
if ui.button("Reset").clicked() {
832832
self.memory()
833-
.data
833+
.id_data
834834
.reset::<containers::collapsing_header::State>();
835835
}
836836
});
@@ -841,7 +841,7 @@ impl Context {
841841
self.memory().id_data_temp.count::<menu::BarState>()
842842
));
843843
if ui.button("Reset").clicked() {
844-
self.memory().data_temp.reset::<menu::BarState>();
844+
self.memory().id_data_temp.reset::<menu::BarState>();
845845
}
846846
});
847847

@@ -851,7 +851,7 @@ impl Context {
851851
self.memory().id_data.count::<scroll_area::State>()
852852
));
853853
if ui.button("Reset").clicked() {
854-
self.memory().data_temp.reset::<scroll_area::State>();
854+
self.memory().id_data.reset::<scroll_area::State>();
855855
}
856856
});
857857

@@ -861,7 +861,7 @@ impl Context {
861861
self.memory().id_data.count::<resize::State>()
862862
));
863863
if ui.button("Reset").clicked() {
864-
self.memory().data_temp.reset::<resize::State>();
864+
self.memory().id_data.reset::<resize::State>();
865865
}
866866
});
867867

egui/src/memory.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ use crate::{any, area, window, Id, InputState, LayerId, Pos2, Rect, Style};
1111
///
1212
/// If you want this to persist when closing your app you should serialize `Memory` and store it.
1313
///
14-
/// If you want to store data for your widgets, you should look at `data`/`data_temp` and `id_data`/`id_data_temp` fields, and read documentation of [`any`] module.
14+
/// If you want to store data for your widgets, you should look at `data`/`data_temp` and `id_data`/`id_data_temp` fields, and read the documentation of [`any`] module.
1515
#[derive(Clone, Debug, Default)]
1616
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
1717
#[cfg_attr(feature = "persistence", serde(default))]
1818
pub struct Memory {
1919
pub options: Options,
2020

21-
/// This map stores current states for widgets that does not require `Id`. This will be saved between different program runs if you use `persistence` feature.
21+
/// This map stores current states for widgets that don't require `Id`. This will be saved between different program runs if you use the `persistence` feature.
2222
pub data: any::serializable::AnyMap,
2323

2424
/// Same as `data`, but this data will not be saved between runs.
2525
#[cfg_attr(feature = "persistence", serde(skip))]
2626
pub data_temp: any::AnyMap,
2727

28-
/// This map stores current states for all widgets with custom `Id`s. This will be saved between different program runs if you use `persistence` feature.
28+
/// This map stores current states for all widgets with custom `Id`s. This will be saved between different program runs if you use the `persistence` feature.
2929
pub id_data: any::serializable::AnyMapId,
3030

3131
/// Same as `id_data`, but this data will not be saved between runs.

0 commit comments

Comments
 (0)