Struct magnus::r_string::RString

source ·
pub struct RString(/* private fields */);
Expand description

A Value pointer to a RString struct, Ruby’s internal representation of strings.

See the ReprValue and Object traits for additional methods available on this type. See Ruby for methods to create an RString.

Implementations§

source§

impl RString

source

pub fn from_value(val: Value) -> Option<Self>

Return Some(RString) if val is a RString, None otherwise.

§Examples
use magnus::{eval, RString};

assert!(RString::from_value(eval(r#""example""#).unwrap()).is_some());
assert!(RString::from_value(eval(":example").unwrap()).is_none());
source

pub fn new(s: &str) -> Self

Create a new Ruby string from the Rust string s.

The encoding of the Ruby string will be UTF-8.

§Panics

Panics if called from a non-Ruby thread. See Ruby::str_new for the non-panicking version.

§Examples
use magnus::{rb_assert, RString};

let val = RString::new("example");
rb_assert!(r#"val == "example""#, val);
source

pub fn buf_new(n: usize) -> Self

Create a new Ruby string with capacity n.

The encoding will be set to ASCII-8BIT (aka BINARY). See also with_capacity.

§Panics

Panics if called from a non-Ruby thread. See Ruby::str_buf_new for the non-panicking version.

§Examples
use magnus::{rb_assert, RString};

let buf = RString::buf_new(4096);
buf.cat(&[13, 14, 10, 13, 11, 14, 14, 15]);
rb_assert!(r#"buf == "\r\x0E\n\r\v\x0E\x0E\x0F""#, buf);
source

pub fn with_capacity(n: usize) -> Self

Create a new Ruby string with capacity n.

The encoding will be set to UTF-8. See also buf_new.

§Panics

Panics if called from a non-Ruby thread. See Ruby::str_with_capacity for the non-panicking version.

§Examples
use magnus::{rb_assert, RString};

let s = RString::with_capacity(9);
s.cat("foo");
s.cat("bar");
s.cat("baz");
rb_assert!(r#"s == "foobarbaz""#, s);
source

pub fn from_slice(s: &[u8]) -> Self

Create a new Ruby string from the Rust slice s.

The encoding of the Ruby string will be set to ASCII-8BIT (aka BINARY).

§Panics

Panics if called from a non-Ruby thread. See Ruby::str_from_slice for the non-panicking version.

§Examples
use magnus::{rb_assert, RString};

let buf = RString::from_slice(&[13, 14, 10, 13, 11, 14, 14, 15]);
rb_assert!(r#"buf == "\r\x0E\n\r\v\x0E\x0E\x0F""#, buf);
source

pub fn enc_new<T, E>(s: T, enc: E) -> Self
where T: AsRef<[u8]>, E: Into<RbEncoding>,

Create a new Ruby string from the value s with the encoding enc.

§Panics

Panics if called from a non-Ruby thread. See Ruby::enc_str_new for the non-panicking version.

§Examples
use magnus::{encoding::RbEncoding, rb_assert, RString};

let val = RString::enc_new("example", RbEncoding::usascii());
rb_assert!(r#"val == "example""#, val);
use magnus::{encoding::RbEncoding, rb_assert, RString};

let val = RString::enc_new([255, 128, 128], RbEncoding::ascii8bit());
rb_assert!(r#"val == "\xFF\x80\x80".force_encoding("BINARY")"#, val);
source

pub fn from_char(c: char) -> Self

Create a new Ruby string from the Rust char c.

The encoding of the Ruby string will be UTF-8.

§Panics

Panics if called from a non-Ruby thread. See Ruby::str_from_char for the non-panicking version.

§Examples
use magnus::{rb_assert, RString};

let c = RString::from_char('a');
rb_assert!(r#"c == "a""#, c);
use magnus::{rb_assert, RString};

let c = RString::from_char('🦀');
rb_assert!(r#"c == "🦀""#, c);
source

pub fn chr<T>(code: u32, enc: T) -> Result<Self, Error>
where T: Into<RbEncoding>,

Create a new Ruby string containing the codepoint code in the encoding enc.

The encoding of the Ruby string will be the passed encoding enc.

§Panics

Panics if called from a non-Ruby thread. See Ruby::chr for the non-panicking version.

§Examples
use magnus::{encoding::RbEncoding, rb_assert, RString};

let c = RString::chr(97, RbEncoding::usascii()).unwrap();
rb_assert!(r#"c == "a""#, c);
use magnus::{encoding::RbEncoding, rb_assert, RString};

let c = RString::chr(129408, RbEncoding::utf8()).unwrap();
rb_assert!(r#"c == "🦀""#, c);
source

pub fn new_shared(s: Self) -> Self

Create a new Ruby string that shares the same backing data as s.

Both string objects will point at the same underlying data until one is modified, and only then will the data be duplicated. This operation is cheep, and useful for cases where you may need to modify a string, but don’t want to mutate a value passed to your function.

§Examples
use magnus::{rb_assert, RString};

let s = RString::new("example");
let dup = RString::new_shared(s);
rb_assert!("s == dup", s, dup);
// mutating one doesn't mutate both
dup.cat("foo");
rb_assert!("s != dup", s, dup);
source

pub fn new_frozen(s: Self) -> Self

Create a new Ruby string that is a frozen copy of s.

This can be used to get a copy of a string that is guranteed not to be modified while you are referencing it.

§Examples
use magnus::{rb_assert, RString};

let orig = RString::new("example");
let frozen = RString::new_frozen(orig);
rb_assert!(r#"frozen == "example""#, frozen);
// mutating original doesn't impact the frozen copy
orig.cat("foo");
rb_assert!(r#"frozen == "example""#, frozen);
source

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

Return self as a slice of bytes.

§Safety

This is directly viewing memory owned and managed by Ruby. Ruby may modify or free the memory backing the returned slice, the caller must ensure this does not happen.

Ruby must not be allowed to garbage collect or modify self while a refrence to the slice is held.

§Examples
use magnus::RString;

let s = RString::new("example");
// safe as we don't give Ruby the chance to mess with the string while
// we hold a refrence to the slice.
unsafe { assert_eq!(s.as_slice(), [101, 120, 97, 109, 112, 108, 101]) };
source

pub unsafe fn codepoints(&self) -> Codepoints<'_>

Return an iterator over self’s codepoints.

§Safety

The returned iterator references memory owned and managed by Ruby. Ruby may modify or free that memory, the caller must ensure this does not happen at any time while still holding a reference to the iterator.

§Examples
use magnus::{Error, RString};

let s = RString::new("🦀 café");

let codepoints = unsafe {
    // ensure string isn't mutated during iteration by creating a
    // frozen copy and iterating over that
    let f = RString::new_frozen(s);
    f.codepoints().collect::<Result<Vec<_>, Error>>().unwrap()
};

assert_eq!(codepoints, [129408, 32, 99, 97, 102, 233]);
source

pub unsafe fn char_bytes(&self) -> CharBytes<'_>

Return an iterator over self’s chars as slices of bytes.

§Safety

The returned iterator references memory owned and managed by Ruby. Ruby may modify or free that memory, the caller must ensure this does not happen at any time while still holding a reference to the iterator.

§Examples
use magnus::RString;

let s = RString::new("🦀 café");

// ensure string isn't mutated during iteration by creating a frozen
// copy and iterating over that
let f = RString::new_frozen(s);
let codepoints = unsafe { f.char_bytes().collect::<Vec<_>>() };

assert_eq!(
    codepoints,
    [
        &[240, 159, 166, 128][..],
        &[32],
        &[99],
        &[97],
        &[102],
        &[195, 169]
    ]
);
source

pub fn offset(self, pos: usize) -> usize

Converts a character offset to a byte offset.

§Examples
use magnus::RString;

let s = RString::new("🌊🦀🏝️");
assert_eq!(s.offset(1), 4);
assert_eq!(s.offset(2), 8);
source

pub fn is_utf8_compatible_encoding(self) -> bool

Returns true if the encoding for this string is UTF-8 or US-ASCII, false otherwise.

The encoding on a Ruby String is just a label, it provides no guarantee that the String really is valid UTF-8.

§Examples
use magnus::{eval, RString};

let s: RString = eval!(r#""café""#).unwrap();
assert!(s.is_utf8_compatible_encoding());
use magnus::{eval, RString};

let s: RString = eval!(r#""café".encode("ISO-8859-1")"#).unwrap();
assert!(!s.is_utf8_compatible_encoding());
source

pub fn conv_enc<T>(self, enc: T) -> Result<Self, Error>
where T: Into<RbEncoding>,

Returns a new string by reencoding self from its current encoding to the given enc.

§Examples
use magnus::{encoding::RbEncoding, eval, RString};

let s: RString = eval!(r#""café".encode("ISO-8859-1")"#).unwrap();
// safe as we don't give Ruby the chance to mess with the string while
// we hold a refrence to the slice.
unsafe { assert_eq!(s.as_slice(), &[99, 97, 102, 233]) };
let e = s.conv_enc(RbEncoding::utf8()).unwrap();
unsafe { assert_eq!(e.as_slice(), &[99, 97, 102, 195, 169]) };
source

pub fn scrub(self, replacement: Option<Self>) -> Result<Option<Self>, Error>

Returns a string omitting ‘broken’ parts of the string according to its encoding.

If replacement is Some(RString) and ‘broken’ portion will be replaced with that string. When replacement is None an encoding specific default will be used.

If self is not ‘broken’ and no replacement was made, returns Ok(None).

§Examples
use magnus::{encoding::RbEncoding, RString};

// 156 is invalid for utf-8
let s = RString::enc_new([156, 57, 57], RbEncoding::utf8());
assert_eq!(s.scrub(None).unwrap().unwrap().to_string().unwrap(), "�99");
assert_eq!(s.scrub(Some(RString::new("?"))).unwrap().unwrap().to_string().unwrap(), "?99");
assert_eq!(s.scrub(Some(RString::new(""))).unwrap().unwrap().to_string().unwrap(), "99");
source

pub fn enc_coderange(self) -> Coderange

Returns the cached coderange value that describes how self relates to its encoding.

See also enc_coderange_scan.

§Examples
use magnus::{encoding::Coderange, prelude::*, RString};

// Coderange is unknown on creation.
let s = RString::new("test");
assert_eq!(s.enc_coderange(), Coderange::Unknown);

// Methods that operate on the string using the encoding will set the
// coderange as a side effect.
let _: usize = s.funcall("length", ()).unwrap();
assert_eq!(s.enc_coderange(), Coderange::SevenBit);

// Operations with two strings with known coderanges will set it
// appropriately.
let t = RString::new("🦀");
let _: usize = t.funcall("length", ()).unwrap();
assert_eq!(t.enc_coderange(), Coderange::Valid);
s.buf_append(t).unwrap();
assert_eq!(s.enc_coderange(), Coderange::Valid);

// Operations that modify the string with an unknown coderange will
// set the coderange back to unknown.
s.cat([128]);
assert_eq!(s.enc_coderange(), Coderange::Unknown);

// Which may leave the string with a broken encoding.
let _: usize = s.funcall("length", ()).unwrap();
assert_eq!(s.enc_coderange(), Coderange::Broken);
source

pub fn enc_coderange_scan(self) -> Coderange

Scans self to establish its coderange.

If the coderange is already known, simply returns the known value. See also enc_coderange.

§Examples
use magnus::{encoding::Coderange, RString};

let s = RString::new("test");
assert_eq!(s.enc_coderange_scan(), Coderange::SevenBit);
source

pub fn enc_coderange_clear(self)

Clear self’s cached coderange, setting it to Unknown.

§Examples
use magnus::{encoding::Coderange, prelude::*, RString};

let s = RString::new("🦀");
// trigger setting coderange
let _: usize = s.funcall("length", ()).unwrap();
assert_eq!(s.enc_coderange(), Coderange::Valid);

s.enc_coderange_clear();
assert_eq!(s.enc_coderange(), Coderange::Unknown);
source

pub unsafe fn enc_coderange_set(self, cr: Coderange)

Sets self’s cached coderange.

Rather than using the method it is recommended to set the coderange to Unknown with enc_coderange_clear and let Ruby determine the coderange lazily when needed.

§Safety

This must be set correctly. SevenBit if all codepoints are within 0..=127, Valid if the string is valid for its encoding, or Broken if it is not. Unknown can be set safely with enc_coderange_clear.

§Examples
use magnus::{
    encoding::{self, Coderange},
    exception,
    prelude::*,
    Error, RString,
};

fn crabbify(s: RString) -> Result<(), Error> {
    if s.enc_get() != encoding::Index::utf8() {
        return Err(Error::new(exception::encoding_error(), "expected utf-8"));
    }
    let original = s.enc_coderange();
    // ::cat() will clear the coderange
    s.cat("🦀");
    // we added a multibyte char, so if we started with `SevenBit` it
    // should be upgraded to `Valid`, and if it was `Valid` it is still
    // `Valid`.
    if original == Coderange::SevenBit || original == Coderange::Valid {
        unsafe {
            s.enc_coderange_set(Coderange::Valid);
        }
    }
    Ok(())
}

let s = RString::new("test");
// trigger setting coderange
let _: usize = s.funcall("length", ()).unwrap();

crabbify(s).unwrap();
assert_eq!(s.enc_coderange(), Coderange::Valid);
source

pub unsafe fn test_as_str(&self) -> Option<&str>

Returns a Rust &str reference to the value of self.

Returns None if self’s encoding is not UTF-8 (or US-ASCII), or if the string is not valid UTF-8.

§Safety

This is directly viewing memory owned and managed by Ruby. Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.

Ruby must not be allowed to garbage collect or modify self while a refrence to the str is held.

§Examples
use magnus::RString;

let s = RString::new("example");
// safe as we don't give Ruby the chance to mess with the string while
// we hold a refrence to the slice.
unsafe { assert_eq!(s.test_as_str().unwrap(), "example") };
source

pub unsafe fn as_str(&self) -> Result<&str, Error>

Returns a Rust &str reference to the value of self.

Errors if self’s encoding is not UTF-8 (or US-ASCII), or if the string is not valid UTF-8.

§Safety

This is directly viewing memory owned and managed by Ruby. Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.

Ruby must not be allowed to garbage collect or modify self while a refrence to the str is held.

§Examples
use magnus::RString;

let s = RString::new("example");
// safe as we don't give Ruby the chance to mess with the string while
// we hold a refrence to the slice.
unsafe { assert_eq!(s.as_str().unwrap(), "example") };
source

pub unsafe fn to_string_lossy(&self) -> Cow<'_, str>

Returns self as a Rust string, ignoring the Ruby encoding and dropping any non-UTF-8 characters. If self is valid UTF-8 this will return a &str reference.

§Safety

This may return a direct view of memory owned and managed by Ruby. Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.

Ruby must not be allowed to garbage collect or modify self while a refrence to the str is held.

§Examples
use magnus::RString;

let s = RString::new("example");
// safe as we don't give Ruby the chance to mess with the string while
// we hold a refrence to the slice.
unsafe { assert_eq!(s.to_string_lossy(), "example") };
source

pub fn to_string(self) -> Result<String, Error>

Returns self as an owned Rust String. The Ruby string will be reencoded as UTF-8 if required. Errors if the string can not be encoded as UTF-8.

§Examples
use magnus::RString;

let s = RString::new("example");
assert_eq!(s.to_string().unwrap(), "example");
source

pub fn to_bytes(self) -> Bytes

Available on crate feature bytes only.

Returns self as an owned Rust Bytes.

§Examples
use bytes::Bytes;
use magnus::RString;

let s = RString::new("example");
assert_eq!(s.to_bytes(), Bytes::from("example"));
source

pub fn to_char(self) -> Result<char, Error>

Converts self to a char. Errors if the string is more than one character or can not be encoded as UTF-8.

§Examples
use magnus::RString;

let s = RString::new("a");
assert_eq!(s.to_char().unwrap(), 'a');
source

pub fn dump(self) -> Result<Self, Error>

Returns a quoted version of the self.

This can be thought of as the opposite of eval. A string returned from dump can be safely passed to eval and will result in a string with the exact same contents as the original.

§Examples
use magnus::RString;

let s = RString::new("🦀 café");
assert_eq!(
    s.dump().unwrap().to_string().unwrap(),
    r#""\u{1F980} caf\u00E9""#
);
source

pub fn is_interned(self) -> bool

Returns whether self is a frozen interned string. Interned strings are usually string literals with the in files with the # frozen_string_literal: true ‘magic comment’.

Interned strings won’t be garbage collected or modified, so should be safe to store on the heap or hold a &str refrence to. See as_interned_str.

§Examples
use magnus::{eval, RString};

let s: RString = eval!(
    r#"
      # frozen_string_literal: true

      "example"
    "#
)
.unwrap();
assert!(s.is_interned());
use magnus::{eval, RString};

let s: RString = eval!(r#""example""#).unwrap();
assert!(!s.is_interned());
source

pub fn as_interned_str(self) -> Option<FString>

Returns Some(FString) if self is interned, None otherwise.

Interned strings won’t be garbage collected or modified, so should be safe to store on the heap or hold a &str refrence to. The FString type returned by this function provides a way to encode this property into the type system, and provides safe methods to access the string as a &str or slice.

§Examples
use magnus::{eval, RString};

let s: RString = eval!(
    r#"
      # frozen_string_literal: true

      "example"
    "#
)
.unwrap();
assert!(s.as_interned_str().is_some());
use magnus::{eval, RString};

let s: RString = eval!(r#""example""#).unwrap();
assert!(s.as_interned_str().is_none());
source

pub fn to_interned_str(self) -> FString

Available on ruby_gte_3_0 only.

Interns self and returns a FString. Be aware that once interned a string will never be garbage collected.

§Examples
use magnus::RString;

let fstring = RString::new("example").to_interned_str();
assert_eq!(fstring.as_str().unwrap(), "example");
source

pub fn buf_append(self, other: Self) -> Result<(), Error>

Mutate self, adding other to the end. Errors if self and other’s encodings are not compatible.

§Examples
use magnus::RString;

let a = RString::new("foo");
let b = RString::new("bar");
a.buf_append(b).unwrap();
assert_eq!(a.to_string().unwrap(), "foobar");
source

pub fn cat<T: AsRef<[u8]>>(self, buf: T)

Mutate self, adding buf to the end.

Note: This ignore’s self’s encoding, and may result in self containing invalid bytes for its encoding. It’s assumed this will more often be used with ASCII-8BIT (aka BINARY) encoded strings. See buf_new and from_slice.

§Examples
use magnus::RString;

let buf = RString::buf_new(4096);
buf.cat(&[102, 111, 111]);
buf.cat("bar");
assert_eq!(buf.to_string().unwrap(), "foobar");
source

pub fn replace(self, other: Self) -> Result<(), Error>

Replace the contents and encoding of self with those of other.

§Examples
use magnus::RString;

let a = RString::new("foo");
let b = RString::new("bar");
a.replace(b).unwrap();
assert_eq!(a.to_string().unwrap(), "bar");
source

pub fn shared_replace(self, other: Self) -> Result<(), Error>

Modify self to share the same backing data as other.

Both string objects will point at the same underlying data until one is modified, and only then will the data be duplicated.

See also replace and new_shared.

§Examples
use magnus::RString;

let a = RString::new("foo");
let b = RString::new("bar");
a.shared_replace(b).unwrap();
assert_eq!(a.to_string().unwrap(), "bar");
// mutating one doesn't mutate both
b.cat("foo");
assert_eq!(a.to_string().unwrap(), "bar");
source

pub fn update(self, beg: isize, len: usize, other: Self) -> Result<(), Error>

Replace a portion of self with other.

beg is the offset of the portion of self to replace. Negative values offset from the end of the string. len is the length of the portion of self to replace. It does not need to match the length of other, self will be expanded or contracted as needed to accomdate other.

§Examples
use magnus::RString;

let s = RString::new("foo");
s.update(-1, 1, RString::new("x")).unwrap();
assert_eq!(s.to_string().unwrap(), "fox");

let s = RString::new("splat");
s.update(0, 3, RString::new("b")).unwrap();
assert_eq!(s.to_string().unwrap(), "bat");

let s = RString::new("corncob");
s.update(1, 5, RString::new("ra")).unwrap();
assert_eq!(s.to_string().unwrap(), "crab");
source

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

Create a new string by appending other to self.

§Examples
use magnus::RString;

let a = RString::new("foo");
let b = RString::new("bar");
assert_eq!(a.plus(b).unwrap().to_string().unwrap(), "foobar");
assert_eq!(a.to_string().unwrap(), "foo");
assert_eq!(b.to_string().unwrap(), "bar");
source

pub fn times(self, num: usize) -> Self

Create a new string by repeating self num times.

§Examples
use magnus::RString;

assert_eq!(
    RString::new("foo").times(3).to_string().unwrap(),
    "foofoofoo"
);
source

pub fn drop_bytes(self, len: usize) -> Result<(), Error>

Shrink self by len bytes.

§Examples
use magnus::RString;

let s = RString::new("foobar");
s.drop_bytes(3).unwrap();
assert_eq!(s.to_string().unwrap(), "bar");
source

pub fn len(self) -> usize

Returns the number of bytes in self.

See also length.

§Examples
use magnus::RString;

let s = RString::new("🦀 Hello, Ferris");
assert_eq!(s.len(), 18);
source

pub fn length(self) -> usize

Returns the number of characters in self.

See also len.

§Examples
use magnus::RString;

let s = RString::new("🦀 Hello, Ferris");
assert_eq!(s.length(), 15);
source

pub fn capacity(self) -> usize

Returns the capacity of self.

§Examples
use magnus::RString;

let s = RString::with_capacity(9);
s.cat("foo");
assert_eq!(3, s.len());
assert!(s.capacity() >= 9);
source

pub fn is_empty(self) -> bool

Return whether self contains any characters or not.

§Examples
use magnus::RString;

let s = RString::new("");
assert!(s.is_empty());
source

pub fn cmp(self, other: Self) -> Ordering

Compares self with other to establish an ordering.

§Examples
use std::cmp::Ordering;

use magnus::RString;

let a = RString::new("a");
let b = RString::new("b");
assert_eq!(Ordering::Less, a.cmp(b));

Note that std::cmp::Ordering can be cast to i{8,16,32,64,size} to get the Ruby standard -1/0/+1 for comparison results.

assert_eq!(std::cmp::Ordering::Less as i64, -1);
assert_eq!(std::cmp::Ordering::Equal as i64, 0);
assert_eq!(std::cmp::Ordering::Greater as i64, 1);
source

pub fn comparable(self, other: Self) -> bool

Returns whether there is a total order of strings in the encodings of self and other.

If this function returns true for self and other then the ordering returned from cmp for those strings is ‘correct’. If false, while stable, the ordering may not follow established rules.

source

pub fn ellipsize(self, len: usize) -> Self

Shorten self to len, adding “…”.

If self is shorter than len the returned value will be self.

§Examples
use magnus::RString;

let s = RString::new("foobarbaz");
assert_eq!(s.ellipsize(6).to_string().unwrap(), "foo...");
source

pub fn split(self, delim: &str) -> RArray

Split self around the given delimiter.

If delim is an empty string then self is split into characters. If delim is solely whitespace then self is split around whitespace, with leading, trailing, and runs of contiguous whitespace ignored. Otherwise, self is split around delim.

§Examples
use magnus::{prelude::*, RString};

let s = RString::new(" foo  bar  baz ");
assert_eq!(
    Vec::<String>::try_convert(s.split("").as_value()).unwrap(),
    vec![" ", "f", "o", "o", " ", " ", "b", "a", "r", " ", " ", "b", "a", "z", " "]
);
assert_eq!(
    Vec::<String>::try_convert(s.split(" ").as_value()).unwrap(),
    vec!["foo", "bar", "baz"]
);
assert_eq!(
    Vec::<String>::try_convert(s.split(" bar ").as_value()).unwrap(),
    vec![" foo ", " baz "]
);

Trait Implementations§

source§

impl Clone for RString

source§

fn clone(&self) -> RString

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 Debug for RString

source§

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

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

impl Display for RString

source§

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

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

impl EncodingCapable for RString

source§

fn enc_get(self) -> Index

Get the encoding of self. Read more
source§

fn enc_set<T>(self, enc: T) -> Result<(), Error>
where T: Into<Index>,

Set self’s encoding. Read more
source§

fn enc_associate<T>(self, enc: T) -> Result<(), Error>
where T: Into<Index>,

Set self’s encoding, along with performing additional fix-up self’s contents. Read more
source§

impl IntoRString for RString

source§

fn into_r_string_with(self, _: &Ruby) -> RString

Convert self into RString.
source§

fn into_r_string(self) -> RString

Convert self into RString. Read more
source§

unsafe fn into_r_string_unchecked(self) -> RString

Convert self into RString. Read more
source§

impl IntoValue for RString

source§

fn into_value_with(self, _: &Ruby) -> Value

Convert self into Value.
source§

fn into_value(self) -> Value

Convert self into Value. Read more
source§

unsafe fn into_value_unchecked(self) -> Value

Convert self into Value. Read more
source§

impl Object for RString

source§

fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>
where M: Method,

Define a singleton method in self’s scope. Read more
source§

fn ivar_get<T, U>(self, name: T) -> Result<U, Error>
where T: IntoId, U: TryConvert,

Get the value for the instance variable name within self’s scope. Read more
source§

fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>
where T: IntoId, U: IntoValue,

Set the value for the instance variable name within self’s scope. Read more
source§

fn singleton_class(self) -> Result<RClass, Error>

Finds or creates the singleton class of self. Read more
source§

fn extend_object(self, module: RModule) -> Result<(), Error>

Extend self with module. Read more
source§

impl ReprValue for RString

source§

fn as_value(self) -> Value

Return self as a Value.
source§

fn is_nil(self) -> bool

Returns whether self is Ruby’s nil value. Read more
source§

fn equal<T>(self, other: T) -> Result<bool, Error>
where T: ReprValue,

Checks for equality, delegating to the Ruby method #==. Read more
source§

fn eql<T>(self, other: T) -> Result<bool, Error>
where T: ReprValue,

Checks for equality, delegating to the Ruby method #eql?. Read more
source§

fn hash(self) -> Result<Integer, Error>

Returns an integer non-uniquely identifying self. Read more
source§

fn class(self) -> RClass

Returns the class that self is an instance of. Read more
source§

fn is_frozen(self) -> bool

Returns whether self is ‘frozen’. Read more
source§

fn check_frozen(self) -> Result<(), Error>

Returns an error if self is ‘frozen’. Read more
source§

fn freeze(self)

Mark self as frozen. Read more
source§

fn to_bool(self) -> bool

Convert self to a bool, following Ruby’s rules of false and nil as boolean false and everything else boolean true. Read more
source§

fn funcall<M, A, T>(self, method: M, args: A) -> Result<T, Error>
where M: IntoId, A: ArgList, T: TryConvert,

Call the method named method on self with args. Read more
source§

fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
where M: IntoId, A: ArgList, T: TryConvert,

Call the public method named method on self with args. Read more
source§

fn check_funcall<M, A, T>(self, method: M, args: A) -> Option<Result<T, Error>>
where M: IntoId, A: ArgList, T: TryConvert,

If self responds to the method named method, call it with args. Read more
source§

fn funcall_with_block<M, A, T>( self, method: M, args: A, block: Proc ) -> Result<T, Error>
where M: IntoId, A: ArgList, T: TryConvert,

Call the method named method on self with args and block. Read more
source§

fn block_call<M, A, R, T>( self, method: M, args: A, block: fn(_: &[Value], _: Option<Proc>) -> R ) -> Result<T, Error>
where M: IntoId, A: ArgList, R: BlockReturn, T: TryConvert,

Call the method named method on self with args and block. Read more
source§

fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>
where M: IntoId,

Check if self responds to the given Ruby method. Read more
source§

fn to_r_string(self) -> Result<RString, Error>

Convert self to a Ruby String. Read more
source§

unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>

Convert self to a Rust string. Read more
source§

fn inspect(self) -> String

Convert self to its Ruby debug representation. Read more
source§

unsafe fn classname(&self) -> Cow<'_, str>

Return the name of self’s class. Read more
source§

fn is_kind_of<T>(self, class: T) -> bool
where T: ReprValue + Module,

Returns whether or not self is an instance of class. Read more
source§

fn enumeratorize<M, A>(self, method: M, args: A) -> Enumerator
where M: IntoSymbol, A: ArgList,

Generate an Enumerator from method on self, passing args to method. Read more
source§

impl TryConvert for RString

source§

fn try_convert(val: Value) -> Result<Self, Error>

Convert val into Self.
source§

impl Write for RString

source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Write a buffer into this writer, returning how many bytes were written. Read more
source§

fn flush(&mut self) -> Result<()>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more
source§

impl Copy for RString

Auto Trait Implementations§

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> AsRawValue for T
where T: ReprValue,

source§

fn as_raw(self) -> u64

Available on crate feature rb-sys only.
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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Inspect for T
where T: Debug,

source§

fn inspect(&self) -> String

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,

§

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§

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

§

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

§

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.
source§

impl<T> BlockReturn for T
where T: BlockReturn,

source§

impl<T> Locate for T
where T: ReprValue,

source§

impl<T> Mark for T
where T: ReprValue,

source§

impl<T> ReturnValue for T
where T: ReturnValue,