rune/runtime/borrow_ref.rs
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
use core::fmt;
use core::marker::PhantomData;
use core::ops::Deref;
use core::ptr::NonNull;
use super::RawAccessGuard;
/// Guard for a data borrowed from a slot in the virtual machine.
///
/// These guards are necessary, since we need to guarantee certain forms of
/// access depending on what we do. Releasing the guard releases the access.
pub struct BorrowRef<'a, T: ?Sized + 'a> {
data: NonNull<T>,
guard: Option<RawAccessGuard>,
_marker: PhantomData<&'a T>,
}
impl<'a, T: ?Sized> BorrowRef<'a, T> {
/// Construct a borrow ref from static data.
pub(crate) fn from_static(data: &'static T) -> Self {
Self {
data: NonNull::from(data),
guard: None,
_marker: PhantomData,
}
}
/// Construct a new shared guard.
///
/// # Safety
///
/// since this has implications for releasing access, the caller must
/// ensure that access has been acquired correctly using e.g.
/// [Access::shared]. Otherwise access can be release incorrectly once
/// this guard is dropped.
pub(crate) unsafe fn new(data: NonNull<T>, guard: RawAccessGuard) -> Self {
Self {
data,
guard: Some(guard),
_marker: PhantomData,
}
}
/// Map the reference.
///
/// # Examples
///
/// ```
/// use rune::runtime::{BorrowRef, Bytes};
/// use rune::alloc::try_vec;
///
/// let bytes = rune::to_value(Bytes::from_vec(try_vec![1, 2, 3, 4]))?;
/// let bytes = bytes.borrow_ref::<Bytes>()?;
///
/// let bytes: BorrowRef<[u8]> = BorrowRef::map(bytes, |bytes| &bytes[0..2]);
///
/// assert_eq!(&bytes[..], &[1u8, 2u8][..]);
/// # Ok::<_, rune::support::Error>(())
/// ```
pub fn map<U: ?Sized>(this: Self, m: impl FnOnce(&T) -> &U) -> BorrowRef<'a, U> {
unsafe {
BorrowRef {
data: NonNull::from(m(this.data.as_ref())),
guard: this.guard,
_marker: PhantomData,
}
}
}
/// Try to map the reference to a projection.
///
/// # Examples
///
/// ```
/// use rune::runtime::{BorrowRef, Bytes};
/// use rune::alloc::try_vec;
///
/// let bytes = rune::to_value(Bytes::from_vec(try_vec![1, 2, 3, 4]))?;
/// let bytes = bytes.borrow_ref::<Bytes>()?;
///
/// let Ok(bytes) = BorrowRef::try_map(bytes, |bytes| bytes.get(0..2)) else {
/// panic!("Conversion failed");
/// };
///
/// assert_eq!(&bytes[..], &[1u8, 2u8][..]);
/// # Ok::<_, rune::support::Error>(())
/// ```
pub fn try_map<U: ?Sized>(
this: Self,
m: impl FnOnce(&T) -> Option<&U>,
) -> Result<BorrowRef<'a, U>, Self> {
unsafe {
let Some(data) = m(this.data.as_ref()) else {
return Err(BorrowRef {
data: this.data,
guard: this.guard,
_marker: PhantomData,
});
};
Ok(BorrowRef {
data: NonNull::from(data),
guard: this.guard,
_marker: PhantomData,
})
}
}
}
impl<T: ?Sized> Deref for BorrowRef<'_, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { self.data.as_ref() }
}
}
impl<T: ?Sized> AsRef<T> for BorrowRef<'_, T> {
#[inline]
fn as_ref(&self) -> &T {
unsafe { self.data.as_ref() }
}
}
impl<T: ?Sized> fmt::Debug for BorrowRef<'_, T>
where
T: fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, fmt)
}
}