From c88ce30c485b8df5f6dfa55d216c19c10cafe5dd Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sat, 9 Mar 2013 19:14:35 -0800 Subject: [PATCH] core: Add vec::raw::mut_buf_as_slice --- src/libcore/vec.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index aed98f3573e71..697bfe63b189b 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -2145,6 +2145,20 @@ pub mod raw { f(*v) } + /** + * Form a slice from a pointer and length (as a number of units, + * not bytes). + */ + #[inline(always)] + pub unsafe fn mut_buf_as_slice(p: *mut T, + len: uint, + f: &fn(v: &mut [T]) -> U) -> U { + let pair = (p, len * sys::nonzero_size_of::()); + let v : *(&blk/mut [T]) = + ::cast::reinterpret_cast(&addr_of(&pair)); + f(*v) + } + /** * Unchecked vector indexing. */