Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Into types #28

Closed
wants to merge 3 commits into from
Closed

Into types #28

wants to merge 3 commits into from

Conversation

yoshuawuyts
Copy link
Member

This makes creating Request and Response easier by taking auto-converting arguments:

Example

let mut res = Response::new(200)?;
let mut res = Response::new(StatusCode::NotFound)?;

let url = Url::parse("https://example.com")?;
let mut req = Request::new(Method::Get, url)?;

let url = Url::parse("https://example.com")?;
let mut req = Request::new("GET", url)?;

Caveats

Unfortunately TryFrom is not yet implemented for url, so I've filed servo/rust-url#568. A workaround for this would be to implement this using new types so we can implement the trait ourselves. This would then enable us to write:

let mut res = Response::new(200)?;
let mut req = Request::new("GET", "https://example.com")?;

Signed-off-by: Yoshua Wuyts <[email protected]>
Signed-off-by: Yoshua Wuyts <[email protected]>
Signed-off-by: Yoshua Wuyts <[email protected]>
@rylev
Copy link
Collaborator

rylev commented Dec 5, 2019

Hmmm it feels weird that an operation that you know will not fail returns a Result. Response::new(StatusCode::NotFound) will not fail but because of new being able to take arbitrary u16s it now must return a Result. This makes sense but is strange. Would the following work and be acceptable?

Response::new(404.try_into()?);

@yoshuawuyts
Copy link
Member Author

@rylev heh, I mean it could work of course, but I'd like us to try the other version first. The reason why I'm going down this path is because I'm feeling quite inspired by how Rust does handles sockets. If you think of it it's extremely flexible but also ergonomic. The following are all valid:

TcpStream::connect("127.0.0.1:80")?;
TcpStream::connect("localhost:80")?;
TcpStream::connect(("127.0.0.1", 80))?;
TcpStream::connect((Ipv4Addr::LOCALHOST, 80))?;
TcpStream::connect(["127.0.0.1:80", "127.0.0.1:81", "127.0.0.1:82"])?;
TcpStream::connect(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))?;
TcpStream::connect(Ipv4Addr::new(127, 0, 0, 1))?;

And all of the values passed in can in turn be constructed from other values. I've got a hunch that if we can provide flexibility at this API layer and propagate it, people will come to appreciate it quite a bit. Of course it's not certain, but I'd like for us to try if possible.

@rylev
Copy link
Collaborator

rylev commented Dec 11, 2019

@yoshuawuyts and I have decided to close this PR because it's offering a bit too high level of an API that would be more appropriate for higher level crates.

@rylev rylev closed this Dec 11, 2019
@rylev rylev deleted the into-types branch December 11, 2019 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants