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>
impl<O: Object> Array<O>
Sourcepub fn from_slice<'s, T>(slice: &'s [T]) -> Self
pub fn from_slice<'s, T>(slice: &'s [T]) -> Self
Creates a new instance from the elements in slice.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new instance with capacity amount of storage.
Sourcepub fn len(self) -> usize
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);Sourcepub fn as_ptr_mut(self) -> *mut O
pub fn as_ptr_mut(self) -> *mut O
Returns a mutable pointer to the first object in self.
Sourcepub unsafe fn as_slice(&self) -> &[O]
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.
Sourcepub unsafe fn as_slice_mut(&mut self) -> &mut [O]
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.
Sourcepub fn get<I: ArrayIndex<O>>(self, index: I) -> Option<I::Output>
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.
Sourcepub unsafe fn get_unchecked<I: ArrayIndex<O>>(self, index: I) -> I::Output
pub unsafe fn get_unchecked<I: ArrayIndex<O>>(self, index: I) -> I::Output
Returns the output at index without safety checks on index.
Sourcepub fn subseq(self, range: Range<usize>) -> Option<Self>
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.
Sourcepub unsafe fn clear(self)
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.
Sourcepub unsafe fn extend_from_slice(self, slice: &[O])
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.
Sourcepub unsafe fn push(self, obj: O) -> AnyObject
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
FrozenErrorexception will be raised Array<AnyObject>that referencesArray<ConcreteObject>whereobjis not the same type asConcreteObject
Sourcepub fn contains(self, obj: impl Into<O>) -> bool
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"));Sourcepub unsafe fn remove_all(self, obj: impl Into<O>) -> Option<O>
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.
Sourcepub unsafe fn reverse(self)
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.
Trait Implementations§
Source§impl<O: Object, A: Into<O>> FromIterator<A> for Array<O>
impl<O: Object, A: Into<O>> FromIterator<A> for Array<O>
Source§fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self
Source§impl<O: Object> IntoIterator for Array<O>
impl<O: Object> IntoIterator for Array<O>
Source§impl<O: Object> Object for Array<O>
impl<O: Object> Object for Array<O>
Source§fn unique_id() -> Option<u128>
fn unique_id() -> Option<u128>
Source§fn cast<A: Object>(obj: A) -> Option<Self>
fn cast<A: Object>(obj: A) -> Option<Self>
obj. Read moreSource§unsafe fn from_raw(raw: usize) -> Self
unsafe fn from_raw(raw: usize) -> Self
raw without checking. Read moreSource§unsafe fn cast_unchecked(obj: impl Object) -> Self
unsafe fn cast_unchecked(obj: impl Object) -> Self
obj to Self without checking its type.Source§fn into_any_object(self) -> AnyObject
fn into_any_object(self) -> AnyObject
self as an AnyObject.Source§fn as_any_object(&self) -> &AnyObject
fn as_any_object(&self) -> &AnyObject
self as an AnyObject.Source§fn as_any_slice(&self) -> &[AnyObject]
fn as_any_slice(&self) -> &[AnyObject]
self as a reference to a single-element slice.Source§unsafe fn as_unchecked<O: Object>(&self) -> &O
unsafe fn as_unchecked<O: Object>(&self) -> &O
self to O without checking whether it is one.Source§unsafe fn into_unchecked<O: Object>(self) -> O
unsafe fn into_unchecked<O: Object>(self) -> O
self to O without checking whether it is one.Source§fn singleton_class(self) -> Class<Self>
fn singleton_class(self) -> Class<Self>
Source§unsafe fn force_recycle(self)
unsafe fn force_recycle(self)
self. Read moreSource§fn def_singleton_method<N, F>(self, name: N, f: F) -> Result
fn def_singleton_method<N, F>(self, name: N, f: F) -> Result
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)
unsafe fn def_singleton_method_unchecked<N, F>(self, name: N, f: F)
Source§unsafe fn call_with_protected(
self,
method: impl Into<SymbolId>,
args: &[impl Object],
) -> Result<AnyObject>
unsafe fn call_with_protected( self, method: impl Into<SymbolId>, args: &[impl Object], ) -> Result<AnyObject>
Source§unsafe fn call_public_with(
self,
method: impl Into<SymbolId>,
args: &[impl Object],
) -> AnyObject
unsafe fn call_public_with( self, method: impl Into<SymbolId>, args: &[impl Object], ) -> AnyObject
Source§unsafe fn call_public_with_protected(
self,
method: impl Into<SymbolId>,
args: &[impl Object],
) -> Result<AnyObject>
unsafe fn call_public_with_protected( self, method: impl Into<SymbolId>, args: &[impl Object], ) -> Result<AnyObject>
method on self with args and returns its output,
or an exception if one is raised. Read moreSource§fn is_eql<O: Object>(self, other: &O) -> bool
fn is_eql<O: Object>(self, other: &O) -> bool
self is equal to other in terms of the eql?
method.Source§fn get_attr<N: Into<SymbolId>>(self, name: N) -> AnyObject
fn get_attr<N: Into<SymbolId>>(self, name: N) -> AnyObject
self associated with name.