Struct mlua::String

source ·
pub struct String<'lua>(/* private fields */);
Expand description

Handle to an internal Lua string.

Unlike Rust strings, Lua strings may not be valid UTF-8.

Implementations§

source§

impl<'lua> String<'lua>

source

pub fn to_str(&self) -> Result<&str>

Get a &str slice if the Lua string is valid UTF-8.

§Examples
let globals = lua.globals();

let version: String = globals.get("_VERSION")?;
assert!(version.to_str()?.contains("Lua"));

let non_utf8: String = lua.load(r#"  "test\255"  "#).eval()?;
assert!(non_utf8.to_str().is_err());
source

pub fn to_string_lossy(&self) -> Cow<'_, str>

Converts this string to a Cow<str>.

Any non-Unicode sequences are replaced with U+FFFD REPLACEMENT CHARACTER.

§Examples
let lua = Lua::new();

let s = lua.create_string(b"test\xff")?;
assert_eq!(s.to_string_lossy(), "test\u{fffd}");
source

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

Get the bytes that make up this string.

The returned slice will not contain the terminating nul byte, but will contain any nul bytes embedded into the Lua string.

§Examples
let non_utf8: String = lua.load(r#"  "test\255"  "#).eval()?;
assert!(non_utf8.to_str().is_err());    // oh no :(
assert_eq!(non_utf8.as_bytes(), &b"test\xff"[..]);
source

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

Get the bytes that make up this string, including the trailing nul byte.

source

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

Converts this string to a generic C pointer.

There is no way to convert the pointer back to its original value.

Typically this function is used only for hashing and debug information.

source

pub fn into_owned(self) -> OwnedString

Available on crate feature unstable and non-crate feature send only.

Convert this handle to owned version.

Trait Implementations§

source§

impl<'lua> AsRef<[u8]> for String<'lua>

source§

fn as_ref(&self) -> &[u8]

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

impl<'lua> Borrow<[u8]> for String<'lua>

source§

fn borrow(&self) -> &[u8]

Immutably borrows from an owned value. Read more
source§

impl<'lua> Clone for String<'lua>

source§

fn clone(&self) -> String<'lua>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<'lua> Debug for String<'lua>

source§

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

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

impl<'lua> FromLua<'lua> for String<'lua>

source§

fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<String<'lua>>

Performs the conversion.
source§

impl<'lua> Hash for String<'lua>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'lua> IntoLua<'lua> for &String<'lua>

source§

fn into_lua(self, _: &'lua Lua) -> Result<Value<'lua>>

Performs the conversion.
source§

impl<'lua> IntoLua<'lua> for String<'lua>

source§

fn into_lua(self, _: &'lua Lua) -> Result<Value<'lua>>

Performs the conversion.
source§

impl<'lua, T> PartialEq<T> for String<'lua>
where T: AsRef<[u8]> + ?Sized,

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'lua> Serialize for String<'lua>

source§

fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'lua> Eq for String<'lua>

Auto Trait Implementations§

§

impl<'lua> Freeze for String<'lua>

§

impl<'lua> !RefUnwindSafe for String<'lua>

§

impl<'lua> !Send for String<'lua>

§

impl<'lua> !Sync for String<'lua>

§

impl<'lua> Unpin for String<'lua>

§

impl<'lua> !UnwindSafe for String<'lua>

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<'lua, T> FromLuaMulti<'lua> for T
where T: FromLua<'lua>,

source§

fn from_lua_multi(values: MultiValue<'lua>, lua: &'lua Lua) -> Result<T, Error>

Performs the conversion. Read more
source§

fn from_lua_args( args: MultiValue<'lua>, i: usize, to: Option<&str>, lua: &'lua Lua ) -> Result<T, Error>

source§

unsafe fn from_stack_multi(nvals: i32, lua: &'lua Lua) -> Result<T, Error>

source§

unsafe fn from_stack_args( nargs: i32, i: usize, to: Option<&str>, lua: &'lua Lua ) -> Result<T, Error>

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<'lua, T> IntoLuaMulti<'lua> for T
where T: IntoLua<'lua>,

source§

fn into_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>, Error>

Performs the conversion.
source§

unsafe fn push_into_stack_multi(self, lua: &'lua Lua) -> Result<i32, Error>

source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer ) -> Result<(), ErrorImpl>

source§

impl<T> ToOwned for T
where T: Clone,

§

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

§

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.