[][src]Struct rosy::String

#[repr(transparent)]
pub struct String(_);

An instance of Ruby's String class.

Methods

impl String[src]

pub fn new() -> Self[src]

Creates a new empty string with a capacity of 0.

pub fn with_capacity(capacity: usize) -> Self[src]

Creates a new string with capacity.

pub unsafe fn with_encoding(s: impl AsRef<[u8]>, enc: Encoding) -> Self[src]

Returns a new instance from s encoded as enc.

Safety

Care must be taken to ensure that the bytes are actually encoded this way. Otherwise, Ruby may make incorrect assumptions about the underlying data.

pub fn duplicate(self) -> Self[src]

Duplicates the contents of self into a new instance.

pub fn encoding(self) -> Encoding[src]

Returns how the bytes of self are encoded.

Examples

let string = rosy::String::from("¡Hola!");
assert!(string.encoding().is_utf8());

pub unsafe fn force_encoding(self, encoding: Encoding)[src]

Associates the bytes of self with encoding without checking whether self is actually encoded that way.

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

A fast shortcut to self.encoding().is_ascii_8bit().

Examples

let bytes: &[u8] = &[0, 1, 255, 42];
let string = rosy::String::from(bytes);
assert!(string.encoding_is_ascii_8bit());

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

A fast shortcut to self.encoding().is_utf8().

Examples

let string = rosy::String::from("hellooo");
assert!(string.encoding_is_utf8());

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

A fast shortcut to self.encoding().is_us_ascii().

Examples

use rosy::{Object, String};

let string = String::from("hellooo");
unsafe {
    string.call_with("encode!", &[String::from("US-ASCII")]);
}

assert!(string.encoding_is_us_ascii());

pub unsafe fn as_bytes(&self) -> &[u8][src]

Returns a reference to the underlying bytes in self.

Safety

Care must be taken to ensure that the length of self and the bytes pointed to by self are not changed through the VM or otherwise.

Examples

let rs = "Hey, I just met you, and this is crazy;
          but here's my number, so call me maybe.";
let rb = rosy::String::from(rs);

unsafe { assert_eq!(rs.as_bytes(), rb.as_bytes()) };

pub fn to_bytes(self) -> Vec<u8>[src]

Returns a buffer of the underlying bytes in self.

pub fn bytes_all<F>(self, f: F) -> bool where
    F: FnMut(u8) -> bool
[src]

Returns whether all bytes in self satisfy f.

pub fn bytes_any<F>(self, f: F) -> bool where
    F: FnMut(u8) -> bool
[src]

Returns whether any bytes in self satisfy f.

pub unsafe fn to_str(&self) -> Result<&str, Utf8Error>[src]

Returns a reference to the underlying UTF-8 encoded string in self.

Safety

Care must be taken to ensure that the length of self and the characters pointed to by self are not changed through the VM or otherwise.

If Ruby believes that the underlying encoding is indeed UTF-8, then we return the bytes directly without any further checking. However, if the method force_encoding has been called on self, then we are susceptible to getting invalid UTF-8 in a str instance, which is UB. To force a check, one should call str::from_utf8 on the result of as_bytes.

Examples

let rs = "Somebody once told me the world is gonna roll me...";
let rb = rosy::String::from(rs);

unsafe { assert_eq!(rb.to_str().unwrap(), rs) };

pub unsafe fn to_str_lossy(&self) -> Cow<str>[src]

Returns the underlying string lossy-encoded as UTF-8. See String::from_utf8_lossy for more details.

Safety

Care must be taken to ensure that, if the returned value is a reference to self, the length of self and the characters pointed to by self are not changed through the VM or otherwise.

If Ruby believes that the underlying encoding is indeed UTF-8, then we return the bytes directly without any further checking. However, if the method force_encoding has been called on self, then we are susceptible to getting invalid UTF-8 in a str instance, which is UB. To force a check, one should call str::from_utf8 on the result of as_bytes.

pub unsafe fn to_str_unchecked(&self) -> &str[src]

Returns a reference to the underlying bytes of self as if they were UTF-8 encoded.

Safety

Same reasons as to_str as well as that no UTF-8 checking is performed.

pub fn to_string(self) -> Result<String, Utf8Error>[src]

Returns a buffer of the underlying UTF-8 encoded string of self.

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

Returns the number of bytes in self.

Examples

let s1 = "Í'm in Rüby!";
let s2 = "I'm in Ruby!";
let s3 = rosy::String::from(s1);

assert_eq!(s3.len(), s1.len());
assert_ne!(s3.len(), s2.len());

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

Returns the number of characters in self.

Examples

This is a Unicode-aware method:

let s1 = "Í'm in Rüby!";
let s2 = "I'm in Ruby!";
let s3 = rosy::String::from(s1);

assert_eq!(s3.char_len(), s1.chars().count());
assert_eq!(s3.char_len(), s2.chars().count());

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

Returns whether self has no characters.

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

Returns whether self only contains whitespace.

Examples

When self is encoded as UTF-8, it checks against all unicode characters with the property "WSpace=Y":

use rosy::String;

let space = String::from("\u{0009}\u{000A}\u{000B}\u{000C}\u{000D}\
                          \u{0020}\u{0085}\u{00A0}\u{1680}\u{2000}\
                          \u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\
                          \u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\
                          \u{2028}\u{202F}\u{2029}\u{205F}\u{3000}");

assert!(space.is_whitespace());

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

Returns whether self only contains ASCII whitespace.

pub unsafe fn push(self, c: char)[src]

