[−][src]Struct rosy::Array
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); }
Methods
impl<O: Object> Array<O>
[src]
pub fn new() -> Self
[src]
Creates a new empty instance.
pub fn from_slice<'s, T>(slice: &'s [T]) -> Self where
&'s [T]: Into<Self>,
[src]
&'s [T]: Into<Self>,
Creates a new instance from the elements in slice
.
pub fn with_capacity(capacity: usize) -> Self
[src]
Creates a new instance with capacity
amount of storage.
pub fn len(self) -> usize
[src]
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);
pub fn is_empty(self) -> bool
[src]
Returns whether self
is empty.
pub fn as_ptr(self) -> *const O
[src]
Returns a pointer to the first object in self
.
pub fn as_ptr_mut(self) -> *mut O
[src]
Returns a mutable pointer to the first object in self
.
pub unsafe fn as_slice(&self) -> &[O]
[src]
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.
pub unsafe fn as_slice_mut(&mut self) -> &mut [O]
[src]
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.
pub fn get(self, index: usize) -> Option<O>
[src]
Returns the object at index
or None
if index
is out-of-bounds.
pub unsafe fn get_unchecked(self, index: usize) -> O
[src]
Returns the object at index
without bounds checking.
pub fn first(self) -> Option<O>
[src]
Returns the first object in self
.
pub fn last(self) -> Option<O>
[src]
Returns the last element in self
.
pub unsafe fn clear(self)
[src]
Removes all elements from self
.
Safety
The caller must ensure that self
is not frozen or else a FrozenError
exception will be raised.
pub unsafe fn extend_from_slice(self, slice: &[O])
[src]
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.
pub fn plus(self, other: Self) -> Self
[src]
Returns the result of performing self + other
.
pub unsafe fn push(self, obj: O) -> AnyObject
[src]
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 referencesArray<ConcreteObject>
whereobj
is not the same type asConcreteObject
pub unsafe fn pop(self) -> AnyObject
[src]
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()); }
pub fn contains(self, obj: impl Object) -> bool
[src]
Returns whether self
contains obj
.
This is equivalent to the include?
method.
Examples
use rosy::{Array, String}; let s = String::from("hi"); let a = Array::from_slice(&[String::from("yo"), s]); assert!(a.contains(s));
pub unsafe fn remove_all(self, obj: impl Object) -> AnyObject
[src]
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.
pub unsafe fn reverse(self)
[src]
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.
#[must_use]
pub fn sorted(self) -> Self
[src]
Returns an instance with its contents sorted.
pub unsafe fn sort(self)
[src]
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.
pub fn join(self, separator: impl Into<String>) -> String
[src]
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
impl<O: Object> Object for Array<O>
[src]
fn unique_id() -> Option<u128>
[src]
fn cast<A: Object>(obj: A) -> Option<Self>
[src]
fn ty(self) -> Ty
[src]
fn is_ty(self, ty: Ty) -> bool
[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]
N: Into<SymbolId>,
F: MethodFn,
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]
N: Into<SymbolId>,
F: MethodFn,
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]
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> Result<AnyObject>
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]
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> AnyObject
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]
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> Result<AnyObject>
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]
self,
method: impl Into<SymbolId>,
args: &[impl Object]
) -> AnyObject
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<O: Object> AsRef<AnyObject> for Array<O>
[src]
impl<O> Clone for Array<O>
[src]
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<O: Object> From<Array<O>> for AnyObject
[src]
impl<O: Object, '_> From<&'_ [O]> for Array<O>
[src]
impl<O> Copy for Array<O>
[src]
impl<T: Object, U: Object> PartialOrd<Array<U>> for Array<T>
[src]
fn partial_cmp(&self, other: &Array<U>) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<O: Object> PartialEq<AnyObject> for Array<O>
[src]
fn eq(&self, obj: &AnyObject) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<O, A> PartialEq<[A]> for Array<O> where
O: Object + PartialEq<A>,
[src]
O: Object + PartialEq<A>,
fn eq(&self, other: &[A]) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<T: Object, U: Object> PartialEq<Array<U>> for Array<T>
[src]
fn eq(&self, other: &Array<U>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<O: Object> IntoIterator for Array<O>
[src]
type Item = O
The type of the elements being iterated over.
type IntoIter = Iter<O>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
impl<O: Object> Debug for Array<O>
[src]
impl<O: Object> Display for Array<O>
[src]
impl<O: Object> Add<Array<O>> for Array<O>
[src]
type Output = Self
The resulting type after applying the +
operator.
fn add(self, other: Self) -> Self::Output
[src]
impl<O: Object, A: Into<O>> FromIterator<A> for Array<O>
[src]
fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<I> IntoIterator for I where
I: Iterator,
[src]
I: Iterator,
type Item = <I as Iterator>::Item
The type of the elements being iterated over.
type IntoIter = I
Which kind of iterator are we turning this into?
fn into_iter(self) -> I
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,