Move slice::raw::from_buf_raw() to vec::raw::from_buf()

Change from_buf_raw() to return a Vec<T> instead of a ~[T]. As such, it
belongs in vec, in the newly-created vec::raw module.
This commit is contained in:
Kevin Ballard 2014-05-03 18:20:35 -07:00
parent 3296bd7e46
commit 189dc5f30b
3 changed files with 39 additions and 59 deletions

View File

@ -22,7 +22,7 @@ use std::ptr;
use std::rt::rtio;
use std::str;
use std::sync::arc::UnsafeArc;
use std::slice;
use std::vec;
use io::IoResult;
@ -368,8 +368,8 @@ pub fn readdir(p: &CString) -> IoResult<Vec<Path>> {
if fp_buf as uint == 0 {
fail!("os::list_dir() failure: got null ptr from wfd");
} else {
let fp_vec = slice::from_buf(fp_buf, libc::wcslen(fp_buf) as uint);
let fp_trimmed = str::truncate_utf16_at_nul(fp_vec);
let fp_vec = vec::raw::from_buf(fp_buf, libc::wcslen(fp_buf) as uint);
let fp_trimmed = str::truncate_utf16_at_nul(fp_vec.as_slice());
let fp_str = str::from_utf16(fp_trimmed)
.expect("rust_list_dir_wfd_fp_buf returned invalid UTF-16");
paths.push(Path::new(fp_str));

View File

@ -722,19 +722,6 @@ impl<'a, T: TotalOrd> MutableTotalOrdVector<T> for &'a mut [T] {
}
}
/**
* Constructs a vector from an unsafe pointer to a buffer
*
* # Arguments
*
* * ptr - An unsafe pointer to a buffer of `T`
* * elts - The number of elements in the buffer
*/
// Wrapper for fn in raw: needs to be called by net_tcp::on_tcp_read_cb
pub unsafe fn from_buf<T>(ptr: *T, elts: uint) -> ~[T] {
raw::from_buf_raw(ptr, elts)
}
/// Unsafe operations
pub mod raw {
use iter::Iterator;
@ -744,23 +731,6 @@ pub mod raw {
pub use core::slice::raw::{buf_as_slice, mut_buf_as_slice};
pub use core::slice::raw::{shift_ptr, pop_ptr};
/**
* Constructs a vector from an unsafe pointer to a buffer
*
* # Arguments
*
* * ptr - An unsafe pointer to a buffer of `T`
* * elts - The number of elements in the buffer
*/
// Was in raw, but needs to be called by net_tcp::on_tcp_read_cb
#[inline]
pub unsafe fn from_buf_raw<T>(ptr: *T, elts: uint) -> ~[T] {
let mut dst = Vec::with_capacity(elts);
dst.set_len(elts);
ptr::copy_memory(dst.as_mut_ptr(), ptr, elts);
dst.move_iter().collect()
}
}
/// An iterator that moves out of a vector.
@ -820,31 +790,6 @@ mod tests {
fn is_odd(n: &uint) -> bool { *n % 2u == 1u }
#[test]
fn test_unsafe_ptrs() {
unsafe {
// Test on-stack copy-from-buf.
let a = box [1, 2, 3];
let mut ptr = a.as_ptr();
let b = from_buf(ptr, 3u);
assert_eq!(b.len(), 3u);
assert_eq!(b[0], 1);
assert_eq!(b[1], 2);
assert_eq!(b[2], 3);
// Test on-heap copy-from-buf.
let c = box [1, 2, 3, 4, 5];
ptr = c.as_ptr();
let d = from_buf(ptr, 5u);
assert_eq!(d.len(), 5u);
assert_eq!(d[0], 1);
assert_eq!(d[1], 2);
assert_eq!(d[2], 3);
assert_eq!(d[3], 4);
assert_eq!(d[4], 5);
}
}
#[test]
fn test_from_fn() {
// Test on-stack from_fn.

View File

@ -1455,12 +1455,30 @@ pub fn unzip<T, U, V: Iterator<(T, U)>>(mut iter: V) -> (Vec<T>, Vec<U>) {
(ts, us)
}
/// Unsafe operations
pub mod raw {
use super::Vec;
use ptr;
/// Constructs a vector from an unsafe pointer to a buffer.
///
/// The elements of the buffer are copied into the vector without cloning,
/// as if `ptr::read()` were called on them.
#[inline]
pub unsafe fn from_buf<T>(ptr: *T, elts: uint) -> Vec<T> {
let mut dst = Vec::with_capacity(elts);
dst.set_len(elts);
ptr::copy_nonoverlapping_memory(dst.as_mut_ptr(), ptr, elts);
dst
}
}
#[cfg(test)]
mod tests {
use prelude::*;
use mem::size_of;
use super::unzip;
use super::{unzip, raw};
#[test]
fn test_small_vec_struct() {
@ -1720,4 +1738,21 @@ mod tests {
assert_eq!((2, 5), (left[1], right[1]));
assert_eq!((3, 6), (left[2], right[2]));
}
#[test]
fn test_unsafe_ptrs() {
unsafe {
// Test on-stack copy-from-buf.
let a = [1, 2, 3];
let ptr = a.as_ptr();
let b = raw::from_buf(ptr, 3u);
assert_eq!(b, vec![1, 2, 3]);
// Test on-heap copy-from-buf.
let c = box [1, 2, 3, 4, 5];
let ptr = c.as_ptr();
let d = raw::from_buf(ptr, 5u);
assert_eq!(d, vec![1, 2, 3, 4, 5]);
}
}
}