@@ -250,63 +250,12 @@ impl Uri {
250250 T : AsRef < [ u8 ] > + ' static ,
251251 {
252252 if_downcast_into ! ( T , Bytes , src, {
253- return Uri :: from_shared ( src) ;
253+ return Uri :: try_from ( src) ;
254254 } ) ;
255255
256256 Uri :: try_from ( src. as_ref ( ) )
257257 }
258258
259- // Not public while `bytes` is unstable.
260- fn from_shared ( s : Bytes ) -> Result < Uri , InvalidUri > {
261- use self :: ErrorKind :: * ;
262-
263- if s. len ( ) > MAX_LEN {
264- return Err ( TooLong . into ( ) ) ;
265- }
266-
267- match s. len ( ) {
268- 0 => {
269- return Err ( Empty . into ( ) ) ;
270- }
271- 1 => match s[ 0 ] {
272- b'/' => {
273- return Ok ( Uri {
274- scheme : Scheme :: empty ( ) ,
275- authority : Authority :: empty ( ) ,
276- path_and_query : PathAndQuery :: slash ( ) ,
277- } ) ;
278- }
279- b'*' => {
280- return Ok ( Uri {
281- scheme : Scheme :: empty ( ) ,
282- authority : Authority :: empty ( ) ,
283- path_and_query : PathAndQuery :: star ( ) ,
284- } ) ;
285- }
286- _ => {
287- let authority = Authority :: from_shared ( s) ?;
288-
289- return Ok ( Uri {
290- scheme : Scheme :: empty ( ) ,
291- authority : authority,
292- path_and_query : PathAndQuery :: empty ( ) ,
293- } ) ;
294- }
295- } ,
296- _ => { }
297- }
298-
299- if s[ 0 ] == b'/' {
300- return Ok ( Uri {
301- scheme : Scheme :: empty ( ) ,
302- authority : Authority :: empty ( ) ,
303- path_and_query : PathAndQuery :: from_shared ( s) ?,
304- } ) ;
305- }
306-
307- parse_full ( s)
308- }
309-
310259 /// Convert a `Uri` from a static string.
311260 ///
312261 /// This function will not perform any copying, however the string is
@@ -327,7 +276,7 @@ impl Uri {
327276 /// ```
328277 pub fn from_static ( src : & ' static str ) -> Self {
329278 let s = Bytes :: from_static ( src. as_bytes ( ) ) ;
330- match Uri :: from_shared ( s) {
279+ match Uri :: try_from ( s) {
331280 Ok ( uri) => uri,
332281 Err ( e) => panic ! ( "static str is not valid URI: {}" , e) ,
333282 }
@@ -675,12 +624,86 @@ impl Uri {
675624 }
676625}
677626
627+ impl TryFrom < Bytes > for Uri {
628+ type Error = InvalidUri ;
629+
630+ /// Attempt to convert a `Uri` from `Bytes`
631+ ///
632+ /// # Examples
633+ ///
634+ /// ```
635+ /// # extern crate http;
636+ /// # use http::uri::*;
637+ /// extern crate bytes;
638+ ///
639+ /// use std::convert::TryFrom;
640+ /// use bytes::Bytes;
641+ ///
642+ /// # pub fn main() {
643+ /// let bytes = Bytes::from("http://example.com/foo");
644+ /// let uri = Uri::try_from(bytes).unwrap();
645+ ///
646+ /// assert_eq!(uri.host().unwrap(), "example.com");
647+ /// assert_eq!(uri.path(), "/foo");
648+ /// # }
649+ /// ```
650+ fn try_from ( s : Bytes ) -> Result < Uri , Self :: Error > {
651+ use self :: ErrorKind :: * ;
652+
653+ if s. len ( ) > MAX_LEN {
654+ return Err ( TooLong . into ( ) ) ;
655+ }
656+
657+ match s. len ( ) {
658+ 0 => {
659+ return Err ( Empty . into ( ) ) ;
660+ }
661+ 1 => match s[ 0 ] {
662+ b'/' => {
663+ return Ok ( Uri {
664+ scheme : Scheme :: empty ( ) ,
665+ authority : Authority :: empty ( ) ,
666+ path_and_query : PathAndQuery :: slash ( ) ,
667+ } ) ;
668+ }
669+ b'*' => {
670+ return Ok ( Uri {
671+ scheme : Scheme :: empty ( ) ,
672+ authority : Authority :: empty ( ) ,
673+ path_and_query : PathAndQuery :: star ( ) ,
674+ } ) ;
675+ }
676+ _ => {
677+ let authority = Authority :: try_from ( s) ?;
678+
679+ return Ok ( Uri {
680+ scheme : Scheme :: empty ( ) ,
681+ authority : authority,
682+ path_and_query : PathAndQuery :: empty ( ) ,
683+ } ) ;
684+ }
685+ } ,
686+ _ => { }
687+ }
688+
689+ if s[ 0 ] == b'/' {
690+ return Ok ( Uri {
691+ scheme : Scheme :: empty ( ) ,
692+ authority : Authority :: empty ( ) ,
693+ path_and_query : PathAndQuery :: try_from ( s) ?,
694+ } ) ;
695+ }
696+
697+ parse_full ( s)
698+ }
699+ }
700+
678701impl < ' a > TryFrom < & ' a [ u8 ] > for Uri {
679702 type Error = InvalidUri ;
680703
681704 #[ inline]
682705 fn try_from ( t : & ' a [ u8 ] ) -> Result < Self , Self :: Error > {
683- Uri :: from_shared ( Bytes :: copy_from_slice ( t) )
706+ Uri :: try_from ( Bytes :: copy_from_slice ( t) )
684707 }
685708}
686709
@@ -707,7 +730,7 @@ impl TryFrom<String> for Uri {
707730
708731 #[ inline]
709732 fn try_from ( t : String ) -> Result < Self , Self :: Error > {
710- Uri :: from_shared ( Bytes :: from ( t) )
733+ Uri :: try_from ( Bytes :: from ( t) )
711734 }
712735}
713736
@@ -847,7 +870,7 @@ fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUri> {
847870 Ok ( Uri {
848871 scheme : scheme. into ( ) ,
849872 authority : authority,
850- path_and_query : PathAndQuery :: from_shared ( s) ?,
873+ path_and_query : PathAndQuery :: try_from ( s) ?,
851874 } )
852875}
853876
0 commit comments