diff options
author | Josh Matthews <josh@joshmatthews.net> | 2025-03-15 09:58:56 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-15 13:58:56 +0000 |
commit | d8fc1d8bb88d291233022f52550628e6fc0376a4 (patch) | |
tree | 3c99cae670682dbf5d7cf8cbffa151c8c55aef65 /components/script/dom/bindings/serializable.rs | |
parent | 21ecfdd088faa68ba470b15fff6eee61c05e6bba (diff) | |
download | servo-d8fc1d8bb88d291233022f52550628e6fc0376a4.tar.gz servo-d8fc1d8bb88d291233022f52550628e6fc0376a4.zip |
Refactor common boilerplate out of serialize/transfer implementations (#35831)
* script: Create infrastructure for reducing hand-written serializable/transferrable implementations.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Clone all serializable DOM interfaces.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Deserialize all serializable DOM interfaces.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Serialize all serializable DOM interfaces.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Transfer-receive all transferable DOM interfaces.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Transfer all transferable DOM interfaces.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* Formatting.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Extract boilerplate from serialize/deserialize implementations.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Extract boilerplate from transfer-receive.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Extract boilerplate from transfer operation.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* Tidy fixes.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* script: Check transferability of all DOM interfaces.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* Clippy fixes.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* Clippy fixes.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* Remove unnecessary duplicate crate.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
* Formatting.
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
---------
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Diffstat (limited to 'components/script/dom/bindings/serializable.rs')
-rw-r--r-- | components/script/dom/bindings/serializable.rs | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/components/script/dom/bindings/serializable.rs b/components/script/dom/bindings/serializable.rs index edc3b07652c..b791b0ebc1a 100644 --- a/components/script/dom/bindings/serializable.rs +++ b/components/script/dom/bindings/serializable.rs @@ -5,8 +5,14 @@ //! Trait representing the concept of [serializable objects] //! (<https://html.spec.whatwg.org/multipage/#serializable-objects>). +use std::collections::HashMap; +use std::num::NonZeroU32; + +use base::id::PipelineNamespaceId; + use crate::dom::bindings::reflector::DomObject; -use crate::dom::bindings::structuredclone::{StructuredDataReader, StructuredDataWriter}; +use crate::dom::bindings::root::DomRoot; +use crate::dom::bindings::structuredclone::{StructuredData, StructuredDataReader}; use crate::dom::globalscope::GlobalScope; use crate::script_runtime::CanGc; @@ -18,16 +24,51 @@ pub(crate) struct StorageKey { pub(crate) name_space: u32, } +impl StorageKey { + pub(crate) fn new(name_space: PipelineNamespaceId, index: NonZeroU32) -> StorageKey { + let name_space = name_space.0.to_ne_bytes(); + let index = index.get().to_ne_bytes(); + StorageKey { + index: u32::from_ne_bytes(index), + name_space: u32::from_ne_bytes(name_space), + } + } +} + +pub(crate) trait IntoStorageKey +where + Self: Sized, +{ + fn into_storage_key(self) -> StorageKey; +} + /// Interface for serializable platform objects. /// <https://html.spec.whatwg.org/multipage/#serializable> -pub(crate) trait Serializable: DomObject { +pub(crate) trait Serializable: DomObject +where + Self: Sized, +{ + type Id: Copy + Eq + std::hash::Hash + IntoStorageKey + From<StorageKey>; + type Data; + /// <https://html.spec.whatwg.org/multipage/#serialization-steps> - fn serialize(&self, sc_writer: &mut StructuredDataWriter) -> Result<StorageKey, ()>; + fn serialize(&self) -> Result<(Self::Id, Self::Data), ()>; /// <https://html.spec.whatwg.org/multipage/#deserialization-steps> fn deserialize( owner: &GlobalScope, - sc_reader: &mut StructuredDataReader, - extra_data: StorageKey, + serialized: Self::Data, can_gc: CanGc, - ) -> Result<(), ()>; + ) -> Result<DomRoot<Self>, ()> + where + Self: Sized; + + /// Returns the field of [StructuredDataReader]/[StructuredDataWriter] that + /// should be used to read/store serialized instances of this type. + fn serialized_storage(data: StructuredData<'_>) -> &mut Option<HashMap<Self::Id, Self::Data>>; + + /// Returns the field of [StructuredDataReader] that should be used to store + /// deserialized instances of this type. + fn deserialized_storage( + reader: &mut StructuredDataReader, + ) -> &mut Option<HashMap<StorageKey, DomRoot<Self>>>; } |