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,
impl<L> CDataOnStack<'_, L>where
L: AsLua,
sourcepub fn as_ptr(&self) -> *const c_void
pub fn as_ptr(&self) -> *const c_void
Return pointer to data. Maybe use CDataOnStack::data
instead.
sourcepub fn try_as_bytes(&self) -> Option<&[u8]>
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
.
sourcepub fn try_as_bytes_mut(&mut self) -> Option<&mut [u8]>
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
.
sourcepub fn data(&self) -> &[u8] ⓘ
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.
sourcepub fn data_mut(&mut self) -> &mut [u8] ⓘ
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.
sourcepub fn try_downcast<T>(&self) -> Option<&T>where
T: AsCData,
pub fn try_downcast<T>(&self) -> Option<&T>where
T: AsCData,
sourcepub fn try_downcast_mut<T>(&self) -> Option<&mut T>where
T: AsCData,
pub fn try_downcast_mut<T>(&self) -> Option<&mut T>where
T: AsCData,
Trait Implementations§
source§impl<L> AsLua for CDataOnStack<'_, L>where
L: AsLua,
impl<L> AsLua for CDataOnStack<'_, L>where
L: AsLua,
fn as_lua(&self) -> LuaState
source§fn try_push<T>(
self,
v: T,
) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>
fn try_push<T>( self, v: T, ) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>
v
onto the lua stack. Read moresource§fn try_push_one<T>(
self,
v: T,
) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>where
Self: Sized,
T: PushOneInto<Self>,
fn try_push_one<T>(
self,
v: T,
) -> Result<PushGuard<Self>, (<T as PushInto<Self>>::Err, Self)>where
Self: Sized,
T: PushOneInto<Self>,
v
onto the lua stack. Read moresource§fn push_iter<I>(self, iterator: I) -> Result<PushGuard<Self>, Self>
fn push_iter<I>(self, iterator: I) -> Result<PushGuard<Self>, Self>
iterator
onto the lua stack as a lua table. Read moresource§fn try_push_iter<I>(
self,
iterator: I,
) -> Result<PushGuard<Self>, (PushIterErrorOf<I>, Self)>
fn try_push_iter<I>( self, iterator: I, ) -> Result<PushGuard<Self>, (PushIterErrorOf<I>, Self)>
iterator
onto the lua stack as a lua table. Read more