Skip to main content

script/dom/bindings/
serializable.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5//! Trait representing the concept of [serializable objects]
6//! (<https://html.spec.whatwg.org/multipage/#serializable-objects>).
7
8use script_bindings::serializable::Serializable;
9use servo_base::id::{Index, NamespaceIndex, PipelineNamespaceId};
10
11/// The key corresponding to the storage location
12/// of a serialized platform object stored in a StructuredDataHolder.
13#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
14pub(crate) struct StorageKey {
15    pub(crate) index: u32,
16    pub(crate) name_space: u32,
17}
18
19impl StorageKey {
20    pub(crate) fn new<T>(index: NamespaceIndex<T>) -> StorageKey {
21        let name_space = index.namespace_id.0.to_ne_bytes();
22        let index = index.index.0.get().to_ne_bytes();
23        StorageKey {
24            index: u32::from_ne_bytes(index),
25            name_space: u32::from_ne_bytes(name_space),
26        }
27    }
28}
29
30impl<T> From<StorageKey> for NamespaceIndex<T> {
31    fn from(key: StorageKey) -> NamespaceIndex<T> {
32        NamespaceIndex {
33            namespace_id: PipelineNamespaceId(key.name_space),
34            index: Index::new(key.index).expect("Index must not be zero"),
35        }
36    }
37}
38
39pub(crate) fn assert_serializable<T: Serializable<crate::DomTypeHolder>>() {}