Struct tlua::CDataOnStack

source ·
pub struct CDataOnStack<'l, L> { /* private fields */ }
Expand description

A cdata value stored on lua stack. Can be used to check type of cdata, access the raw bytes of the data, downcast it to a rust type or passed as an argument into a lua function.

Examples:

use tlua::{CDataOnStack, Lua, ffi};
let lua = Lua::new();
let cdata: CDataOnStack<_> = lua.eval("
    ffi = require 'ffi';
    return ffi.new('uint8_t', 69)
").unwrap();

// check CTypeID
assert_eq!(cdata.ctypeid(), ffi::CTID_UINT8);
// check raw bytes
assert_eq!(cdata.data(), [69]);

// pass to a lua function
let n: u8 = lua.eval_with("return ... + 1", &cdata).unwrap();
assert_eq!(n, 70);

Implementations§

source§

impl<L> CDataOnStack<'_, L>
where L: AsLua,

source

pub fn as_ptr(&self) -> *const c_void

Return pointer to data. Maybe use CDataOnStack::data instead.

source

pub fn try_as_bytes(&self) -> Option<&[u8]>

Return a slice of bytes covering the data if the data’s size was already retrieved before. Otherwise return None.

See also CDataOnStack::data.

source

pub fn try_as_bytes_mut(&mut self) -> Option<&mut [u8]>

Return a mutable slice of bytes covering the data if the data’s size was already retrieved before. Otherwise return None.

See also CDataOnStack::data_mut.

source

pub fn data(&self) -> &[u8]

Return a slice of bytes covering the data. Calling this function the first time around will perform an expensive operation of retrieving the data’s size. So if for some reason you just need the pointer to the data, use the CDataOnStack::as_ptr. But if you actually need the bytes, use this function.

source

pub fn data_mut(&mut self) -> &mut [u8]

Return a mutable slice of bytes covering the data. Calling this function the first time around will perform an expensive operation of retrieving the data’s size.

source

pub fn ctypeid(&self) -> CTypeID

Return the ctypeid of the cdata.

source

pub fn try_downcast<T>(&self) -> Option<&T>
where T: AsCData,

Return a reference to the underlying value if self.ctypeid() == <T as AsCData>::ctypeid(), otherwise return None.

This function may panic, if self.data().len() != std::mem::size_of::<T>()

source

pub fn try_downcast_mut<T>(&self) -> Option<&mut T>
where T: AsCData,

Return a mutable reference to the underlying value if self.ctypeid() == <T as AsCData>::ctypeid(), otherwise return None.

This function may panic, if self.data().len() != std::mem::size_of::<T>()

source

pub fn try_downcast_into<T>(self) -> Result<T, Self>
where T: AsCData,

Return the underlying value consuming self if self.ctypeid() == <T as AsCData>::ctypeid(), otherwise return Err(self).

This function may panic, if self.data().len() != std::mem::size_of::<T>()

Trait Implementations§

source§

impl<L> AsLua for CDataOnStack<'_, L>
where L: AsLua,

source§

fn as_lua(&self) -> LuaState

source§

fn try_push<T>( self, v: T ) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>
where Self: Sized, T: PushInto<Self>,

Try to push v onto the lua stack. Read more
source§

fn push<T>(self, v: T) -> PushGuard<Self>
where Self: Sized, T: PushInto<Self>, <T as PushInto<Self>>::Err: Into<Void>,

Push v onto the lua stack. Read more
source§

fn try_push_one<T>( self, v: T ) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>
where Self: Sized, T: PushOneInto<Self>,

Try to push v onto the lua stack. Read more
source§

fn push_one<T>(self, v: T) -> PushGuard<Self>
where Self: Sized, T: PushOneInto<Self>, <T as PushInto<Self>>::Err: Into<Void>,

Push v onto the lua stack. Read more
source§

fn push_iter<I>(self, iterator: I) -> Result<PushGuard<Self>, Self>
where Self: Sized, I: Iterator, <I as Iterator>::Item: PushInto<LuaState>, <<I as Iterator>::Item as PushInto<LuaState>>::Err: Into<Void>,

Push iterator onto the lua stack as a lua table. Read more
source§

fn try_push_iter<I>( self, iterator: I ) -> Result<PushGuard<Self>, (PushIterErrorOf<I>, Self)>
where Self: Sized, I: Iterator, <I as Iterator>::Item: PushInto<LuaState>,

Push iterator onto the lua stack as a lua table. Read more
source§

fn read<T>(self) -> ReadResult<T, Self>
where Self: Sized, T: LuaRead<Self>,

source§

fn read_at<T>(self, index: i32) -> ReadResult<T, Self>
where Self: Sized, T: LuaRead<Self>,

source§

fn read_at_nz<T>(self, index: NonZeroI32) -> ReadResult<T, Self>
where Self: Sized, T: LuaRead<Self>,

source§

fn pcall<F, R>(&self, f: F) -> Result<R, LuaError>
where F: FnOnce(StaticLua) -> R,

Call a rust function in protected mode. If a lua error is thrown during execution of f the function will return a LuaError. Read more
source§

impl<'l, L: Debug> Debug for CDataOnStack<'l, L>

source§

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

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

impl<'a, L> From<CDataOnStack<'a, L>> for Object<L>

source§

fn from(ud: CDataOnStack<'a, L>) -> Self

Converts to this type from the input type.
source§

impl<L> LuaRead<L> for CDataOnStack<'_, L>
where L: AsLua,

source§

fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>

Reads the data from Lua at a given position.
source§

fn n_values_expected() -> i32

source§

fn lua_read(lua: L) -> ReadResult<Self, L>

Reads the data from Lua.
source§

fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>
where L: AsLua,

source§

impl<L, O> Push<L> for CDataOnStack<'_, O>
where L: AsLua, O: AsLua,

§

type Err = Void

Error that can happen when pushing a value.
source§

fn push_to_lua(&self, lua: L) -> Result<PushGuard<L>, (Self::Err, L)>

Pushes the value on the top of the stack. Read more
source§

fn push_no_err(&self, lua: L) -> PushGuard<L>
where <Self as Push<L>>::Err: Into<Void>,

Same as push_to_lua but can only succeed and is only available if Err implements Into<Void>.
source§

impl<L> TryFrom<Object<L>> for CDataOnStack<'_, L>
where L: AsLua,

§

type Error = Object<L>

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

fn try_from(o: Object<L>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'l, L> !RefUnwindSafe for CDataOnStack<'l, L>

§

impl<'l, L> !Send for CDataOnStack<'l, L>

§

impl<'l, L> !Sync for CDataOnStack<'l, L>

§

impl<'l, L> Unpin for CDataOnStack<'l, L>
where L: Unpin,

§

impl<'l, L> UnwindSafe for CDataOnStack<'l, L>
where L: UnwindSafe,

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

§

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

§

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.