Sha256: 5a4e9284148747f00dbe08831b87c5c2528e68eff266a6f1996fcb11d00ed821

Contents?: true

Size: 1.99 KB

Versions: 38

Compression:

Stored size: 1.99 KB

Contents

/// Represents a contiguous region in memory.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Region {
    pub start: u32,
    pub len: u32,
}

impl Region {
    pub fn new(start: u32, len: u32) -> Self {
        Self { start, len }
    }

    /// Checks if this `Region` overlaps with `rhs` `Region`.
    pub fn overlaps(&self, rhs: Region) -> bool {
        // Zero-length regions can never overlap!
        if self.len == 0 || rhs.len == 0 {
            return false;
        }

        let self_start = self.start as u64;
        let self_end = self_start + (self.len - 1) as u64;

        let rhs_start = rhs.start as u64;
        let rhs_end = rhs_start + (rhs.len - 1) as u64;

        if self_start <= rhs_start {
            self_end >= rhs_start
        } else {
            rhs_end >= self_start
        }
    }

    pub fn extend(&self, times: u32) -> Self {
        let len = self.len * times;
        Self {
            start: self.start,
            len,
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn zero_length() {
        let r1 = Region::new(0, 0);
        let r2 = Region::new(0, 1);
        assert!(!r1.overlaps(r2));

        let r1 = Region::new(0, 1);
        let r2 = Region::new(0, 0);
        assert!(!r1.overlaps(r2));
    }

    #[test]
    fn nonoverlapping() {
        let r1 = Region::new(0, 10);
        let r2 = Region::new(10, 10);
        assert!(!r1.overlaps(r2));

        let r1 = Region::new(10, 10);
        let r2 = Region::new(0, 10);
        assert!(!r1.overlaps(r2));
    }

    #[test]
    fn overlapping() {
        let r1 = Region::new(0, 10);
        let r2 = Region::new(9, 10);
        assert!(r1.overlaps(r2));

        let r1 = Region::new(0, 10);
        let r2 = Region::new(2, 5);
        assert!(r1.overlaps(r2));

        let r1 = Region::new(9, 10);
        let r2 = Region::new(0, 10);
        assert!(r1.overlaps(r2));

        let r1 = Region::new(2, 5);
        let r2 = Region::new(0, 10);
        assert!(r1.overlaps(r2));
    }
}

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/wiggle-29.0.0/src/region.rs
wasmtime-28.0.0 ./ext/cargo-vendor/wiggle-28.0.0/src/region.rs
wasmtime-27.0.0 ./ext/cargo-vendor/wiggle-27.0.0/src/region.rs
wasmtime-26.0.0 ./ext/cargo-vendor/wiggle-26.0.0/src/region.rs
wasmtime-25.0.2 ./ext/cargo-vendor/wiggle-25.0.2/src/region.rs
wasmtime-25.0.1 ./ext/cargo-vendor/wiggle-25.0.1/src/region.rs
wasmtime-25.0.0 ./ext/cargo-vendor/wiggle-25.0.0/src/region.rs
wasmtime-24.0.0 ./ext/cargo-vendor/wiggle-24.0.0/src/region.rs
wasmtime-23.0.2 ./ext/cargo-vendor/wiggle-23.0.2/src/region.rs
wasmtime-22.0.0 ./ext/cargo-vendor/wiggle-22.0.0/src/region.rs
wasmtime-21.0.1 ./ext/cargo-vendor/wiggle-21.0.1/src/region.rs
wasmtime-20.0.2 ./ext/cargo-vendor/wiggle-20.0.2/src/region.rs
wasmtime-20.0.0 ./ext/cargo-vendor/wiggle-20.0.0/src/region.rs
wasmtime-18.0.3 ./ext/cargo-vendor/wiggle-18.0.3/src/region.rs
wasmtime-17.0.1 ./ext/cargo-vendor/wiggle-17.0.1/src/region.rs
wasmtime-17.0.0 ./ext/cargo-vendor/wiggle-17.0.0/src/region.rs
wasmtime-16.0.0 ./ext/cargo-vendor/wiggle-16.0.0/src/region.rs
wasmtime-15.0.1 ./ext/cargo-vendor/wiggle-15.0.1/src/region.rs
wasmtime-15.0.0 ./ext/cargo-vendor/wiggle-15.0.0/src/region.rs
wasmtime-14.0.4 ./ext/cargo-vendor/wiggle-14.0.4/src/region.rs