[][src]Struct rosy::Hash

#[repr(transparent)]
pub struct Hash<K = AnyObject, V = AnyObject> { /* fields omitted */ }

An instance of Ruby's Hash class.

Methods

impl<K: Object, V: Object> Hash<K, V>[src]

pub fn new() -> Self[src]

Creates a new hash table.

pub fn from_map<'a, M, MK, MV>(map: M) -> Self where
    M: IntoIterator<Item = (&'a MK, &'a MV)>,
    MK: Copy + Into<K> + 'a,
    MV: Copy + Into<V> + 'a, 
[src]

Creates an instance from the key-value pairs in map.

Examples

This initializer is general enough to work with most map types. The most common use case would probably be interacting with Rust's HashMap.

use std::collections::HashMap;
use rosy::prelude::*;

let mut map = HashMap::new();
map.insert("is_working", true);

let hash = Hash::<String, AnyObject>::from_map(&map);
assert_eq!(hash.get("is_working").unwrap(), true);

pub fn from_pairs(pairs: &[(K, V)]) -> Self[src]

This is supported on feature="ruby_2_6" only.

Creates a new hash table from pairs.

Examples

Although this may insert the objects efficiently, it requires a bit more verbose code with explicit Into conversions from non-Ruby types.

use rosy::{Hash, String};

let hash = Hash::<String, String>::from_pairs(&[
    ("user".into(), "nvzqz".into()),
    ("name".into(), "Nikolai Vazquez".into()),
]);

assert_eq!(hash.get("user").unwrap(), "nvzqz");
assert_eq!(hash.get("name").unwrap(), "Nikolai Vazquez");

pub unsafe fn insert(self, key: impl Into<K>, val: impl Into<V>)[src]

Associates val with key.

Safety

The caller must ensure that self is not frozen or else a FrozenError exception will be raised.

Examples

Rust types can automagically be converted to keys and values:

use rosy::prelude::*;

let hash = Hash::<String, AnyObject>::new();
unsafe { hash.insert("should_eat", true) };

