1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
use std::convert::From; use binding::string; use types::{Value, ValueType}; use {Object, VerifiedObject}; /// `String` #[derive(Debug, PartialEq)] pub struct RString { value: Value, } impl RString { /// Creates a new instance of Ruby `String` containing given `string`. /// /// # Examples /// /// ``` /// use ruru::{RString, VM}; /// # VM::init(); /// /// let string = RString::new("Hello, World!"); /// /// assert_eq!(string.to_string(), "Hello, World!".to_string()); /// ``` /// /// Ruby: /// /// ```ruby /// str = 'Hello, World!' /// /// str == 'Hello, World!' /// ``` pub fn new(string: &str) -> Self { Self::from(string::new(string)) } /// Retrieves underlying Rust `String` from Ruby `String` object. /// /// # Examples /// /// ``` /// use ruru::{RString, VM}; /// # VM::init(); /// /// let string = RString::new("Hello, World!"); /// /// assert_eq!(string.to_string(), "Hello, World!".to_string()); /// ``` /// /// Ruby: /// /// ```ruby /// str = 'Hello, World!' /// /// str == 'Hello, World!' /// ``` pub fn to_string(&self) -> String { string::from_value(self.value()) } /// Retrieves underlying Rust `String` from Ruby `String` object. /// /// Unlike `to_string()` it does not perform any checks for internal null-bytes. /// /// This function may be used to safely get binary data from Ruby. /// /// # Examples /// /// ```no_run /// use ruru::{RString, VM}; /// # VM::init(); /// /// let string = RString::new("Hello,\0World!"); /// /// assert_eq!(string.to_string_unchecked(), "Hello,\0World!".to_string()); /// ``` /// /// Ruby: /// /// ```ruby /// str = 'Hello,\0World!' /// /// str == 'Hello,\0World!' /// ``` pub fn to_string_unchecked(&self) -> String { string::from_value_unchecked(self.value()) } /// Returns the length of the string in bytes /// /// # Examples /// /// ``` /// use ruru::{RString, VM}; /// # VM::init(); /// /// let string = RString::new("Hello, World!"); /// let utf8_string = RString::new("⓯"); /// /// assert_eq!(string.bytesize(), 13); /// assert_eq!(utf8_string.bytesize(), 3); /// ``` /// /// Ruby: /// /// ```ruby /// string = 'Hello, World!' /// utf8_string = '⓯' /// /// string.bytesize == 13 /// utf8_string.bytesize == 3 /// ``` pub fn bytesize(&self) -> i64 { string::bytesize(self.value()) } } impl From<Value> for RString { fn from(value: Value) -> Self { RString { value: value } } } impl Object for RString { #[inline] fn value(&self) -> Value { self.value } } impl VerifiedObject for RString { fn is_correct_type<T: Object>(object: &T) -> bool { object.value().ty() == ValueType::RString } fn error_message() -> &'static str { "Error converting to String" } }