When using the url crate https://github.com/servo/rust-url together with hyper I always have to do something like this:
let uri: hyper::Uri = url.into_string().parse()?;
Either provide an impl From<url::Url> for hyper::Uri
or switch to using the servo Url implementation internally. In my opinion having 2 different Url types which basically do the same thing is not only annoying to work with, it is also confusing for newcomers.
The code example you pasted is probably the way to do this, for now.
The reason for having a separate type is simple: all valid URI
s are not valid URL
s. A client may only care about using a Url
in 99% of cases, but a server needs to handle more cases.
Consider these cases, which are all valid Uri
s, but not Url
s:
*
(like OPTIONS * HTTP/1.1
)/
(like GET / HTTP/1.1
)www.example.com
(like CONNECT www.example.com HTTP/1.1
)Additionally, the layout of a URI is different in HTTP2, so having the Uri
type in hyper allows adjusting for that.
As for a conversion, perhaps. I'm not 100% against it, but the reason so far is that url
is a big dependency just to provide a single little From
implementation. It so far seems like hyper shouldn't. The conversion you pasted should be fine. It shouldn't ever error (if it does, likely a bug in Uri
or Url
), and it's really not that expensive. Parsing a Uri
is much cheaper than parsing Url
.
See #1089 for more.
Similar issue I raised before: https://github.com/hyperium/hyper/issues/1102
Although I understand the motivation for removing Url
, it seems users are feeling some pain in not having impl From<url::Url> for hyper::Uri
.
hyper is changing to just using http::Uri
, so this isn't actionable in hyper proper. Closing.
Most helpful comment
Similar issue I raised before: https://github.com/hyperium/hyper/issues/1102
Although I understand the motivation for removing
Url
, it seems users are feeling some pain in not havingimpl From<url::Url> for hyper::Uri
.