@@ -28,7 +28,7 @@ use crate::fmt;
2828use crate :: intrinsics:: { assume, exact_div, unchecked_sub, is_aligned_and_not_null} ;
2929use crate :: isize;
3030use crate :: iter:: * ;
31- use crate :: ops:: { FnMut , self } ;
31+ use crate :: ops:: { FnMut , Range , self } ;
3232use crate :: option:: Option ;
3333use crate :: option:: Option :: { None , Some } ;
3434use crate :: result:: Result ;
@@ -407,6 +407,65 @@ impl<T> [T] {
407407 self as * mut [ T ] as * mut T
408408 }
409409
410+ /// Returns the two raw pointers spanning the slice.
411+ ///
412+ /// The returned range is half-open, which means that the end pointer
413+ /// points *one past* the last element of the slice. This way, an empty
414+ /// slice is represented by two equal pointers, and the difference between
415+ /// the two pointers represents the size of the size.
416+ ///
417+ /// See [`as_ptr`] for warnings on using these pointers. The end pointer
418+ /// requires extra caution, as it does not point to a valid element in the
419+ /// slice.
420+ ///
421+ /// This function is useful for interacting with foreign interfaces which
422+ /// use two pointers to refer to a range of elements in memory, as is
423+ /// common in C++.
424+ ///
425+ /// It can also be useful to check if a reference or pointer to an element
426+ /// refers to an element of this slice:
427+ ///
428+ /// ```
429+ /// let a = [1,2,3];
430+ /// let x = &a[1];
431+ /// let y = &5;
432+ /// assert!(a.as_ptr_range().contains(x));
433+ /// assert!(!a.as_ptr_range().contains(y));
434+ /// ```
435+ ///
436+ /// [`as_ptr`]: #method.as_ptr
437+ #[ unstable( feature = "slice_ptr_range" , issue = "0" ) ]
438+ #[ inline]
439+ pub fn as_ptr_range ( & self ) -> Range < * const T > {
440+ let start = self . as_ptr ( ) ;
441+ let end = unsafe { start. add ( self . len ( ) ) } ;
442+ start..end
443+ }
444+
445+ /// Returns the two unsafe mutable pointers spanning the slice.
446+ ///
447+ /// The returned range is half-open, which means that the end pointer
448+ /// points *one past* the last element of the slice. This way, an empty
449+ /// slice is represented by two equal pointers, and the difference between
450+ /// the two pointers represents the size of the size.
451+ ///
452+ /// See [`as_mut_ptr`] for warnings on using these pointers. The end
453+ /// pointer requires extra caution, as it does not point to a valid element
454+ /// in the slice.
455+ ///
456+ /// This function is useful for interacting with foreign interfaces which
457+ /// use two pointers to refer to a range of elements in memory, as is
458+ /// common in C++.
459+ ///
460+ /// [`as_mut_ptr`]: #method.as_mut_ptr
461+ #[ unstable( feature = "slice_ptr_range" , issue = "0" ) ]
462+ #[ inline]
463+ pub fn as_mut_ptr_range ( & mut self ) -> Range < * mut T > {
464+ let start = self . as_mut_ptr ( ) ;
465+ let end = unsafe { start. add ( self . len ( ) ) } ;
466+ start..end
467+ }
468+
410469 /// Swaps two elements in the slice.
411470 ///
412471 /// # Arguments
0 commit comments