pub struct NativeSessionRef<'s> { /* private fields */ }Expand description
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
use tenferro_tensor::{BackendSession, NativeSessionRef};
struct Marker;
fn is_marked(token: &NativeSessionRef<'_>) -> bool {
token.has_marker::<Marker>()
}
fn inspect(session: &mut dyn BackendSession) -> bool {
session.native_session().is_some_and(|token| is_marked(&token))
}Implementations§
Source§impl<'s> NativeSessionRef<'s>
impl<'s> NativeSessionRef<'s>
Sourcepub unsafe fn new<M: 'static, S>(session: &'s mut S) -> Self
pub unsafe fn new<M: 'static, S>(session: &'s mut S) -> Self
Create a token that names session by the backend-leaf marker M.
§Safety
M must be a marker type private to the calling backend leaf crate,
and every token that crate creates with M must point to a value of the
single concrete session type its visitor recovers for M. session
is exclusively borrowed for 's, which the signature enforces; the
visitor relies on the marker/type correspondence for its cast.
§Examples
use tenferro_tensor::NativeSessionRef;
struct LeafSession(u32);
struct LeafMarker;
let mut session = LeafSession(7);
// SAFETY: `LeafMarker` is private to this example and names `LeafSession`.
let token = unsafe { NativeSessionRef::new::<LeafMarker, _>(&mut session) };
let pointer = token.into_marked_ptr::<LeafMarker>().expect("same marker");
// SAFETY: the marker proved the pointee is `LeafSession`, and the
// exclusive borrow of `session` is still held here.
assert_eq!(unsafe { pointer.cast::<LeafSession>().as_ref().0 }, 7);Sourcepub fn has_marker<M: 'static>(&self) -> bool
pub fn has_marker<M: 'static>(&self) -> bool
Whether this token was created with the backend-leaf marker M.
§Examples
use tenferro_tensor::NativeSessionRef;
struct Marker;
struct Other;
let mut value = 0_u8;
// SAFETY: `Marker` is private to this example and names `u8`.
let token = unsafe { NativeSessionRef::new::<Marker, _>(&mut value) };
assert!(token.has_marker::<Marker>());
assert!(!token.has_marker::<Other>());Sourcepub fn into_marked_ptr<M: 'static>(self) -> Option<NonNull<()>>
pub fn into_marked_ptr<M: 'static>(self) -> Option<NonNull<()>>
Consume the token and return its session pointer when it carries M.
Returning the pointer is safe; dereferencing it is the backend leaf’s
audited step. The leaf may only do so while the borrow the token was
created from is still held, which its visitor guarantees by holding the
&mut session for the whole visit.
§Examples
use tenferro_tensor::NativeSessionRef;
struct Marker;
struct Other;
let mut value = 3_u8;
// SAFETY: `Marker` is private to this example and names `u8`.
let token = unsafe { NativeSessionRef::new::<Marker, _>(&mut value) };
assert!(token.into_marked_ptr::<Other>().is_none());