Trait magnus::encoding::EncodingCapable
source · pub trait EncodingCapable: Deref<Target = Value> {
fn enc_get(&self) -> Index { ... }
fn enc_set<T>(&self, enc: T) -> Result<(), Error>
where
T: Into<Index>,
{ ... }
fn enc_associate<T>(&self, enc: T) -> Result<(), Error>
where
T: Into<Index>,
{ ... }
}
Expand description
Trait that marks Ruby types cable of having an encoding.
Provided Methods
sourcefn enc_get(&self) -> Index
fn enc_get(&self) -> Index
Get the encoding of self
.
Examples
use magnus::{encoding::{self, EncodingCapable}, RString};
assert!(RString::new("example").enc_get() == encoding::Index::utf8());
sourcefn enc_set<T>(&self, enc: T) -> Result<(), Error>where
T: Into<Index>,
fn enc_set<T>(&self, enc: T) -> Result<(), Error>where
T: Into<Index>,
Set self
’s encoding.
Returns Err
if self
is frozen or the encoding can not be loaded.
See also EncodingCapable::enc_associate
.
Examples
use magnus::{encoding::{self, EncodingCapable}, RString};
let s = RString::new("example");
assert!(s.enc_get() == encoding::Index::utf8());
s.enc_set(encoding::Index::usascii());
assert!(s.enc_get() == encoding::Index::usascii());
sourcefn enc_associate<T>(&self, enc: T) -> Result<(), Error>where
T: Into<Index>,
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.
For example, Ruby’s strings contain an additional terminating null byte hidden from Ruby, but allowing for easy c string interop. This method will adjust the length of that terminating char depending on the encoding.
Returns Err
if self
is frozen or the encoding can not be loaded.
Examples
use magnus::{encoding::{self, EncodingCapable}, RString};
let s = RString::new("example");
assert!(s.enc_get() == encoding::Index::utf8());
s.enc_associate(encoding::Index::usascii());
assert!(s.enc_get() == encoding::Index::usascii());