script/dom/bindings/
transferable.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 [transferable objects]
6//! (<https://html.spec.whatwg.org/multipage/#transferable-objects>).
7
8use std::collections::HashMap;
9use std::hash::Hash;
10
11use base::id::NamespaceIndex;
12use script_bindings::structuredclone::MarkedAsTransferableInIdl;
13
14use crate::dom::bindings::error::Fallible;
15use crate::dom::bindings::reflector::DomObject;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::structuredclone::StructuredData;
18use crate::dom::globalscope::GlobalScope;
19
20pub(crate) trait Transferable: DomObject + MarkedAsTransferableInIdl
21where
22    Self: Sized,
23{
24    type Index: Copy + Eq + Hash;
25    type Data;
26
27    fn can_transfer(&self) -> bool {
28        true
29    }
30
31    /// <https://html.spec.whatwg.org/multipage/#transfer-steps>
32    fn transfer(&self) -> Fallible<(NamespaceIndex<Self::Index>, Self::Data)>;
33
34    /// <https://html.spec.whatwg.org/multipage/#transfer-receiving-steps>
35    fn transfer_receive(
36        owner: &GlobalScope,
37        id: NamespaceIndex<Self::Index>,
38        serialized: Self::Data,
39    ) -> Result<DomRoot<Self>, ()>;
40
41    fn serialized_storage<'a>(
42        data: StructuredData<'a, '_>,
43    ) -> &'a mut Option<HashMap<NamespaceIndex<Self::Index>, Self::Data>>;
44}
45
46pub(crate) fn assert_transferable<T: Transferable>() {}