Skip to content

Commit

Permalink
Only implement serde-traits on Memory on "persistence" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Oct 24, 2021
1 parent 58f961f commit 4e7e28c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
19 changes: 11 additions & 8 deletions egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -49,33 +50,35 @@ pub struct Memory {
/// let cache = memory.caches.cache::<CharCountCache<'_>>();
/// 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<f32>,

/// 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<epaint::text::FontDefinitions>,

#[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<window::WindowInteraction>,

#[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<Id>,

#[cfg_attr(feature = "serde", serde(skip))]
#[cfg_attr(feature = "persistence", serde(skip))]
everything_is_visible: bool,
}

Expand Down
24 changes: 12 additions & 12 deletions egui/src/util/id_any_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -33,28 +33,28 @@ impl From<std::any::TypeId> 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<T> 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<T> 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,
Expand Down Expand Up @@ -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(
Expand All @@ -461,7 +461,7 @@ impl PersistedMap {
}
}

#[cfg(feature = "serde")]
#[cfg(feature = "persistence")]
impl serde::Serialize for IdAnyMap {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -471,7 +471,7 @@ impl serde::Serialize for IdAnyMap {
}
}

#[cfg(feature = "serde")]
#[cfg(feature = "persistence")]
impl<'de> serde::Deserialize<'de> for IdAnyMap {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand All @@ -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);

Expand Down

0 comments on commit 4e7e28c

Please sign in to comment.