1010
1111use  std:: fmt; 
1212use  std:: default:: Default ; 
13- use  std:: hash; 
14- use  std:: { mem,  raw,  ptr,  slice,  vec} ; 
15- use  std:: rt:: heap:: EMPTY ; 
13+ use  std:: vec; 
1614use  serialize:: { Encodable ,  Decodable ,  Encoder ,  Decoder } ; 
1715
18- /// A non-growable owned slice. This would preferably become `~[T]`  
19- /// under DST . 
20- #[ unsafe_no_drop_flag ]    // data is set to null on destruction 
16+ /// A non-growable owned slice. This is a separate type to allow the  
17+ /// representation to change . 
18+ #[ deriving ( Hash ,   PartialEq ,   Eq ,   PartialOrd ,   Ord ) ]  
2119pub  struct  OwnedSlice < T >  { 
22-  /// null iff len == 0 
23-   data :  * mut  T , 
24-  len :  uint , 
20+  data :  Box < [ T ] > 
2521} 
2622
2723impl < T : fmt:: Show >  fmt:: Show  for  OwnedSlice < T >  { 
2824 fn  fmt ( & self ,  fmt :  & mut  fmt:: Formatter )  -> fmt:: Result  { 
29-  try!( "OwnedSlice {{" . fmt ( fmt) ) ; 
30-  for  i in  self . iter ( )  { 
31-  try!( i. fmt ( fmt) ) ; 
32-  } 
33-  try!( "}}" . fmt ( fmt) ) ; 
34-  Ok ( ( ) ) 
35-  } 
36- } 
37- 
38- #[ unsafe_destructor]  
39- impl < T >  Drop  for  OwnedSlice < T >  { 
40-  fn  drop ( & mut  self )  { 
41-  if  self . data . is_null ( )  {  return  } 
42- 
43-  // extract the vector 
44-  let  v = mem:: replace ( self ,  OwnedSlice :: empty ( ) ) ; 
45-  // free via the Vec destructor 
46-  v. into_vec ( ) ; 
25+  self . data . fmt ( fmt) 
4726 } 
4827} 
4928
5029impl < T >  OwnedSlice < T >  { 
5130 pub  fn  empty ( )  -> OwnedSlice < T >  { 
52-  OwnedSlice  {  data :  ptr :: null_mut ( ) ,   len :   0  } 
31+  OwnedSlice  {  data :  box  [ ]  } 
5332 } 
5433
5534 #[ inline( never) ]  
56-  pub  fn  from_vec ( mut  v :  Vec < T > )  -> OwnedSlice < T >  { 
57-  let  len = v. len ( ) ; 
58- 
59-  if  len == 0  { 
60-  OwnedSlice :: empty ( ) 
61-  }  else  { 
62-  // drop excess capacity to avoid breaking sized deallocation 
63-  v. shrink_to_fit ( ) ; 
64- 
65-  let  p = v. as_mut_ptr ( ) ; 
66-  // we own the allocation now 
67-  unsafe  {  mem:: forget ( v)  } 
68- 
69-  OwnedSlice  {  data :  p,  len :  len } 
70-  } 
35+  pub  fn  from_vec ( v :  Vec < T > )  -> OwnedSlice < T >  { 
36+  OwnedSlice  {  data :  v. into_boxed_slice ( )  } 
7137 } 
7238
7339 #[ inline( never) ]  
7440 pub  fn  into_vec ( self )  -> Vec < T >  { 
75-  // null is ok, because len == 0 in that case, as required by Vec. 
76-  unsafe  { 
77-  let  ret = Vec :: from_raw_parts ( self . data ,  self . len ,  self . len ) ; 
78-  // the vector owns the allocation now 
79-  mem:: forget ( self ) ; 
80-  ret
81-  } 
41+  self . data . into_vec ( ) 
8242 } 
8343
8444 pub  fn  as_slice < ' a > ( & ' a  self )  -> & ' a  [ T ]  { 
85-  let  ptr = if  self . data . is_null ( )  { 
86-  // length zero, i.e. this will never be read as a T. 
87-  EMPTY  as  * const  T 
88-  }  else  { 
89-  self . data  as  * const  T 
90-  } ; 
91- 
92-  let  slice:  & [ T ]  = unsafe  { mem:: transmute ( raw:: Slice  { 
93-  data :  ptr, 
94-  len :  self . len 
95-  } ) } ; 
96- 
97-  slice
98-  } 
99- 
100-  pub  fn  get < ' a > ( & ' a  self ,  i :  uint )  -> & ' a  T  { 
101-  self . as_slice ( ) . get ( i) . expect ( "OwnedSlice: index out of bounds" ) 
102-  } 
103- 
104-  pub  fn  iter < ' r > ( & ' r  self )  -> slice:: Items < ' r ,  T >  { 
105-  self . as_slice ( ) . iter ( ) 
45+  & * self . data 
10646 } 
10747
10848 pub  fn  move_iter ( self )  -> vec:: MoveItems < T >  { 
@@ -112,10 +52,12 @@ impl<T> OwnedSlice<T> {
11252 pub  fn  map < U > ( & self ,  f:  |& T | -> U )  -> OwnedSlice < U >  { 
11353 self . iter ( ) . map ( f) . collect ( ) 
11454 } 
55+ } 
11556
116-  pub  fn  len ( & self )  -> uint  {  self . len  } 
117- 
118-  pub  fn  is_empty ( & self )  -> bool  {  self . len  == 0  } 
57+ impl < T >  Deref < [ T ] >  for  OwnedSlice < T >  { 
58+  fn  deref ( & self )  -> & [ T ]  { 
59+  self . as_slice ( ) 
60+  } 
11961} 
12062
12163impl < T >  Default  for  OwnedSlice < T >  { 
@@ -130,20 +72,6 @@ impl<T: Clone> Clone for OwnedSlice<T> {
13072 } 
13173} 
13274
133- impl < S :  hash:: Writer ,  T :  hash:: Hash < S > >  hash:: Hash < S >  for  OwnedSlice < T >  { 
134-  fn  hash ( & self ,  state :  & mut  S )  { 
135-  self . as_slice ( ) . hash ( state) 
136-  } 
137- } 
138- 
139- impl < T :  PartialEq >  PartialEq  for  OwnedSlice < T >  { 
140-  fn  eq ( & self ,  other :  & OwnedSlice < T > )  -> bool  { 
141-  self . as_slice ( )  == other. as_slice ( ) 
142-  } 
143- } 
144- 
145- impl < T :  Eq >  Eq  for  OwnedSlice < T >  { } 
146- 
14775impl < T >  FromIterator < T >  for  OwnedSlice < T >  { 
14876 fn  from_iter < I :  Iterator < T > > ( mut  iter :  I )  -> OwnedSlice < T >  { 
14977 OwnedSlice :: from_vec ( iter. collect ( ) ) 
0 commit comments