assert_eq!(hash.to_s(), r#"{"should_eat"=>true}"#);

pub unsafe fn insert_pairs(self, pairs: &[(K, V)])[src]

This is supported on feature="ruby_2_6" only.

Inserts pairs into self in bulk.

Safety

The caller must ensure that self is not frozen or else a FrozenError exception will be raised.

pub fn get(self, key: impl Into<K>) -> Option<V>[src]

Returns the value for key.

pub fn len(self) -> usize[src]

Returns the number of key-value pairs in self.

pub fn is_empty(self) -> bool[src]

Returns whether self is empty.

pub unsafe fn remove(self, key: impl Into<K>) -> Option<V>[src]

Removes the value associated with key from self and returns it.

Safety

The caller must ensure that self is not frozen or else a FrozenError exception will be raised.

Examples

use rosy::prelude::*;

let hash = Hash::<String, AnyObject>::new();

unsafe {
    assert!(hash.remove("not_here").is_none());
    hash.insert("is_here", true);
    assert_eq!(hash.remove("is_here").unwrap(), true);
}

pub unsafe fn clear(self)[src]

Removes all elements from self in-place.

Safety

The caller must ensure that self is not frozen or else a FrozenError exception will be raised.

Trait Implementations

impl GcInfoKey for Hash[src]

impl<K: Object, V: Object> Object for Hash<K, V>[src]

unsafe fn from_raw(raw: usize) -> Self[src]

Creates a new object from raw without checking. Read more

unsafe fn cast_unchecked(obj: impl Object) -> Self[src]

Casts obj to Self without checking its type.

fn into_any_object(self) -> AnyObject[src]

Returns self as an AnyObject.

fn as_any_object(&self) -> &AnyObject[src]

Returns a reference to self as an AnyObject.

fn as_any_slice(&self) -> &[AnyObject][src]

Returns self as a reference to a single-element slice.

fn raw(self) -> usize[src]

Returns the raw object pointer.

unsafe fn as_unchecked<O: Object>(&self) -> &O[src]

Casts self to O without checking whether it is one.

unsafe fn into_unchecked<O: Object>(self) -> O[src]

Converts self to O without checking whether it is one.

fn id(self) -> u64[src]

Returns the object's identifier.

fn class(self) -> Class[src]

Returns the Class for self.

fn singleton_class(self) -> Class[src]

Returns the singleton Class of self, creating one if it doesn't exist already. Read more

fn mark(self)[src]

Marks self for Ruby to avoid garbage collecting it.

unsafe fn force_recycle(self)[src]

Forces the garbage collector to free the contents of self. Read more

fn def_singleton_method<N, F>(self, name: N, f: F) -> Result where
    N: Into<SymbolId>,
    F: MethodFn
[src]

Defines a method for name on the singleton class of self that calls f when invoked. Read more

unsafe fn def_singleton_method_unchecked<N, F>(self, name: N, f: F) where
    N: Into<SymbolId>,
    F: MethodFn
[src]

Defines a method for name on the singleton class of self that calls f when invoked. Read more

fn call(self, method: impl Into<SymbolId>) -> Result<AnyObject>[src]

Calls method on self and returns the result.

unsafe fn call_unchecked(self, method: impl Into<SymbolId>) -> AnyObject[src]

Calls method on self and returns the result. Read more

fn call_with(
    self,
    method: impl Into<SymbolId>,
    args: &[impl Object]
) -> Result<AnyObject>
[src]

Calls method on self with args and returns the result.

unsafe fn call_with_unchecked(
    self,
    method: impl Into<SymbolId>,
    args: &[impl Object]
) -> AnyObject
[src]

Calls method on self with args and returns the result. Read more

fn call_public(self, method: impl Into<SymbolId>) -> Result<AnyObject>[src]

Calls the public method on self and returns the result.

unsafe fn call_public_unchecked(self, method: impl Into<SymbolId>) -> AnyObject[src]

Calls the public method on self and returns the result. Read more

fn call_public_with(
    self,
    method: impl Into<SymbolId>,
    args: &[impl Object]
) -> Result<AnyObject>
[src]

Calls the public method on self with args and returns the result.

unsafe fn call_public_with_unchecked(
    self,
    method: impl Into<SymbolId>,
    args: &[impl Object]
) -> AnyObject
[src]

Calls the public method on self with args and returns the result. Read more

fn inspect(self) -> String[src]

Returns a printable string representation of self. Read more

fn to_s(self) -> String[src]

Returns the result of calling the to_s method on self.

fn is_frozen(self) -> bool[src]

Returns whether modifications can be made to self.

fn freeze(self)[src]

Freezes self, preventing any further mutations.

fn is_eql<O: Object>(self, other: &O) -> bool[src]

Returns whether self is equal to other in terms of the eql? method. Read more

impl<K: Object, V: Object> AsRef<AnyObject> for Hash<K, V>[src]

impl<K, V> Clone for Hash<K, V>[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<K: Object, V: Object> From<Hash<K, V>> for AnyObject[src]

impl<K: Object, V: Object, '_> From<&'_ [(K, V)]> for Hash<K, V>[src]

impl<K, V> Copy for Hash<K, V>[src]

impl<K: Object, V: Object> PartialEq<AnyObject> for Hash<K, V>[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl<K: Object, V: Object> Debug for Hash<K, V>[src]

impl<K: Object, V: Object> Display for Hash<K, V>[src]

impl<K1, K2, V1, V2> FromIterator<(K2, V2)> for Hash<K1, V1> where
    K1: Object,
    K2: Into<K1>,
    V1: Object,
    V2: Into<V1>, 
[src]

Auto Trait Implementations

impl<K = AnyObject, V = AnyObject> !Send for Hash<K, V>

impl<K = AnyObject, V = AnyObject> !Sync for Hash<K, V>

Blanket Implementations

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]