From b331b65888f84a5f3a0b25c7e711f686cca1a695 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sat, 23 Aug 2014 20:26:53 -0400 Subject: [PATCH] add #[experimental] as_string/as_vec functions This provides a way to pass `&[T]` to functions taking `&U` where `U` is a `Vec`. It can be seen as an alternative to the `Equiv` trait, as it handles all of the common cases well, without requiring a new trait and new equiv methods. Methods like `find_with` on TreeMap taking a closure with an equivalent `Ord` implementation are still useful. Closes #14549 --- src/libcollections/string.rs | 28 ++++++++++++- src/libcollections/vec.rs | 76 ++++++++++++++++++++++++++++++------ 2 files changed, 89 insertions(+), 15 deletions(-) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 05d91a7504150..e45294cd71ff6 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -26,7 +26,7 @@ use hash; use str; use str::{CharRange, StrAllocating, MaybeOwned, Owned}; use str::Slice as MaybeOwnedSlice; // So many `Slice`s... -use vec::Vec; +use vec::{AsVec, Vec, as_vec}; /// A growable string stored as a UTF-8 encoded buffer. #[deriving(Clone, PartialEq, PartialOrd, Eq, Ord)] @@ -808,6 +808,24 @@ impl Add for String { } } +/// Wrapper type providing a `&String` reference via `Deref`. +#[experimental] +pub struct AsString<'a> { + x: AsVec<'a, u8> +} + +impl<'a> Deref for AsString<'a> { + fn deref<'b>(&'b self) -> &'b String { + unsafe { mem::transmute(&*self.x) } + } +} + +/// Convert a string slice to a wrapper type providing a `&String` reference. +#[experimental] +pub fn as_string<'a>(x: &'a str) -> AsString<'a> { + AsString { x: as_vec(x.as_bytes()) } +} + /// Unsafe operations pub mod raw { use core::mem; @@ -873,9 +891,15 @@ mod tests { use {Mutable, MutableSeq}; use str; use str::{Str, StrSlice, Owned, Slice}; - use super::String; + use super::{as_string, String}; use vec::Vec; + #[test] + fn test_as_string() { + let x = "foo"; + assert_eq!(x, as_string(xs).as_slice()); + } + #[test] fn test_from_str() { let owned: Option<::std::string::String> = from_str("string"); diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index a7005cf454db5..a8714ae093975 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -16,6 +16,7 @@ use alloc::heap::{allocate, reallocate, deallocate}; use core::cmp::max; use core::default::Default; use core::fmt; +use core::kinds; use core::mem; use core::num; use core::ptr; @@ -750,7 +751,6 @@ impl Vec { } } - /// Sets the length of a vector. /// /// This will explicitly set the size of the vector, without actually @@ -1692,6 +1692,39 @@ pub fn unzip>(mut iter: V) -> (Vec, Vec) { (ts, us) } +/// Wrapper type providing a `&Vec` reference via `Deref`. +#[experimental] +pub struct AsVec<'a, T> { + x: Vec, + l: kinds::marker::ContravariantLifetime<'a> +} + +impl<'a, T> Deref> for AsVec<'a, T> { + fn deref<'b>(&'b self) -> &'b Vec { + &self.x + } +} + +// Prevent the inner `Vec` from attempting to deallocate memory. +#[unsafe_destructor] +impl<'a, T> Drop for AsVec<'a, T> { + fn drop(&mut self) { + self.x.len = 0; + self.x.cap = 0; + } +} + +/// Convert a slice to a wrapper type providing a `&Vec` reference. +#[experimental] +pub fn as_vec<'a, T>(x: &'a [T]) -> AsVec<'a, T> { + unsafe { + AsVec { + x: Vec::from_raw_parts(x.len(), x.len(), x.as_ptr() as *mut T), + l: kinds::marker::ContravariantLifetime::<'a> + } + } +} + /// Unsafe vector operations. pub mod raw { use super::Vec; @@ -1717,10 +1750,38 @@ mod tests { use std::prelude::*; use std::mem::size_of; use test::Bencher; - use super::{unzip, raw, Vec}; + use super::{as_vec, unzip, raw, Vec}; use MutableSeq; + struct DropCounter<'a> { + count: &'a mut int + } + + #[unsafe_destructor] + impl<'a> Drop for DropCounter<'a> { + fn drop(&mut self) { + *self.count += 1; + } + } + + #[test] + fn test_as_vec() { + let xs = &[1u8, 2u8, 3u8]; + assert_eq!(xs, as_vec(xs).as_slice()); + } + + #[test] + fn test_as_vec_dtor() { + let (mut count_x, mut count_y) = (0, 0); + { + let xs = &[DropCounter { count: &mut count_x }, DropCounter { count: &mut count_y }]; + assert_eq!(as_vec(xs).len(), 2); + } + assert_eq!(count_x, 1); + assert_eq!(count_y, 1); + } + #[test] fn test_small_vec_struct() { assert!(size_of::>() == size_of::() * 3); @@ -1733,17 +1794,6 @@ mod tests { y: Vec } - struct DropCounter<'a> { - count: &'a mut int - } - - #[unsafe_destructor] - impl<'a> Drop for DropCounter<'a> { - fn drop(&mut self) { - *self.count += 1; - } - } - let (mut count_x, mut count_y) = (0, 0); { let mut tv = TwoVec {