#[repr(transparent)]pub struct FString(_);
Expand description
FString contains an RString known to be interned.
Interned strings won’t be garbage collected or modified, so should be
safe to store on the heap or hold a &str
refrence to. FString
provides
a way to encode this property into the type system, and provides safe
methods to access the string as a &str
or slice.
Implementations§
source§impl FString
impl FString
sourcepub fn as_r_string(self) -> RString ⓘ
pub fn as_r_string(self) -> RString ⓘ
Returns the interned string as a RString
.
sourcepub fn as_slice(self) -> &'static [u8] ⓘ
pub fn as_slice(self) -> &'static [u8] ⓘ
Returns the interned string as a slice of bytes.
Examples
use magnus::{eval, RString};
let s: RString = eval!(r#"
# frozen_string_literal: true
"example"
"#).unwrap();
let fstring = s.as_interned_str().unwrap();
assert_eq!(fstring.as_slice(), &[101, 120, 97, 109, 112, 108, 101]);
sourcepub fn test_as_str(self) -> Option<&'static str>
pub fn test_as_str(self) -> Option<&'static str>
Returns the interned string as a &str
or None
string contains
invliad UTF-8.
Examples
use magnus::{eval, RString};
let s: RString = eval!(r#"# frozen_string_literal: true
"example"
"#).unwrap();
let fstring = s.as_interned_str().unwrap();
assert_eq!(fstring.test_as_str().unwrap(), "example");
sourcepub fn as_str(self) -> Result<&'static str, Error>
pub fn as_str(self) -> Result<&'static str, Error>
Returns the interned string as a &str
. Errors if the string contains
invliad UTF-8.
Examples
use magnus::{eval, RString};
let s: RString = eval!(r#"# frozen_string_literal: true
"example"
"#).unwrap();
let fstring = s.as_interned_str().unwrap();
assert_eq!(fstring.as_str().unwrap(), "example");
sourcepub fn to_string_lossy(self) -> Cow<'static, str>
pub fn to_string_lossy(self) -> Cow<'static, str>
Returns interned string as a Rust string, ignoring the Ruby encoding
and dropping any non-UTF-8 characters. If the string is valid UTF-8
this will return a &str
reference.
Examples
use magnus::{eval, RString};
let s: RString = eval!(r#"
# frozen_string_literal: true
"example"
"#).unwrap();
let fstring = s.as_interned_str().unwrap();
assert_eq!(fstring.to_string_lossy(), "example");
Methods from Deref<Target = Value>§
sourcepub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
pub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
Convert self
to a Rust string.
Safety
This may return a direct view of memory owned and managed by Ruby. Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.
This can be used safely by immediately calling
into_owned
on the return value.
Examples
use magnus::{eval, QTRUE};
let value = QTRUE;
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.to_s() }.unwrap().into_owned();
assert_eq!(s, "true");
sourcepub unsafe fn classname(&self) -> Cow<'_, str>
pub unsafe fn classname(&self) -> Cow<'_, str>
Return the name of self
’s class.
Safety
Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.
This can be used safely by immediately calling
into_owned
on the return value.
Examples
use magnus::{eval, RHash};
let value = RHash::new();
// safe as we never give Ruby a chance to free the string.
let s = unsafe { value.classname() }.into_owned();
assert_eq!(s, "Hash");