From 4e7e28c619912fb589eb14eaed27477fddb0d1fe Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 24 Oct 2021 18:44:36 +0200 Subject: [PATCH] Only implement serde-traits on Memory on "persistence" feature --- egui/src/memory.rs | 19 +++++++++++-------- egui/src/util/id_any_map.rs | 24 ++++++++++++------------ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/egui/src/memory.rs b/egui/src/memory.rs index 116ed2c12e1d..836e2239e247 100644 --- a/egui/src/memory.rs +++ b/egui/src/memory.rs @@ -10,11 +10,12 @@ use crate::{area, window, Id, IdMap, InputState, LayerId, Pos2, Rect, Style}; /// how far the user has scrolled in a `ScrollArea` etc. /// /// If you want this to persist when closing your app you should serialize `Memory` and store it. +/// For this you need to enable the `persistence`. /// /// If you want to store data for your widgets, you should look at [`Memory::data`] #[derive(Clone, Debug, Default)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -#[cfg_attr(feature = "serde", serde(default))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", serde(default))] pub struct Memory { pub options: Options, @@ -49,33 +50,35 @@ pub struct Memory { /// let cache = memory.caches.cache::>(); /// assert_eq!(cache.get("hello"), 5); /// ``` - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] pub caches: crate::util::cache::CacheStorage, // ------------------------------------------ /// new scale that will be applied at the start of the next frame + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) new_pixels_per_point: Option, /// new fonts that will be applied at the start of the next frame + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) new_font_definitions: Option, - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) interaction: Interaction, - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) window_interaction: Option, - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) drag_value: crate::widgets::drag_value::MonoState, pub(crate) areas: Areas, /// Which popup-window is open (if any)? /// Could be a combo box, color picker, menu etc. - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] popup: Option, - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] everything_is_visible: bool, } diff --git a/egui/src/util/id_any_map.rs b/egui/src/util/id_any_map.rs index 2dc3f7fbf628..5fd588460022 100644 --- a/egui/src/util/id_any_map.rs +++ b/egui/src/util/id_any_map.rs @@ -9,7 +9,7 @@ use std::any::Any; /// 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. #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] pub struct TypeId(u64); impl TypeId { @@ -33,28 +33,28 @@ impl From for TypeId { // ----------------------------------------------------------------------------------------------- -#[cfg(feature = "serde")] +#[cfg(feature = "persistence")] pub trait SerializableAny: 'static + Any + Clone + serde::Serialize + for<'a> serde::Deserialize<'a> + Send + Sync { } -#[cfg(feature = "serde")] +#[cfg(feature = "persistence")] impl SerializableAny for T where T: 'static + Any + Clone + serde::Serialize + for<'a> serde::Deserialize<'a> + Send + Sync { } -#[cfg(not(feature = "serde"))] +#[cfg(not(feature = "persistence"))] pub trait SerializableAny: 'static + Any + Clone + for<'a> Send + Sync {} -#[cfg(not(feature = "serde"))] +#[cfg(not(feature = "persistence"))] impl SerializableAny for T where T: 'static + Any + Clone + for<'a> Send + Sync {} // ----------------------------------------------------------------------------------------------- #[cfg(feature = "persistence")] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] struct SerializedElement { type_id: TypeId, ron: String, @@ -435,11 +435,11 @@ fn hash(type_id: TypeId, id: Id) -> u64 { // ---------------------------------------------------------------------------- -#[cfg(feature = "serde")] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg(feature = "persistence")] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] pub struct PersistedMap(Vec<(u64, SerializedElement)>); -#[cfg(feature = "serde")] +#[cfg(feature = "persistence")] impl PersistedMap { fn from_map(map: &IdAnyMap) -> Self { Self( @@ -461,7 +461,7 @@ impl PersistedMap { } } -#[cfg(feature = "serde")] +#[cfg(feature = "persistence")] impl serde::Serialize for IdAnyMap { fn serialize(&self, serializer: S) -> Result where @@ -471,7 +471,7 @@ impl serde::Serialize for IdAnyMap { } } -#[cfg(feature = "serde")] +#[cfg(feature = "persistence")] impl<'de> serde::Deserialize<'de> for IdAnyMap { fn deserialize(deserializer: D) -> Result where @@ -485,7 +485,7 @@ impl<'de> serde::Deserialize<'de> for IdAnyMap { #[test] fn test_mix() { - #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] + #[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] #[derive(Clone, Debug, PartialEq)] struct Foo(i32);