Skip to content

Commit 1cb8d1d

Browse files
author
Jorge Aparicio
committed
DSTify BytesContainer
1 parent 82045ca commit 1cb8d1d

File tree

3 files changed

+31
-54
lines changed

3 files changed

+31
-54
lines changed

src/libstd/path/mod.rs

+30-25
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ println!("path exists: {}", path.exists());
6868
#![experimental]
6969

7070
use collections::{Collection, MutableSeq};
71+
use core::kinds::Sized;
7172
use c_str::CString;
7273
use clone::Clone;
7374
use fmt;
@@ -627,7 +628,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
627628
/// ```
628629
#[inline]
629630
fn push_many<T: BytesContainer>(&mut self, paths: &[T]) {
630-
let t: Option<T> = None;
631+
let t: Option<&T> = None;
631632
if BytesContainer::is_str(t) {
632633
for p in paths.iter() {
633634
self.push(p.container_as_str().unwrap())
@@ -792,14 +793,9 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
792793
}
793794

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

814810
/// A trait that represents the unsafe operations on GenericPaths
@@ -860,48 +856,44 @@ impl<'a, P: GenericPath> Display<'a, P> {
860856
}
861857
}
862858

863-
impl<'a> BytesContainer for &'a str {
859+
impl BytesContainer for str {
864860
#[inline]
865-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
861+
fn container_as_bytes(&self) -> &[u8] {
866862
self.as_bytes()
867863
}
868864
#[inline]
869-
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
870-
Some(*self)
865+
fn container_as_str(&self) -> Option<&str> {
866+
Some(self)
871867
}
872868
#[inline]
873-
fn is_str(_: Option<&'a str>) -> bool { true }
869+
fn is_str(_: Option<&str>) -> bool { true }
874870
}
875871

876872
impl BytesContainer for String {
877873
#[inline]
878-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
874+
fn container_as_bytes(&self) -> &[u8] {
879875
self.as_bytes()
880876
}
881877
#[inline]
882-
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
878+
fn container_as_str(&self) -> Option<&str> {
883879
Some(self.as_slice())
884880
}
885881
#[inline]
886-
fn is_str(_: Option<String>) -> bool { true }
882+
fn is_str(_: Option<&String>) -> bool { true }
887883
}
888884

889-
impl<'a> BytesContainer for &'a [u8] {
885+
impl BytesContainer for [u8] {
890886
#[inline]
891-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
892-
*self
887+
fn container_as_bytes(&self) -> &[u8] {
888+
self
893889
}
894890
}
895891

896892
impl BytesContainer for Vec<u8> {
897893
#[inline]
898-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
894+
fn container_as_bytes(&self) -> &[u8] {
899895
self.as_slice()
900896
}
901-
#[inline]
902-
fn container_into_owned_bytes(self) -> Vec<u8> {
903-
self
904-
}
905897
}
906898

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

927932
#[inline(always)]

src/libstd/path/posix.rs

-11
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,6 @@ impl BytesContainer for Path {
130130
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
131131
self.as_vec()
132132
}
133-
#[inline]
134-
fn container_into_owned_bytes(self) -> Vec<u8> {
135-
self.into_vec()
136-
}
137-
}
138-
139-
impl<'a> BytesContainer for &'a Path {
140-
#[inline]
141-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
142-
self.as_vec()
143-
}
144133
}
145134

146135
impl GenericPathUnsafe for Path {

src/libstd/path/windows.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,6 @@ impl<S: hash::Writer> hash::Hash<S> for Path {
157157
}
158158

159159
impl BytesContainer for Path {
160-
#[inline]
161-
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
162-
self.as_vec()
163-
}
164-
#[inline]
165-
fn container_into_owned_bytes(self) -> Vec<u8> {
166-
self.into_vec()
167-
}
168-
#[inline]
169-
fn container_as_str<'a>(&'a self) -> Option<&'a str> {
170-
self.as_str()
171-
}
172-
#[inline]
173-
fn is_str(_: Option<Path>) -> bool { true }
174-
}
175-
176-
impl<'a> BytesContainer for &'a Path {
177160
#[inline]
178161
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
179162
self.as_vec()
@@ -183,7 +166,7 @@ impl<'a> BytesContainer for &'a Path {
183166
self.as_str()
184167
}
185168
#[inline]
186-
fn is_str(_: Option<&'a Path>) -> bool { true }
169+
fn is_str(_: Option<&Path>) -> bool { true }
187170
}
188171

189172
impl GenericPathUnsafe for Path {

0 commit comments

Comments
 (0)