Struct String

Source
pub struct String(/* private fields */);
Expand description

Handle to an internal Lua string.

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

Implementations§

Source§

impl String

Source

pub fn to_str(&self) -> Result<BorrowedStr<'_>>

Get a BorrowedStr 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) -> StdString

Converts this string to a StdString.

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

This method returns StdString instead of Cow<'_, str> because lifetime cannot be bound to a weak Lua object.

§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 display(&self) -> impl Display + '_

Returns an object that implements Display for safely printing a Lua String that may contain non-Unicode data.

This may perform lossy conversion.

Source

pub fn as_bytes(&self) -> BorrowedBytes<'_>

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) -> BorrowedBytes<'_>

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§

impl String

Source

pub fn wrap(data: impl AsRef<[u8]>) -> impl IntoLua

Wraps bytes, returning an opaque type that implements IntoLua trait.

This function uses Lua::create_string under the hood.

Trait Implementations§

Source§

impl Clone for String

Source§

fn clone(&self) -> String

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 Debug for String

Source§

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

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

impl<'a> From<&'a String> for BorrowedBytes<'a>

Source§

fn from(value: &'a String) -> Self

Converts to this type from the input type.
Source§

impl FromLua for String

Source§

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

Performs the conversion.
Source§

impl Hash for String

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 IntoLua for &String

Source§

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

Performs the conversion.
Source§

impl IntoLua for String

Source§

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

Performs the conversion.
Source§

impl Ord for String

Source§

fn cmp(&self, other: &String) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

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

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for String

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn partial_cmp(&self, other: &T) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl PartialOrd for String

Source§

fn partial_cmp(&self, other: &String) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for String

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<'a> TryFrom<&'a String> for BorrowedStr<'a>

Source§

type Error = Error

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

fn try_from(value: &'a String) -> Result<Self>

Performs the conversion.
Source§

impl Eq for String

Auto Trait Implementations§

§

impl Freeze for String

§

impl !RefUnwindSafe for String

§

impl Send for String

§

impl Sync for String

§

impl Unpin for String

§

impl !UnwindSafe for String

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

Source§

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

Performs the conversion. Read more
Source§

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

Source§

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

Source§

unsafe fn from_stack_args( nargs: i32, i: usize, to: Option<&str>, lua: &RawLua, ) -> 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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoLuaMulti for T
where T: IntoLua,

Source§

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

Performs the conversion.
Source§

unsafe fn push_into_stack_multi(self, lua: &RawLua) -> 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,

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

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.
Source§

impl<T> MaybeSend for T
where T: Send,