Struct Array

Source
pub struct Array<O = AnyObject> { /* private fields */ }
Expand description

An instance of Ruby’s Array class.

§Performance

Although caution must be taken with as_slice and its mutable counterpart, it is much faster to iterate over the inner slice of objects in an array than it is to iterate over the array directly.

§Examples

Ruby arrays can be treated as somewhat like a Vec without the borrow checker.

use rosy::prelude::*;

let s = String::from("hellooo");

let a = Array::from_slice(&[s, s, s]);
assert_eq!(a.len(), 3);

for obj in a {
    assert_eq!(obj, s);
}

Because the Iterator for Array performs a volatile read of the array length each time, a buffer overrun will never occur.

assert_eq!(a.len(), 3);
let mut num_iter = 0;

for _ in a {
    // `unsafe` required because `pop` raises an exception if `a` is frozen
    unsafe { a.pop() };
    num_iter += 1;
}

assert_eq!(num_iter, 2);

Just like Vec, one can even safely collect an iterator into an Array:

let array: Array = (0..10).collect();

for (i, obj) in array.into_iter().enumerate() {
    assert_eq!(obj, i);
}

Implementations§

Source§

impl<O: Object> Array<O>

Source

pub fn new() -> Self

Creates a new empty instance.

Source

pub fn from_slice<'s, T>(slice: &'s [T]) -> Self
where &'s [T]: Into<Self>,

Creates a new instance from the elements in slice.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new instance with capacity amount of storage.

Source

pub fn duplicate(self) -> Self

Duplicates the contents of self into a new instance.

Source

pub fn len(self) -> usize

Returns the number of elements in self.

§Examples
use rosy::{Array, String};

let s = String::from("hi");
let a = Array::from_slice(&[s, s, s]);

assert_eq!(a.len(), 3);
Source

pub fn is_empty(self) -> bool

Returns whether self is empty.

Source

pub fn as_ptr(self) -> *const O

Returns a pointer to the first object in self.

Source

pub fn as_ptr_mut(self) -> *mut O

Returns a mutable pointer to the first object in self.

Source

pub unsafe fn as_slice(&self) -> &[O]

Returns a slice to the underlying objects of self.

§Safety

Care must be taken to ensure that the length of self is not changed through the VM or otherwise.

Source

pub unsafe fn as_slice_mut(&mut self) -> &mut [O]

Returns a mutable slice to the underlying objects of self.

§Safety

The caller must ensure that self is not frozen or else a FrozenError exception will be raised. Care must also be taken to ensure that the length of self is not changed through the VM or otherwise.

Source

pub fn get<I: ArrayIndex<O>>(self, index: I) -> Option<I::Output>

Returns the output at index or None if index is out-of-bounds.

Source

pub unsafe fn get_unchecked<I: ArrayIndex<O>>(self, index: I) -> I::Output

Returns the output at index without safety checks on index.

Source

pub fn subseq(self, range: Range<usize>) -> Option<Self>

Returns the subsequence of self at range.

Note: This respects the semantics of rb_ary_subseq, where indexing out of the array with a range greater than the actual slice in self will just return all remaining elements. Use get for slice indexing semantics.

Source

pub fn first(self) -> Option<O>

Returns the first object in self.

Source

pub fn last(self) -> Option<O>

Returns the last element in self.

Source

pub unsafe fn clear(self)

Removes all elements from self.

§Safety

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

Source

pub unsafe fn extend_from_slice(self, slice: &[O])

Appends all of the elements in slice to self.

§Safety

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

Source

pub fn plus(self, other: Self) -> Self

Returns the result of performing self + other.

Source

pub unsafe fn push(self, obj: O) -> AnyObject

Pushes obj onto the end of self.

§Safety

The caller must ensure that self is not:

  • Frozen, or else a FrozenError exception will be raised
  • Array<AnyObject> that references Array<ConcreteObject> where obj is not the same type as ConcreteObject
Source

pub unsafe fn pop(self) -> AnyObject

Pops the last element from self.

§Safety

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

§Examples
use rosy::{Array, String};

let s = String::from("Hi");
let a = Array::from_slice(&[s]);

unsafe {
    assert!(!a.pop().is_nil());
    assert!(a.pop().is_nil());
}
Source

pub fn contains(self, obj: impl Into<O>) -> bool

Returns whether self contains obj.

This is equivalent to the include? method.

§Examples
use rosy::{Array, String};

let array = Array::from_slice(&[
    String::from("yo"),
    String::from("hi"),
]);

assert!(array.contains("hi"));
Source

pub unsafe fn remove_all(self, obj: impl Into<O>) -> Option<O>

Removes all items in self that are equal to obj.

This is equivalent to the delete method.

§Safety

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

Source

pub unsafe fn reverse(self)

Reverses the contents of self in-palace.

This is equivalent to the reverse! method.

§Safety

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

Source

pub fn sorted(self) -> Self

Returns an instance with its contents sorted.

Source

pub unsafe fn sort(self)

Sorts the contents of self in-place without checking whether self is frozen.

§Safety

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

Source

pub fn join(self, separator: impl Into<String>) -> String

Joins the contents of self with separator.

§Examples
use rosy::{Array, String};

let s = String::from("-");
let a = Array::from_slice(&[s, s, s]);

assert_eq!(a.join("."), "-.-.-");

Trait Implementations§

Source§

impl<O: Object> Add for Array<O>

