Open andreeaflorescu opened 2 years ago
To make it easy to use, the crate defined range should define Deref
and DerefMut
such that all functions defined in std::ops
are also available for the crate defined range. This is following the NewType Rust pattern: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#using-the-newtype-pattern-to-implement-external-traits-on-external-types
If we use std::ops::RangeInclusive
, we also need to implement PartialOrd
, PartialEq
for RangeInclusive
. And std::ops::RangeInclusive
doesn't implement Copy trait
Why is the Copy
trait needed?
The Copy
trait doesn't seem to be necessary.
std::ops::RangeInclusive.contain()
is to check one number in this range. So we also implement the contain function. This means we only need len
, start
, end
function in std::ops::RangeInclusive
The sample code:
#[derive(Clone, Debug)]
pub struct RangeInclusive {
inner: std::ops::RangeInclusive<u64>
}
impl PartialEq for RangeInclusive {
fn eq(&self, other: &Self) -> bool {
}
}
impl Eq for RangeInclusive {}
impl PartialOrd for RangeInclusive {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
}
}
impl Ord for RangeInclusive {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
}
}
impl RangeInclusive {
pub fn new(start: u64, end: u64) -> Result<Self> {
}
pub fn contains(&self, other: &RangeInclusive) -> bool {
}
pub fn overlaps(&self, other: &RangeInclusive) -> bool {
}
}
I think both https://github.com/rust-vmm/vm-allocator/pull/36 and https://github.com/rust-vmm/vm-allocator/pull/38 cover this.
The
Range
object already exists in Rust. Instead of reimplementing the functionality, we can just extend such that we can add the needed functionality, which for now is just theoverlaps
function.Also, depending on how the crate is shaped, we will need to use either
Range
orRangeInclusive
, where the latter represents a range that is inclusive on both ends: