Struct magnus::symbol::Symbol

source ·
pub struct Symbol(/* private fields */);
Expand description

A type wrapping either a StaticSymbol or a Value pointer to a RSymbol struct.

See the ReprValue trait for additional methods available on this type. See Ruby for methods to create a Symbol.

Implementations§

source§

impl Symbol

source

pub fn from_value(val: Value) -> Option<Self>

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

§Examples
use magnus::{eval, Symbol};

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

pub fn new<T: AsRef<str>>(name: T) -> Self

Create a new Symbol from name.

§Panics

Panics if called from a non-Ruby thread. See Ruby::to_symbol for the non-panicking version.

§Examples
use magnus::{rb_assert, Symbol};

let sym = Symbol::new("example");
rb_assert!(":example == sym", sym);
source

pub fn is_static(self) -> bool

Returns whether self is static or not.

Static symbols won’t be garbage collected, so should be safe to store on the heap. See StaticSymbol.

§Examples
use magnus::{eval, Symbol};

assert!(eval::<Symbol>(":foo").unwrap().is_static());
assert!(!Symbol::new("bar").is_static());
assert!(!eval::<Symbol>(r#""baz".to_sym"#).unwrap().is_static());
source

pub fn name(self) -> Result<Cow<'static, str>, Error>

Return the symbol as a string. If the symbol is static this will be a &str, otherwise an owned String.

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

§Examples
use magnus::Symbol;

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

pub fn as_static(self) -> Option<StaticSymbol>

If self is static, returns self as a StaticSymbol, otherwise returns None.

§Examples
use magnus::{eval, Symbol};

assert!(eval::<Symbol>(":foo").unwrap().as_static().is_some());
assert!(Symbol::new("bar").as_static().is_none());
assert!(eval::<Symbol>(r#""baz".to_sym"#)
    .unwrap()
    .as_static()
    .is_none());
source

pub fn to_static(self) -> StaticSymbol

If self is already static simply returns self as a StaticSymbol. If self is not static it will be made so and returned as a StaticSymbol.

Be aware that once static a symbol will never be garbage collected.

§Examples
use magnus::{rb_assert, Symbol};

let sym = Symbol::new("example");
let static_sym = sym.to_static();
rb_assert!("sym == static_sym", sym, static_sym);

Trait Implementations§

source§

impl Borrow<Symbol> for StaticSymbol

source§

fn borrow(&self) -> &Symbol

Immutably borrows from an owned value. Read more
source§

impl Clone for Symbol

source§

fn clone(&self) -> Symbol

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 Symbol

source§

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

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

impl Display for Symbol

source§

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

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

impl EncodingCapable for Symbol

source§

fn enc_get(self) -> Index

Get the encoding of self. Read more
source§

fn enc_set<T>(self, enc: T) -> Result<(), Error>
where T: Into<Index>,

Set self’s encoding. Read more
source§

fn enc_associate<T>(self, enc: T) -> Result<(), Error>
where T: Into<Index>,

Set self’s encoding, along with performing additional fix-up self’s contents. Read more
source§

impl From<Id> for Symbol

source§

fn from(id: Id) -> Self

Converts to this type from the input type.
source§

impl From<StaticSymbol> for Symbol

source§

fn from(s: StaticSymbol) -> Self

Converts to this type from the input type.
source§

impl From<Symbol> for Id

source§

fn from(sym: Symbol) -> Self

Converts to this type from the input type.
source§

impl From<Symbol> for OpaqueId

source§

fn from(sym: Symbol) -> Self

Converts to this type from the input type.
source§

impl IntoId for Symbol

source§

fn into_id_with(self, _: &Ruby) -> Id

Convert self into Id.
source§

fn into_id(self) -> Id

Convert self into Id. Read more
source§

unsafe fn into_id_unchecked(self) -> Id

Convert self into Id. Read more
source§

impl IntoSymbol for Symbol

source§

fn into_symbol_with(self, _: &Ruby) -> Symbol

Convert self into Symbol. Read more
source§

fn into_symbol(self) -> Symbol

Convert self into Symbol. Read more
source§

unsafe fn into_symbol_unchecked(self) -> Symbol

Convert self into Symbol. Read more
source§

impl IntoValue for Symbol

source§

fn into_value_with(self, _: &Ruby) -> Value

Convert self into Value.
source§

fn into_value(self) -> Value

Convert self into Value. Read more
source§

unsafe fn into_value_unchecked(self) -> Value

Convert self into Value. Read more
source§

impl PartialEq<Id> for Symbol

source§

fn eq(&self, other: &Id) -> 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 PartialEq<LazyId> for Symbol

source§

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

§Panics

Panics if the first call is from a non-Ruby thread. The LazyId will then be poisoned and all future use of it will panic.

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 PartialEq<OpaqueId> for Symbol

source§

fn eq(&self, other: &OpaqueId) -> 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 PartialEq<StaticSymbol> for Symbol

source§

fn eq(&self, other: &StaticSymbol) -> 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 PartialEq<Symbol> for Id

source§

fn eq(&self, other: &Symbol) -> 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 PartialEq<Symbol> for LazyId

source§

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

§Panics

Panics if the first call is from a non-Ruby thread. This LazyId will then be poisoned and all future use of it will panic.

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 PartialEq<Symbol> for OpaqueId

source§

fn eq(&self, other: &Symbol) -> 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 PartialEq<Symbol> for StaticSymbol

source§

fn eq(&self, other: &Symbol) -> 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 PartialEq for Symbol

source§

fn eq(&self, other: &Symbol) -> 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 ReprValue for Symbol

source§

fn as_value(self) -> Value

Return self as a Value.
source§

fn is_nil(self) -> bool

Returns whether self is Ruby’s nil value. Read more
source§

fn equal<T>(self, other: T) -> Result<bool, Error>
where T: ReprValue,

Checks for equality, delegating to the Ruby method #==. Read more
source§

fn eql<T>(self, other: T) -> Result<bool, Error>
where T: ReprValue,

Checks for equality, delegating to the Ruby method #eql?. Read more
source§

fn hash(self) -> Result<Integer, Error>

Returns an integer non-uniquely identifying self. Read more
source§

fn class(self) -> RClass

Returns the class that self is an instance of. Read more
source§

fn is_frozen(self) -> bool

Returns whether self is ‘frozen’. Read more
source§

fn check_frozen(self) -> Result<(), Error>

Returns an error if self is ‘frozen’. Read more
source§

fn freeze(self)

Mark self as frozen. Read more
source§

fn to_bool(self) -> bool

Convert self to a bool, following Ruby’s rules of false and nil as boolean false and everything else boolean true. Read more
source§

fn funcall<M, A, T>(self, method: M, args: A) -> Result<T, Error>
where M: IntoId, A: ArgList, T: TryConvert,

Call the method named method on self with args. Read more
source§

fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
where M: IntoId, A: ArgList, T: TryConvert,

Call the public method named method on self with args. Read more
source§

fn check_funcall<M, A, T>(self, method: M, args: A) -> Option<Result<T, Error>>
where M: IntoId, A: ArgList, T: TryConvert,

If self responds to the method named method, call it with args. Read more
source§

fn funcall_with_block<M, A, T>( self, method: M, args: A, block: Proc ) -> Result<T, Error>
where M: IntoId, A: ArgList, T: TryConvert,

Call the method named method on self with args and block. Read more
source§

fn block_call<M, A, R, T>( self, method: M, args: A, block: fn(_: &[Value], _: Option<Proc>) -> R ) -> Result<T, Error>
where M: IntoId, A: ArgList, R: BlockReturn, T: TryConvert,

Call the method named method on self with args and block. Read more
source§

fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>
where M: IntoId,

Check if self responds to the given Ruby method. Read more
source§

fn to_r_string(self) -> Result<RString, Error>

Convert self to a Ruby String. Read more
source§

unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>

Convert self to a Rust string. Read more
source§

fn inspect(self) -> String

Convert self to its Ruby debug representation. Read more
source§

unsafe fn classname(&self) -> Cow<'_, str>

Return the name of self’s class. Read more
source§

fn is_kind_of<T>(self, class: T) -> bool
where T: ReprValue + Module,

Returns whether or not self is an instance of class. Read more
source§

fn enumeratorize<M, A>(self, method: M, args: A) -> Enumerator
where M: IntoSymbol, A: ArgList,

Generate an Enumerator from method on self, passing args to method. Read more
source§

impl TryConvert for Symbol

source§

fn try_convert(val: Value) -> Result<Self, Error>

Convert val into Self.
source§

impl Copy for Symbol

source§

impl Eq for Symbol

source§

impl StructuralPartialEq for Symbol

Auto Trait Implementations§

§

impl Freeze for Symbol

§

impl RefUnwindSafe for Symbol

§

impl Send for Symbol

§

impl Sync for Symbol

§

impl Unpin for Symbol

§

impl UnwindSafe for Symbol

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> AsRawValue for T
where T: ReprValue,

source§

fn as_raw(self) -> u64

Available on crate feature rb-sys only.
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<T> Inspect for T
where T: Debug,

source§

fn inspect(&self) -> String

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,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.
source§

impl<T> BlockReturn for T
where T: BlockReturn,

source§

impl<T> Locate for T
where T: ReprValue,

source§

impl<T> Mark for T
where T: ReprValue,

source§

impl<T> ReturnValue for T
where T: ReturnValue,