1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Lifetime-bound native session capability tokens.
//!
//! A backend leaf crate (CPU, CUDA, WebGPU) exposes its concrete execution
//! session to higher-level operation crates through a [`NativeSessionRef`]
//! returned by [`Engine::native_engine`](crate::Engine::native_engine).
//! The token is opaque: its fields are private and its only constructor is
//! `unsafe`, so safe code can neither fabricate one nor retarget it at another
//! value. A backend leaf recovers its own session with a safe visitor that
//! checks the token's marker against a marker type only that leaf can name.
//!
//! This narrows the unsafe boundary to each leaf's constructor and visitor; it
//! does not remove it.
//!
//! # Examples
//!
//! A session without native services returns no token, which is the default:
//!
//! ```rust
//! use tenferro_tensor::{Engine, NativeSessionRef};
//!
//! fn has_native_services(session: &mut dyn Engine) -> bool {
//! session.native_engine().is_some()
//! }
//! ```
//!
//! The token borrows its session exclusively and cannot outlive that borrow:
//!
//! ```compile_fail
//! use tenferro_tensor::{Engine, NativeSessionRef};
//!
//! fn escape(session: &mut dyn Engine) -> NativeSessionRef<'static> {
//! session.native_engine().unwrap()
//! }
//! ```
//!
//! It cannot be built from safe code, even with a private marker type:
//!
//! ```compile_fail,E0133
//! use tenferro_tensor::NativeSessionRef;
//!
//! struct Marker;
//! let mut value = 0_u8;
//! let _token = NativeSessionRef::new::<Marker, u8>(&mut value);
//! ```
//!
//! It is neither `Send` nor `Clone`, so it cannot leave its thread or be
//! duplicated into a second exclusive borrow:
//!
//! ```compile_fail,E0277
//! fn require_send<T: Send>(_: T) {}
//! fn check(token: tenferro_tensor::NativeSessionRef<'_>) {
//! require_send(token);
//! }
//! ```
//!
//! ```compile_fail,E0599
//! fn duplicate(token: tenferro_tensor::NativeSessionRef<'_>) {
//! let _second = token.clone();
//! let _first = token;
//! }
//! ```
use TypeId;
use fmt;
use PhantomData;
use NonNull;
/// An exclusively borrowed backend-leaf execution session.
///
/// The token names one concrete session by a backend-leaf marker type and
/// borrows it for `'s`. It is not `Clone`, `Copy`, `Send` or `Sync`. Only the
/// backend leaf that owns the marker can recover the session, through its own
/// safe visitor; custom sessions either return no token or forward the token of
/// a standard delegate they own.
///
/// # Examples
///
/// ```rust
/// use tenferro_tensor::{Engine, NativeSessionRef};
///
/// struct Marker;
///
/// fn is_marked(token: &NativeSessionRef<'_>) -> bool {
/// token.has_marker::<Marker>()
/// }
///
/// fn inspect(session: &mut dyn Engine) -> bool {
/// session.native_engine().is_some_and(|token| is_marked(&token))
/// }
/// ```