Struct magnus::value::StaticSymbol

source ·
#[repr(transparent)]
pub struct StaticSymbol(_);
Expand description

A static Ruby symbol that will live for the life of the program and never be garbage collected.

See also Symbol.

All Value methods should be available on this type through Deref, but some may be missed by this documentation.

Implementations§

Return Some(StaticSymbol) if val is a StaticSymbol, None otherwise.

Examples
use magnus::{eval, StaticSymbol};

assert!(StaticSymbol::from_value(eval(":foo").unwrap()).is_some());
assert!(StaticSymbol::from_value(eval(r#""bar""#).unwrap()).is_none());
assert!(StaticSymbol::from_value(eval(r#""baz".to_sym"#).unwrap()).is_none());

Create a new StaticSymbol.

Examples
use magnus::{eval, StaticSymbol};

let sym = StaticSymbol::new("example");
let result: bool = eval!(":example == sym", sym).unwrap();
assert!(result);

Return the Id for name, if one exists.

Examples
use magnus::{eval, StaticSymbol};

assert!(StaticSymbol::check("example").is_none());
eval::<StaticSymbol>(":example").unwrap();
assert!(StaticSymbol::check("example").is_some());

Return the symbol as a static string reference.

May error if the name is not valid utf-8.

Examples
use magnus::StaticSymbol;

let sym = StaticSymbol::new("example");
assert_eq!(sym.name().unwrap(), "example");

Methods from Deref<Target = Value>§

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");

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");

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
Formats the value using the given formatter. Read more
Get the encoding of self. Read more
Set self’s encoding. Read more
Set self’s encoding, along with performing additional fix-up self’s contents. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Convert val into Self.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.