Skip to main content

Value

Enum Value 

Source
#[repr(C, u64)]
pub enum Value { Nil, Bool(bool), Int(i64), Num(f64), Str(StrRef), Table(TableRef), Func(FuncRef), UserData(UdRef), Thread(ThreadRef), }
Expand description

A RemedyLua value. Strings, tables, functions, userdata and threads are references into the collector’s heap (StrRef and friends); the rest are plain data. Values are Copy: holding one does not keep what it names alive (see Root).

Variants§

§

Nil

nil.

§

Bool(bool)

true or false.

§

Int(i64)

An integer (64-bit, wrapping).

§

Num(f64)

A float (64-bit).

§

Str(StrRef)

A string (bytes, not necessarily UTF-8).

§

Table(TableRef)

A table.

§

Func(FuncRef)

A function.

§

UserData(UdRef)

A full userdata.

§

Thread(ThreadRef)

A coroutine.

Implementations§

Source§

impl Value

Source

pub fn truthy(self) -> bool

Whether it counts as true in a condition (everything but nil and false).

Source

pub fn is_nil(self) -> bool

Whether it is nil.

Examples found in repository?
examples/sechost.rs (line 40)
11fn main() {
12    let a: Vec<String> = std::env::args().collect();
13    let kind = a.get(1).cloned().unwrap_or_else(|| "ud".into());
14    let n: usize = a.get(2).and_then(|s| s.parse().ok()).unwrap_or(200_000);
15    let mut lua = Lua::new(Limits::default().memory(1 << 30).fuel(1 << 40), Libs::ALL);
16    lua.exec("KEEP = {}", "=s").unwrap();
17    let keep = match lua.exec("return KEEP", "=s").unwrap()[0] { Value::Table(t) => t, _ => panic!() };
18    let mut roots = Vec::new();
19    lua.collect_garbage().unwrap();
20    let (r0, m0) = (rss(), lua.memory_used());
21    for i in 0..n {
22        let v = match kind.as_str() {
23            "ud" | "ud_root" => lua.create_userdata(i as u64, None).unwrap(),
24            "ud_tracked" => lua.create_userdata_tracked(i as u64, None, i as u64).unwrap(),
25            "fn" | "fn_root" => lua.create_function("f", move |_, _| Ok(vec![Value::Int(i as i64)])).unwrap(),
26            "root" => Value::Int(i as i64),
27            "register" => {
28                lua.register(&format!("g{i}"), move |_, _| Ok(vec![Value::Int(i as i64)])).unwrap();
29                Value::Nil
30            }
31            _ => panic!("kind"),
32        };
33        if kind.ends_with("root") {
34            let t = if kind == "root" { lua.create_table().map(Value::Table).unwrap() } else { v };
35            roots.push(lua.root(t));
36            if kind == "root" {
37                continue;
38            }
39        }
40        if !v.is_nil() {
41            lua.table_set(keep, Value::Int(i as i64 + 1), v).unwrap();
42        }
43        if i % 4096 == 0 {
44            lua.exec("", "=s").unwrap();
45        }
46    }
47    lua.exec("", "=s").unwrap();
48    lua.collect_garbage().unwrap();
49    let (r1, m1) = (rss(), lua.memory_used());
50    let (dr, dm) = (r1.saturating_sub(r0) as f64 / n as f64, m1.saturating_sub(m0) as f64 / n as f64);
51    println!("{kind}: {dr:.0} B resident, {dm:.0} B counted per object ({:.2}x)", dr / dm.max(1.0));
52    drop(roots);
53}
Source

pub fn type_name(self) -> &'static str

Lua’s name for its type, as type returns it.

Source

pub fn raw_eq(self, other: Value) -> bool

Equality without metamethods, by identity for strings: short strings (up to 40 bytes) are interned, so this is rawequal for them, but two equal long strings may be different objects. Hosts compare values with Lua::raw_equal.

Source

pub fn as_int(self) -> Option<i64>

The integer it is, or converts to exactly (a float with an integral value).

Source

pub fn as_num(self) -> Option<f64>

The number it is, as a float.

Source

pub fn as_bool(self) -> Option<bool>

The boolean it is.

Source

pub fn as_table(self) -> Option<TableRef>

The table it is.

Source

pub fn as_func(self) -> Option<FuncRef>

The function it is.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Value

Source§

impl Debug for Value

Source§

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

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

impl Default for Value

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> bool

Raw equality without the heap: 1 == 1.0, objects by identity, strings by identity (equal to by content for short strings only). Compare script values with Lua::raw_equal, which compares strings by content.

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl Forget for Value

§

impl Freeze for Value

§

impl Move for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

§

impl UnwindSafe for Value

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.