Skip to content

Commit 423e17b

Browse files
author
Jorge Aparicio
committed
DSTify BytesContainer
1 parent 39f90ae commit 423e17b

File tree

3 files changed

+31
-54
lines changed

3 files changed

+31
-54
lines changed

src/libstd/path/mod.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ println!("path exists: {}", path.exists());
6767

6868
#![experimental]
6969

70+
use core::kinds::Sized;
7071
use c_str::CString;
7172
use clone::Clone;
7273
use fmt;
@@ -626,7 +627,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
626627
/// ```
627628
#[inline]
628629
fn push_many<T: BytesContainer>(&mut self, paths: &[T]) {
629-
let t: Option<T> = None;
630+
let t: Option<&T> = None;
630631
if BytesContainer::is_str(t) {
631632
for p in paths.iter() {
632633
self.push(p.container_as_str().unwrap())
@@ -791,14 +792,9 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
791792
}
792793

793794
/// A trait that represents something bytes-like (e.g. a &[u8] or a &str)
794-
pub trait BytesContainer {
795+
pub trait BytesContainer for Sized? {
795796
/// Returns a &[u8] representing the receiver
796797
fn container_as_bytes<'a>(&'a self) -> &'a [u8];
797-
/// Consumes the receiver and converts it into Vec<u8>
798-
#[inline]
799-
fn container_into_owned_bytes(self) -> Vec<u8> {
800-
self.container_as_bytes().to_vec()
801-
}
802798
/// Returns the receiver interpreted as a utf-8 string, if possible
803799
#[inline]
804800
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
@@ -807,7 +803,7 @@ pub trait BytesContainer {
807803
/// Returns whether .container_as_str() is guaranteed to not fail
808804
// FIXME (#8888): Remove unused arg once ::<for T> works
809805
#[inline]
810-
fn is_str(_: Option<Self>) -> bool { false }
806+
fn is_str(_: Option<&Self>) -> bool { false }
811807
}
812808

813809
/// A trait that represents the unsafe operations on GenericPaths
@@ -859,48 +855,44 @@ impl<'a, P: GenericPath> Display<'a, P> {
859855
}
860856
}
861857

862-
impl<'a> BytesContainer for &'a str {
858+
impl BytesContainer for str {
863859
#[inline]
864-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
860+
fn container_as_bytes(&self) -> &[u8] {
865861
self.as_bytes()
866862
}
867863
#[inline]
868-
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
869-
Some(*self)
864+
fn container_as_str(&self) -> Option<&str> {
865+
Some(self)
870866
}
871867
#[inline]
872-
fn is_str(_: Option<&'a str>) -> bool { true }
868+
fn is_str(_: Option<&str>) -> bool { true }
873869
}
874870

875871
impl BytesContainer for String {
876872
#[inline]
877-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
873+
fn container_as_bytes(&self) -> &[u8] {
878874
self.as_bytes()
879875
}
880876
#[inline]
881-
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
877+
fn container_as_str(&self) -> Option<&str> {
882878
Some(self.as_slice())
883879
}
884880
#[inline]
885-
fn is_str(_: Option<String>) -> bool { true }
881+
fn is_str(_: Option<&String>) -> bool { true }
886882
}
887883

888-
impl<'a> BytesContainer for &'a [u8] {
884+
impl BytesContainer for [u8] {
889885
#[inline]
890-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
891-
*self
886+
fn container_as_bytes(&self) -> &[u8] {
887+
self
892888
}
893889
}
894890

895891
impl BytesContainer for Vec<u8> {
896892
#[inline]
897-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
893+
fn container_as_bytes(&self) -> &[u8] {
898894
self.as_slice()
899895
}
900-
#[inline]
901-
fn container_into_owned_bytes(self) -> Vec<u8> {
902-
self
903-
}
904896
}
905897

906898
impl BytesContainer for CString {
@@ -920,7 +912,20 @@ impl<'a> BytesContainer for str::MaybeOwned<'a> {
920912
Some(self.as_slice())
921913
}
922914
#[inline]
923-
fn is_str(_: Option<str::MaybeOwned>) -> bool { true }
915+
fn is_str(_: Option<&str::MaybeOwned>) -> bool { true }
916+
}
917+
918+
impl<'a, Sized? T: BytesContainer> BytesContainer for &'a T {
919+
#[inline]
920+
fn container_as_bytes(&self) -> &[u8] {
921+
(**self).container_as_bytes()
922+
}
923+
#[inline]
924+
fn container_as_str(&self) -> Option<&str> {
925+
(**self).container_as_str()
926+
}
927+
#[inline]
928+
fn is_str(_: Option<& &'a T>) -> bool { BytesContainer::is_str(None::<&T>) }
924929
}
925930

926931
#[inline(always)]

src/libstd/path/posix.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,6 @@ impl BytesContainer for Path {
117117
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
118118
self.as_vec()
119119
}
120-
#[inline]
121-
fn container_into_owned_bytes(self) -> Vec<u8> {
122-
self.into_vec()
123-
}
124-
}
125-
126-
impl<'a> BytesContainer for &'a Path {
127-
#[inline]
128-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
129-
self.as_vec()
130-
}
131120
}
132121

133122
impl GenericPathUnsafe for Path {

src/libstd/path/windows.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,6 @@ impl<S: hash::Writer> hash::Hash<S> for Path {
144144
}
145145

146146
impl BytesContainer for Path {
147-
#[inline]
148-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
149-
self.as_vec()
150-
}
151-
#[inline]
152-
fn container_into_owned_bytes(self) -> Vec<u8> {
153-
self.into_vec()
154-
}
155-
#[inline]
156-
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
157-
self.as_str()
158-
}
159-
#[inline]
160-
fn is_str(_: Option<Path>) -> bool { true }
161-
}
162-
163-
impl<'a> BytesContainer for &'a Path {
164147
#[inline]
165148
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
166149
self.as_vec()
@@ -170,7 +153,7 @@ impl<'a> BytesContainer for &'a Path {
170153
self.as_str()
171154
}
172155
#[inline]
173-
fn is_str(_: Option<&'a Path>) -> bool { true }
156+
fn is_str(_: Option<&Path>) -> bool { true }
174157
}
175158

176159
impl GenericPathUnsafe for Path {

0 commit comments

Comments
 (0)