Skip to main content

NativeSessionRef

Struct NativeSessionRef 

Source
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>

Source

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);
Source

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>());
Source

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());

Trait Implementations§

Source§

impl Debug for NativeSessionRef<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'s> !Send for NativeSessionRef<'s>

§

impl<'s> !Sync for NativeSessionRef<'s>

§

impl<'s> !UnwindSafe for NativeSessionRef<'s>

§

impl<'s> Freeze for NativeSessionRef<'s>

§

impl<'s> RefUnwindSafe for NativeSessionRef<'s>

§

impl<'s> Unpin for NativeSessionRef<'s>

§

impl<'s> UnsafeUnpin for NativeSessionRef<'s>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.