wiki/content/20201120095739-as_ptr_range.md

1.1 KiB

date id title
2020-11-20 e3a6f28a-27fd-489d-b2b4-de6d2c149a60 as~ptrrange~

Description

asptrrange1 returns the two raw pointers spanning the slice.

The returned range is half-open, which means that the end pointer points one past the last element of the slice. This way, an empty slice is represented by two equal pointers, and the difference between the two pointers represents the size of the slice.

This function is useful for interacting with foreign interfaces which use two pointers to refer to a range of elements in memory, as is common in C++.

Declaration

pub fn as_ptr_range(&self) -> Range<*const T>

Traits for Range<A>

impl<A> Iterator for Range<A> where
    A: Step,     type Item = A;

Syntax

#![allow(unused)]
fn main() {
    let a = [1, 2, 3];
    let x = &a[1] as *const _;
    let y = &5 as *const _;

    assert!(a.as_ptr_range().contains(&x));
    assert!(!a.as_ptr_range().contains(&y));
}

Footnotes