gdnative_core/object/
ownership.rs

1//! Typestates to express ownership and thread safety of Godot types.
2
3/// Marker that indicates that a value currently only has a
4/// single unique reference.
5///
6/// Using this marker causes the type to be `!Sync`.
7#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
8pub struct Unique(std::marker::PhantomData<*const ()>);
9unsafe impl Send for Unique {}
10
11/// Marker that indicates that a value currently might be shared in the same or
12/// over multiple threads.
13#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
14pub struct Shared;
15
16/// Marker that indicates that a value can currently only be shared in the same thread.
17///
18/// Using this marker causes the type to be `!Send + !Sync`.
19#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
20pub struct ThreadLocal(std::marker::PhantomData<*const ()>);
21
22/// Trait to parametrize over the ownership markers [`Unique`], [`Shared`] and [`ThreadLocal`].
23///
24/// This trait is sealed and has no public members.
25///
26/// It specifies the ownership policy of godot-rust smart pointers such as [`Ref`][super::Ref].
27/// Ownership specifies how references _own_ an object, i.e. how they point to it and who is responsible
28/// for its destruction (in case of [`RefCounted`][super::memory::RefCounted]). Furthermore, it defines
29/// from where the object can be accessed, and if sharing the object across threads is possible.
30pub trait Ownership: private::Sealed {}
31
32/// Trait to parametrize over the ownership markers that are local to the current thread:
33/// [`Unique`] and [`ThreadLocal`].
34pub trait LocalThreadOwnership: Ownership {}
35
36/// Trait to parametrize over the ownership markers that are not unique:
37/// [`Shared`] and [`ThreadLocal`].
38pub trait NonUniqueOwnership: Ownership {}
39
40impl Ownership for Unique {}
41impl LocalThreadOwnership for Unique {}
42impl private::Sealed for Unique {}
43
44impl Ownership for Shared {}
45impl NonUniqueOwnership for Shared {}
46impl private::Sealed for Shared {}
47
48impl Ownership for ThreadLocal {}
49impl LocalThreadOwnership for ThreadLocal {}
50impl NonUniqueOwnership for ThreadLocal {}
51impl private::Sealed for ThreadLocal {}
52
53mod private {
54    pub trait Sealed {}
55}