Concatenates c to self.

Safety

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

pub unsafe fn push_str(self, s: &str)[src]

Concatenates s to self.

Safety

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

pub fn ellipsized(self, len: usize) -> Self[src]

Returns the contents of self with an ellipsis (three dots) if it's longer than len characters.

Examples

let s1 = rosy::String::from("Hello, there!");
let s2 = s1.ellipsized(8);

assert_eq!(s2, "Hello...");

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

Returns whether the string is locked by the VM.

#[must_use]
pub fn with_lock<F, O>(self, f: F) -> Option<O> where
    F: FnOnce(Self) -> O, 
[src]

Attempts to call f if a lock on self can be acquired, returning its output on success.

Examples

let s = rosy::String::from("Hello!");
let result = s.with_lock(|s| s.is_locked());

assert_eq!(result, Some(true));

pub unsafe fn raw_lock(self)[src]

Locks the string, preventing others from writing to it.

Safety

The exception raised by the VM must be handled if the string is already locked.

pub unsafe fn raw_unlock(self)[src]

Unlocks the string, allowing others to write to it.

Safety

The exception raised by the VM must be handled if the string is already unlocked.

Trait Implementations

impl GcInfoKey for String[src]

impl Classify for String[src]

impl Object for String[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<Self>[src]

Returns the Class for self. Read more

fn singleton_class(self) -> Class<Self>[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<Self>, 
[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<Self>, 
[src]

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

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

Calls method on self and returns its output. Read more

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

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

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

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

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

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

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

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

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

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

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

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

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

Calls the public method on self with args and returns its output, or an exception if one is raised. 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

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

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

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

Evaluates args in the context of self. Read more

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

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

impl EvalArgs for String[src]

The script argument without any extra information.

impl Eq for String[src]

impl Ord for String[src]

fn max(self, other: Self) -> Self1.21.0[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self1.21.0[src]

Compares and returns the minimum of two values. Read more

fn clamp(self, min: Self, max: Self) -> Self[src]

🔬 This is a nightly-only experimental API. (clamp)

Restrict a value to a certain interval. Read more

impl<O: Object> PartialEq<O> for String[src]

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

This method tests for !=.

impl PartialEq<[u8]> for String[src]

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

This method tests for !=.

impl<'_> PartialEq<&'_ [u8]> for String[src]

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

This method tests for !=.

impl PartialEq<String> for [u8][src]

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

This method tests for !=.

impl<'_> PartialEq<String> for &'_ [u8][src]

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

This method tests for !=.

impl PartialEq<Vec<u8>> for String[src]

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

This method tests for !=.

impl<'_> PartialEq<&'_ Vec<u8>> for String[src]

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

This method tests for !=.

impl PartialEq<String> for Vec<u8>[src]

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

This method tests for !=.

impl<'_> PartialEq<String> for &'_ Vec<u8>[src]

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

This method tests for !=.

impl PartialEq<str> for String[src]

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

This method tests for !=.

impl<'_> PartialEq<&'_ str> for String[src]

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

This method tests for !=.

impl PartialEq<String> for str[src]

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

This method tests for !=.

impl<'_> PartialEq<String> for &'_ str[src]

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

This method tests for !=.

impl PartialEq<String> for String[src]

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

This method tests for !=.

impl<'_> PartialEq<&'_ String> for String[src]

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

This method tests for !=.

impl PartialEq<String> for String[src]

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

This method tests for !=.

impl<'_> PartialEq<String> for &'_ String[src]

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

This method tests for !=.

impl PartialEq<CStr> for String[src]

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

This method tests for !=.

impl<'_> PartialEq<&'_ CStr> for String[src]

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

This method tests for !=.

impl PartialEq<String> for CStr[src]

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

This method tests for !=.

impl<'_> PartialEq<String> for &'_ CStr[src]

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

This method tests for !=.

impl PartialEq<CString> for String[src]

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

This method tests for !=.

impl<'_> PartialEq<&'_ CString> for String[src]

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

This method tests for !=.

impl PartialEq<String> for CString[src]

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

This method tests for !=.

impl<'_> PartialEq<String> for &'_ CString[src]

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

This method tests for !=.

impl<S: ?Sized + Clone, '_> PartialEq<Cow<'_, S>> for String where
    String: PartialEq<S>, 
[src]

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

This method tests for !=.

impl Copy for String[src]

impl Clone for String[src]

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

Performs copy-assignment from source. Read more

impl PartialOrd<String> for String[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 AsRef<AnyObject> for String[src]

impl From<String> for AnyObject[src]

impl<'_> From<&'_ str> for String[src]

impl<'_> From<&'_ String> for String[src]

impl<'_> From<&'_ CStr> for String[src]

impl<'_> From<&'_ CString> for String[src]

impl<'_> From<&'_ [u8]> for String[src]

impl<'_> From<&'_ Vec<u8>> for String[src]

impl From<String> for Symbol[src]

impl From<String> for SymbolId[src]

impl Debug for String[src]

impl Display for String[src]

impl FromIterator<char> for String[src]

impl<'a> FromIterator<&'a char> for String[src]

impl<'a> FromIterator<&'a str> for String[src]

impl<'a> FromIterator<&'a String> for String[src]

impl TryFrom<String> for String[src]

type Error = Utf8Error

The type returned in the event of a conversion error.

Auto Trait Implementations

impl !Send for String

impl !Sync for String

Blanket Implementations

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

type Owned = T

The resulting type after obtaining ownership.

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

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

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

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]