Add a ref_slice module.

Utility functions for converting &T to an &[T] slice with a single
element.
This commit is contained in:
Jakob Stoklund Olesen
2016-11-04 12:30:51 -07:00
parent 2b8732ae13
commit c995cb6f43
2 changed files with 19 additions and 0 deletions

View File

@@ -21,3 +21,4 @@ mod write;
mod constant_hash;
mod predicates;
mod legalizer;
mod ref_slice;

View File

@@ -0,0 +1,18 @@
//! Functions for converting a reference into a singleton slice.
//!
//! See also the ref_slice crate on crates.io.
//!
//! We define the functions here to avoid external dependencies, and to ensure that they are
//! inlined in this crate.
//!
//! Despite their using an unsafe block, these functions are completely safe.
use std::slice;
pub fn ref_slice<T>(s: &T) -> &[T] {
unsafe { slice::from_raw_parts(s, 1) }
}
pub fn ref_slice_mut<T>(s: &mut T) -> &mut [T] {
unsafe { slice::from_raw_parts_mut(s, 1) }
}