rune

Struct Ref

Source
pub struct Ref<T: ?Sized> { /* private fields */ }
Expand description

A strong owned reference to the given type that can be safely dereferenced.

§Examples

Constructed from a static value:

use rune::Ref;

let value: Ref<str> = Ref::from_static("hello world");

Implementations§

Source§

impl<T: ?Sized> Ref<T>

Source

pub const fn from_static(value: &'static T) -> Ref<T>

Construct an owned reference from a static value.

§Examples
use rune::Ref;

let value: Ref<str> = Ref::from_static("Hello World");
assert_eq!(value.as_ref(), "Hello World");
Source

pub fn map<U: ?Sized, F>(this: Self, f: F) -> Ref<U>
where F: FnOnce(&T) -> &U,

Map the interior reference of an owned mutable value.

§Examples
use rune::Ref;
use rune::runtime::Bytes;
use rune::alloc::try_vec;

let bytes = rune::to_value(Bytes::from_vec(try_vec![1, 2, 3, 4]))?;
let bytes: Ref<Bytes> = rune::from_value(bytes)?;
let value: Ref<[u8]> = Ref::map(bytes, |vec| &vec[0..2]);

assert_eq!(&*value, &[1, 2][..]);
Source

pub fn try_map<U: ?Sized, F>(this: Self, f: F) -> Result<Ref<U>, Ref<T>>
where F: FnOnce(&T) -> Option<&U>,

Try to map the reference to a projection.

§Examples
use rune::Ref;
use rune::runtime::Bytes;
use rune::alloc::try_vec;

let bytes = rune::to_value(Bytes::from_vec(try_vec![1, 2, 3, 4]))?;
let bytes: Ref<Bytes> = rune::from_value(bytes)?;

let Ok(value) = Ref::try_map(bytes, |bytes| bytes.get(0..2)) else {
    panic!("Conversion failed");
};

assert_eq!(&value[..], &[1, 2][..]);
Source

pub fn into_raw(this: Self) -> (NonNull<T>, RawAnyGuard)

Convert into a raw pointer and associated raw access guard.

§Safety

The returned pointer must not outlive the associated guard, since this prevents other uses of the underlying data which is incompatible with the current.

Source

pub unsafe fn from_raw(value: NonNull<T>, guard: RawAnyGuard) -> Self

Convert a raw reference and guard into a regular reference.

§Safety

The caller is responsible for ensuring that the raw reference is associated with the specific pointer.

Trait Implementations§

Source§

impl<T: ?Sized> AsRef<T> for Ref<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Debug for Ref<T>
where T: Debug + ?Sized,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: ?Sized> Deref for Ref<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> From<Arc<T>> for Ref<T>

Source§

fn from(value: Arc<T>) -> Ref<T>

Construct from an atomically reference-counted value.

§Examples
use std::sync::Arc;
use rune::Ref;

let value: Ref<String> = Ref::from(Arc::new(String::from("hello world")));
assert_eq!(value.as_ref(), "hello world");
Source§

impl<T> From<Rc<T>> for Ref<T>

Source§

fn from(value: Rc<T>) -> Ref<T>

Construct from an atomically reference-counted value.

§Examples
use std::rc::Rc;
use rune::Ref;

let value: Ref<String> = Ref::from(Rc::new(String::from("hello world")));
assert_eq!(value.as_ref(), "hello world");
Source§

impl<T> FromValue for Ref<T>
where T: AnyMarker,

Source§

fn from_value(value: Value) -> Result<Self, RuntimeError>

Try to convert to the given type, from the given value.
Source§

impl FromValue for Ref<Tuple>

Source§

fn from_value(value: Value) -> Result<Self, RuntimeError>

Try to convert to the given type, from the given value.
Source§

impl FromValue for Ref<str>

Source§

fn from_value(value: Value) -> Result<Self, RuntimeError>

Try to convert to the given type, from the given value.
Source§

impl<T> MaybeTypeOf for Ref<T>
where T: ?Sized + MaybeTypeOf,

Source§

fn maybe_type_of() -> Result<DocType>

Type information for the given type.
Source§

impl<T> TypeHash for Ref<T>
where T: ?Sized + TypeHash,

Blanket implementation for owned references.

Source§

const HASH: Hash = T::HASH

The complete type hash of the type including type parameters which uniquely identifiers a given type. Read more
Source§

impl<T> TypeOf for Ref<T>
where T: ?Sized + TypeOf,

Blanket implementation for owned references.

Source§

const PARAMETERS: Hash = T::PARAMETERS

Type parameters for the type. Read more
Source§

const STATIC_TYPE_INFO: AnyTypeInfo = T::STATIC_TYPE_INFO

Access diagnostical type information for the current type. Read more
Source§

fn type_info() -> TypeInfo

Get type info associated with the current type.

Auto Trait Implementations§

§

impl<T> Freeze for Ref<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for Ref<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> !Send for Ref<T>

§

impl<T> !Sync for Ref<T>

§

impl<T> Unpin for Ref<T>
where T: ?Sized,

§

impl<T> UnwindSafe for Ref<T>
where T: RefUnwindSafe + ?Sized,

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> EncodedChars for T
where T: AsRef<str>,

Source§

fn start_ptr(&self) -> *const u8

Pointer to the start of the pattern Read more
Source§

fn limit_ptr(&self) -> *const u8

Pointer to the limit of the pattern buffer Read more
Source§

fn len(&self) -> usize

The length of this buffer
Source§

fn encoding(&self) -> *mut OnigEncodingTypeST

The encoding of the contents of the buffer
Source§

fn is_empty(&self) -> bool

Is the buffer empty?
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Formattable for T
where T: Deref, <T as Deref>::Target: Formattable,

Source§

impl<T> MaybeSendSync for T

Source§

impl<T> Parsable for T
where T: Deref, <T as Deref>::Target: Parsable,