pub struct Strict<T>(pub T);
Expand description
A wrapper type for reading lua numbers of concrete precisions without implicit coercions.
By default when reading a numeric type (int or float) from a lua number
(i.e. calling LuaRead::lua_read_at_position
) the resulting number will
be implicitly coerced into the target type. For example if the lua number
has a non zero fractional part it will be discarded when reading the number
as integer.
use tlua::Lua;
let lua = tlua::Lua::new();
let i: Option<i32> = lua.eval("return 3.14").ok();
assert_eq!(i, Some(3));
If you don’t want the implicit coercision, you can use the Strict
wrapper:
use tlua::Strict;
let i: Option<Strict<i32>> = lua.eval("return 3.14").ok();
assert_eq!(i, None); // would result in loss of data
let f: Option<Strict<f64>> = lua.eval("return 3.14").ok();
assert_eq!(f, Some(Strict(3.14))); // ok
This strictness also applies in terms of number sizes:
let i: Option<u8> = lua.eval("return 256").ok();
assert_eq!(i, Some(0)); // non-strict => data loss
let i: Option<Strict<u8>> = lua.eval("return 256").ok();
assert_eq!(i, None); // strict => must not lose data
let i: Option<Strict<u16>> = lua.eval("return 256").ok();
assert_eq!(i, Some(Strict(256))); // strict => no data loss
Reading cdata
This type only supports regular lua numbers. If you want to read a luajit
cdata type, use the CData
wrapper instead.
Tuple Fields§
§0: T
Trait Implementations§
source§impl<L> LuaRead<L> for Strict<f32>where
L: AsLua,
impl<L> LuaRead<L> for Strict<f32>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<f64>where
L: AsLua,
impl<L> LuaRead<L> for Strict<f64>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<i16>where
L: AsLua,
impl<L> LuaRead<L> for Strict<i16>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<i32>where
L: AsLua,
impl<L> LuaRead<L> for Strict<i32>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<i64>where
L: AsLua,
impl<L> LuaRead<L> for Strict<i64>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<i8>where
L: AsLua,
impl<L> LuaRead<L> for Strict<i8>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<isize>where
L: AsLua,
impl<L> LuaRead<L> for Strict<isize>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<u16>where
L: AsLua,
impl<L> LuaRead<L> for Strict<u16>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<u32>where
L: AsLua,
impl<L> LuaRead<L> for Strict<u32>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<u64>where
L: AsLua,
impl<L> LuaRead<L> for Strict<u64>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<u8>where
L: AsLua,
impl<L> LuaRead<L> for Strict<u8>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<L> LuaRead<L> for Strict<usize>where
L: AsLua,
impl<L> LuaRead<L> for Strict<usize>where
L: AsLua,
source§fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
fn lua_read_at_position(lua: L, index: NonZeroI32) -> ReadResult<Self, L>
Reads the data from Lua at a given position.
fn n_values_expected() -> i32
source§fn lua_read(lua: L) -> ReadResult<Self, L>
fn lua_read(lua: L) -> ReadResult<Self, L>
Reads the data from Lua.
fn lua_read_at_maybe_zero_position(lua: L, index: i32) -> ReadResult<Self, L>where
L: AsLua,
source§impl<T: Ord> Ord for Strict<T>
impl<T: Ord> Ord for Strict<T>
source§impl<T: PartialEq> PartialEq for Strict<T>
impl<T: PartialEq> PartialEq for Strict<T>
source§impl<T: PartialOrd> PartialOrd for Strict<T>
impl<T: PartialOrd> PartialOrd for Strict<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl<T: Copy> Copy for Strict<T>
impl<T: Eq> Eq for Strict<T>
impl<T> StructuralEq for Strict<T>
impl<T> StructuralPartialEq for Strict<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Strict<T>where
T: RefUnwindSafe,
impl<T> Send for Strict<T>where
T: Send,
impl<T> Sync for Strict<T>where
T: Sync,
impl<T> Unpin for Strict<T>where
T: Unpin,
impl<T> UnwindSafe for Strict<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more