#[repr(transparent)]pub struct Range(_);
Expand description
Implementations
sourceimpl Range
impl Range
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(Range)
if val
is an Range
, None
otherwise.
Examples
use magnus::eval;
assert!(magnus::Range::from_value(eval("2..7").unwrap()).is_some());
assert!(magnus::Range::from_value(eval("1").unwrap()).is_none());
sourcepub fn new<T, U>(beg: T, end: U, excl: bool) -> Result<Self, Error> where
T: Into<Value>,
U: Into<Value>,
pub fn new<T, U>(beg: T, end: U, excl: bool) -> Result<Self, Error> where
T: Into<Value>,
U: Into<Value>,
Create a new Range
.
Returns Err
if beg
and end
are not comparable.
Examples
use magnus::eval;
let range = magnus::Range::new(2, 7, false).unwrap();
let res: bool = eval!("range == (2..7)", range).unwrap();
assert!(res);
use magnus::eval;
let range = magnus::Range::new(2, 7, true).unwrap();
let res: bool = eval!("range == (2...7)", range).unwrap();
assert!(res);
sourcepub fn beg<T>(self) -> Result<T, Error> where
T: TryConvert,
pub fn beg<T>(self) -> Result<T, Error> where
T: TryConvert,
Return the value that defines the beginning of the range, converting it
to a T
.
Errors if the conversion fails.
Examples
use magnus::eval;
let range = eval::<magnus::Range>("2..7").unwrap();
assert_eq!(range.beg::<i64>().unwrap(), 2);
sourcepub fn end<T>(self) -> Result<T, Error> where
T: TryConvert,
pub fn end<T>(self) -> Result<T, Error> where
T: TryConvert,
Return the value that defines the end of the range, converting it
to a T
.
Errors if the conversion fails.
Examples
use magnus::eval;
let range = eval::<magnus::Range>("2..7").unwrap();
assert_eq!(range.end::<i64>().unwrap(), 7);
sourcepub fn excl(self) -> bool
pub fn excl(self) -> bool
Returns true
if the range excludes its end value, false
if the end
value is included.
Examples
use magnus::eval;
let range = eval::<magnus::Range>("2..7").unwrap();
assert_eq!(range.excl(), false);
sourcepub fn beg_len(self, length: usize) -> Result<(usize, usize), Error>
pub fn beg_len(self, length: usize) -> Result<(usize, usize), Error>
Given a total length
, returns a beginning index and length of the
range within that total length.
Returns Err
if self
is a non-numerical range, or the range is out
of range for length
.
Examples
use magnus::eval;
let range = eval::<magnus::Range>("2..").unwrap();
assert_eq!(range.beg_len(10).unwrap(), (2, 8));
use magnus::eval;
let range = eval::<magnus::Range>("..7").unwrap();
assert_eq!(range.beg_len(10).unwrap(), (0, 8));
use magnus::eval;
let range = eval::<magnus::Range>("-3..-1").unwrap();
assert_eq!(range.beg_len(10).unwrap(), (7, 3));
sourcepub fn to_range_with_len(self, length: usize) -> Result<StdRange<usize>, Error>
pub fn to_range_with_len(self, length: usize) -> Result<StdRange<usize>, Error>
Given a total length
, converts the Ruby Range
to a Rust
std::ops::Range
.
length
is required to account for Ruby conventions such as a range
from -2..-1
returning the end of a collection.
Returns Err
if self
is a non-numerical range, or the range is out
of range for length
.
Examples
use magnus::eval;
// Ruby's .. range is inclusive
let range = eval::<magnus::Range>("2..7").unwrap();
// Rust's .. range in exclusive
assert_eq!(range.to_range_with_len(10).unwrap(), 2..8);
use magnus::eval;
let range = eval::<magnus::Range>("2..").unwrap();
assert_eq!(range.to_range_with_len(10).unwrap(), 2..10);
use magnus::eval;
let range = eval::<magnus::Range>("..7").unwrap();
assert_eq!(range.to_range_with_len(10).unwrap(), 0..8);
use magnus::eval;
let range = eval::<magnus::Range>("-3..-1").unwrap();
assert_eq!(range.to_range_with_len(10).unwrap(), 7..10);
Methods from Deref<Target = Value>
sourcepub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
pub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
Convert self
to a Rust string.
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.
This can be used safely by immediately calling
into_owned
on the return value.
Examples
use magnus::{eval, QTRUE};
let value = QTRUE;
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.to_s() }.unwrap().into_owned();
assert_eq!(s, "true");
sourcepub unsafe fn classname(&self) -> Cow<'_, str>
pub unsafe fn classname(&self) -> Cow<'_, str>
Return the name of self
’s class.
Safety
Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.
This can be used safely by immediately calling
into_owned
on the return value.
Examples
use magnus::{eval, RHash};
let value = RHash::new();
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.classname() }.into_owned();
assert_eq!(s, "Hash");
sourcepub fn try_convert<T>(&self) -> Result<T, Error> where
T: TryConvert,
pub fn try_convert<T>(&self) -> Result<T, Error> where
T: TryConvert,
Convert self
to the Rust type T
.
See the types that TryConvert
is implemented on for what this
method can convert to.
Examples
use magnus::{eval, Value};
assert_eq!(eval::<Value>("42").unwrap().try_convert::<i64>().unwrap(), 42);
assert_eq!(eval::<Value>("1.23").unwrap().try_convert::<i64>().unwrap(), 1);
assert_eq!(eval::<Value>("1").unwrap().try_convert::<f64>().unwrap(), 1.0);
assert_eq!(eval::<Value>("nil").unwrap().try_convert::<Option<i64>>().unwrap(), None);
assert_eq!(eval::<Value>("42").unwrap().try_convert::<Option<i64>>().unwrap(), Some(42));
Trait Implementations
sourceimpl Object for Range
impl Object for Range
sourcefn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error> where
M: Method,
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
sourcefn ivar_get<T, U>(self, name: T) -> Result<U, Error> where
T: Into<Id>,
U: TryConvert,
fn ivar_get<T, U>(self, name: T) -> Result<U, Error> where
T: Into<Id>,
U: TryConvert,
Get the value for the instance variable name
within self
’s scope. Read more
sourcefn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error> where
T: Into<Id>,
U: Into<Value>,
fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error> where
T: Into<Id>,
U: Into<Value>,
Set the value for the instance variable name
within self
’s scope. Read more
sourceimpl TryConvert for Range
impl TryConvert for Range
sourcefn try_convert(val: &Value) -> Result<Self, Error>
fn try_convert(val: &Value) -> Result<Self, Error>
Convert val
into Self
.
impl Copy for Range
impl ReprValue for Range
Auto Trait Implementations
impl RefUnwindSafe for Range
impl Send for Range
impl Sync for Range
impl Unpin for Range
impl UnwindSafe for Range
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more