Trait rutie::EncodingSupport[][src]

pub trait EncodingSupport {
    fn encode(&self, enc: Encoding, opts: Option<Hash>) -> Self
    where
        Self: Sized
;
fn encoding(&self) -> Encoding;
fn force_encoding(&mut self, enc: Encoding) -> Result<Self, AnyException>
    where
        Self: Sized
;
fn is_valid_encoding(&self) -> bool;
fn compatible_with(&self, other: &impl Object) -> bool;
fn compatible_encoding(obj1: &impl Object, obj2: &impl Object) -> AnyObject; }

Required methods

fn encode(&self, enc: Encoding, opts: Option<Hash>) -> Self where
    Self: Sized
[src]

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

fn force_encoding(&mut self, enc: Encoding) -> Result<Self, AnyException> where
    Self: Sized
[src]

fn is_valid_encoding(&self) -> bool[src]

fn compatible_with(&self, other: &impl Object) -> bool[src]

fn compatible_encoding(obj1: &impl Object, obj2: &impl Object) -> AnyObject[src]

Loading content...

Implementors

impl EncodingSupport for RString[src]

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

Get the strings Encoding.

Examples

use rutie::{RString, VM, EncodingSupport};

let string = RString::new_utf8("Hello");
string.encoding();

Ruby:

string = "Hello"
string.encoding()

fn force_encoding(&mut self, enc: Encoding) -> Result<Self, AnyException>[src]

Changes the encoding to encoding and returns Result<Self, AnyException>.

Examples

use rutie::{RString, VM, EncodingSupport, Encoding};

let mut string = RString::new_utf8("Hello");
string.force_encoding(Encoding::us_ascii());

assert_eq!(string.encoding().name(), "US-ASCII");

Ruby:

string = "Hello"
string.force_encoding(Encoding::US_ASCII)

string.encoding.name == "US-ASCII"
use rutie::{RString, VM, EncodingSupport, Encoding, Object, Exception};

let mut string = RString::new_utf8("Hello");
string.force_encoding(Encoding::utf8());
string.freeze();
let result = string.force_encoding(Encoding::us_ascii());

match result {
    Ok(_) => assert_eq!("This is a bad path.", "You shouldn't get this message."),
    Err(happy_path) => assert_eq!(happy_path.message(), "can\'t modify frozen String"),
}

fn encode(&self, enc: Encoding, opts: Option<Hash>) -> Self[src]

Transcodes to encoding and returns Self.

Examples

use rutie::{RString, VM, EncodingSupport, Encoding};

let mut string = RString::new_utf8("Hello");
let result = string.encode(Encoding::us_ascii(), None);

assert_eq!(result.encoding().name(), "US-ASCII");

Ruby:

string = "Hello"
result = string.encode(Encoding::US_ASCII)

result.encoding.name == "US-ASCII"

fn is_valid_encoding(&self) -> bool[src]

Transcodes to encoding and returns Self.

Examples

use rutie::{RString, VM, EncodingSupport, Encoding, Object};

let mut string = RString::new_utf8("Hello");

assert!(string.is_valid_encoding(), "not valid encoding!");

VM::require("enc/encdb");
VM::require("enc/trans/transdb");

let result = VM::eval("'Hello'.force_encoding('UTF-32')").unwrap().
  try_convert_to::<RString>().unwrap();

assert!(!result.is_valid_encoding(), "is valid encoding!");

Ruby:

string = "Hello"

string.valid_encoding? == true

result = string.encode(Encoding::UTF_32)

result.valid_encoding? == false

fn compatible_with(&self, other: &impl Object) -> bool[src]

Reveals if the given object has a compatible encoding with this String.

Examples

use rutie::{RString, VM, EncodingSupport};

let string1 = RString::new_utf8("Hello");
let string2 = RString::new_usascii_unchecked("Hello");

assert!(string1.compatible_with(&string2));

Ruby:

str1 = 'Hello'.force_encoding("UTF-8")
str2 = 'Hello'.force_encoding("US-ASCII")

str1 + str2 == "HelloHello"

fn compatible_encoding(obj1: &impl Object, obj2: &impl Object) -> AnyObject[src]

Returns AnyObject of the compatible encoding between the two objects or nil if incompatible.

Examples

use rutie::{RString, VM, EncodingSupport};

let string1 = RString::new_utf8("Hello");
let string2 = RString::new_usascii_unchecked("Hello");

RString::compatible_encoding(&string1, &string2);

Ruby:

str1 = 'Hello'.force_encoding("UTF-8")
str2 = 'Hello'.force_encoding("US-ASCII")

begin
  (str1 + str2).encoding
rescue
  nil
end
Loading content...