Struct magnus::Range

source ·
#[repr(transparent)]
pub struct Range(_);
Expand description

Wrapper type for a Value known to be an instance of Ruby’s Range class.

All Value methods should be available on this type through Deref, but some may be missed by this documentation.

Implementations§

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());

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);

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);

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);

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);

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));

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

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");

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 never give Ruby a chance to free the string.
let s = unsafe { value.classname() }.into_owned();
assert_eq!(s, "Hash");

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Define a singleton method in self’s scope. Read more
Get the value for the instance variable name within self’s scope. Read more
Set the value for the instance variable name within self’s scope. Read more
Finds or creates the singleton class of self. Read more
Extend self with module. Read more
Convert val into Self.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.