Source§

type Output = Array<O>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<O: Object> AsRef<AnyObject> for Array<O>

Source§

fn as_ref(&self) -> &AnyObject

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<O: Object> Classify for Array<O>

Source§

fn class() -> Class<Self>

Returns the typed class that can be used to get an instance of self.
Source§

impl<O> Clone for Array<O>

Source§

fn clone(&self) -> Self

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<O: Object> Debug for Array<O>

Source§

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

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

impl<O: Object> Display for Array<O>

Source§

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

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

impl<O: Object> From<&[O]> for Array<O>

Source§

fn from(slice: &[O]) -> Self

Converts to this type from the input type.
Source§

impl<O: Object> From<Array<O>> for AnyObject

Source§

fn from(object: Array<O>) -> AnyObject

Converts to this type from the input type.
Source§

impl<O: Object, A: Into<O>> FromIterator<A> for Array<O>

Source§

fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<O: Object> IntoIterator for Array<O>

Source§

type Item = O

The type of the elements being iterated over.
Source§

type IntoIter = Iter<O>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<O: Object> Object for Array<O>

Source§

fn unique_id() -> Option<u128>

Returns a unique identifier for an object type to facilitate casting. Read more
Source§

fn cast<A: Object>(obj: A) -> Option<Self>

Attempts to create an instance by casting obj. Read more
Source§

fn ty(self) -> Ty

Returns the virtual type of self.
Source§

fn is_ty(self, ty: Ty) -> bool

Returns whether the virtual type of self is ty.
Source§

unsafe fn from_raw(raw: usize) -> Self

Creates a new object from raw without checking. Read more
Source§

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

Casts obj to Self without checking its type.
Source§

fn into_any_object(self) -> AnyObject

Returns self as an AnyObject.
Source§

fn as_any_object(&self) -> &AnyObject

Returns a reference to self as an AnyObject.
Source§

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

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

fn raw(self) -> usize

Returns the raw object pointer.
Source§

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

Casts self to O without checking whether it is one.
Source§

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

Converts self to O without checking whether it is one.
Source§

fn id(self) -> u64

Returns the object’s identifier.
Source§

fn class(self) -> Class<Self>

Returns the Class for self. Read more
Source§

fn singleton_class(self) -> Class<Self>

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

fn mark(self)

Marks self for Ruby to avoid garbage collecting it.
Source§

unsafe fn force_recycle(self)

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

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

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

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

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

unsafe fn call(self, method: impl Into<SymbolId>) -> AnyObject

Calls method on self and returns its output. Read more
Source§

unsafe fn call_protected(self, method: impl Into<SymbolId>) -> Result<AnyObject>

Calls method on self and returns its output, or an exception if one is raised. Read more
Source§

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

Calls method on self with args and returns its output. Read more
Source§

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

Calls method on self with args and returns its output, or an exception if one is raised. Read more
Source§

unsafe fn call_public(self, method: impl Into<SymbolId>) -> AnyObject

Calls the public method on self and returns its output. Read more
Source§

unsafe fn call_public_protected( self, method: impl Into<SymbolId>, ) -> Result<AnyObject>

Calls the public method on self and returns its output, or an exception if one is raised. Read more
Source§

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

Calls the public method on self with args and returns its output. Read more
Source§

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

Calls the public method on self with args and returns its output, or an exception if one is raised. Read more
Source§

fn inspect(self) -> String

Returns a printable string representation of self. Read more
Source§

fn to_s(self) -> String

Returns the result of calling the to_s method on self.
Source§

fn is_frozen(self) -> bool

Returns whether modifications can be made to self.
Source§

fn freeze(self)

Freezes self, preventing any further mutations.
Source§

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

Returns whether self is equal to other in terms of the eql? method.
Source§

fn get_attr<N: Into<SymbolId>>(self, name: N) -> AnyObject

Returns the value for the attribute of self associated with name.
Source§

unsafe fn eval(self, args: impl EvalArgs) -> AnyObject

Evaluates args in the context of self. Read more
Source§

unsafe fn eval_protected(self, args: impl EvalArgs) -> Result<AnyObject>

Evaluates args in the context of self, returning any raised exceptions. Read more
Source§

impl<O, A> PartialEq<[A]> for Array<O>
where O: Object + PartialEq<A>,

Source§

fn eq(&self, other: &[A]) -> 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<O: Object> PartialEq<AnyObject> for Array<O>

Source§

fn eq(&self, obj: &AnyObject) -> 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: Object, U: Object> PartialEq<Array<U>> for Array<T>

Source§

fn eq(&self, other: &Array<U>) -> 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<O, A> PartialEq<Vec<A>> for Array<O>
where O: Object + PartialEq<A>,

Source§

fn eq(&self, other: &Vec<A>) -> 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: Object, U: Object> PartialOrd<Array<U>> for Array<T>

Source§

fn partial_cmp(&self, other: &Array<U>) -> 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<O> Copy for Array<O>

Auto Trait Implementations§

§

impl<O> Freeze for Array<O>

§

impl<O> RefUnwindSafe for Array<O>
where O: RefUnwindSafe,

§

impl<O = AnyObject> !Send for Array<O>

§

impl<O = AnyObject> !Sync for Array<O>

§

impl<O> Unpin for Array<O>

§

impl<O> UnwindSafe for Array<O>
where O: RefUnwindSafe,

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

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

Source§

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

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.