-
Notifications
You must be signed in to change notification settings - Fork 39
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
API: substitute Result<Future..>
with Future..
for more idiomatic future chaining
#49
API: substitute Result<Future..>
with Future..
for more idiomatic future chaining
#49
Conversation
2f36b4c
to
2a18364
Compare
Result<Future*>
with Future*
for more idiomatic future chaining
src/v2/auth.rs
Outdated
let subclient = self.hclient.clone(); | ||
let creds = self.credentials.clone(); | ||
let scope = scopes | ||
.iter() | ||
.fold("".to_string(), |acc, &s| acc + "&scope=" + s); | ||
let auth = self | ||
.get_token_provider()? | ||
.get_token_provider() | ||
.unwrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FIXME
src/v2/mod.rs
Outdated
let url = try!(hyper::Uri::from_str( | ||
(self.base_url.clone() + "/v2/").as_str() | ||
)); | ||
let url = hyper::Uri::from_str((self.base_url.clone() + "/v2/").as_str()).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FIXME
Result<Future*>
with Future*
for more idiomatic future chainingResult<Future..>
with Future..
for more idiomatic future chaining
I would say that yes, we should try to drop the outer |
4f5a625
to
d1bdb2a
Compare
I've changed all methods referenced by the examples. A first review pass would make sense now ;-) |
64255ef
to
8b7b13f
Compare
examples/image.rs
Outdated
.and_then(|dclient| { | ||
dclient.is_v2_supported().and_then(|v2_supported| { | ||
if !v2_supported { | ||
return Err("API v2 not supported".into()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can drop the explicit return
here (or flatten the else branch).
examples/image.rs
Outdated
}).and_then(|dclient| { | ||
dclient.is_auth(None).and_then(|is_auth| { | ||
if is_auth { | ||
return Err("no login performed, but already authenticated".into()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
examples/image.rs
Outdated
} | ||
dkregistry::mediatypes::MediaTypes::ManifestV2S2 => { | ||
let m: dkregistry::v2::manifest::ManifestSchema2 = | ||
serde_json::from_slice(manifest_body.as_slice()).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two leftover unwraps here and in the match-arm above.
examples/login.rs
Outdated
extern crate tokio_core; | ||
|
||
use std::{boxed, error}; | ||
use tokio_core::reactor::Core; | ||
|
||
use futures::future::Future; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use the prelude here too.
examples/tags.rs
Outdated
}) | ||
}).and_then(|dclient| { | ||
dclient | ||
.login(&[&format!("repository:{}:pull", image)]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would extract the scope-formatting outside (before) the start of the chain, so that it's harder for people to miss (here and everywhere else too).
examples/tags.rs
Outdated
} else { | ||
println!("logged in!"); | ||
Ok(dclient | ||
.clone() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this clone really needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because:
error[E0596]: cannot borrow immutable captured outer variable in an `FnOnce` closure `dclient` as mutable
--> examples/tags.rs:77:36
|
77 | Ok(dclient
| ^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0596`.
error: Could not compile `dkregistry`.
src/v2/blobs.rs
Outdated
let cl = self.clone(); | ||
let url = { | ||
let ep = format!("{}/v2/{}/blobs/{}", self.base_url.clone(), name, digest); | ||
hyper::Uri::from_str(ep.as_str())? | ||
hyper::Uri::from_str(ep.as_str()).unwrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftover unwrap (there are a few more below without a fixme note).
d4199d0
to
10aafe8
Compare
Before this commit all methods return a `Result<Future..>` chaining them requires to `unwrap()` or slightly milder `unwrap_or_else()` for *every* additional chain element, which adds significant boilerplate for the consumer. This commit aims to aid with this by removing the outer `Result<Future..>` and return `Future..` for all async public methods.
10aafe8
to
363baf3
Compare
Result<Future..>
with Future..
for more idiomatic future chainingResult<Future..>
with Future..
for more idiomatic future chaining
363baf3
to
8ac4299
Compare
src/v2/manifest/mod.rs
Outdated
"{}/v2/{}/manifests/{}", | ||
self.base_url.clone(), | ||
name, | ||
reference | ||
))); | ||
)).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftover unwrap.
src/v2/manifest/mod.rs
Outdated
.append(header::ACCEPT, header::HeaderValue::from_str(&mtype)?); | ||
r.headers_mut().append( | ||
header::ACCEPT, | ||
header::HeaderValue::from_str(&mtype).unwrap(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftover unwrap.
src/v2/manifest/mod.rs
Outdated
let url = { | ||
let ep = format!( | ||
"{}/v2/{}/manifests/{}", | ||
self.base_url.clone(), | ||
name, | ||
reference | ||
); | ||
try!(hyper::Uri::from_str(ep.as_str())) | ||
hyper::Uri::from_str(ep.as_str()).unwrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftover unwrap.
src/v2/manifest/mod.rs
Outdated
}; | ||
let accept_types = match mediatypes { | ||
None => vec![mediatypes::MediaTypes::ManifestV2S2.to_mime()], | ||
Some(ref v) => try!(to_mimes(v)), | ||
Some(ref v) => to_mimes(v).unwrap(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftover unwrap.
src/v2/tags.rs
Outdated
let url = { | ||
let mut s = format!("{}/v2/{}/tags/list", self.base_url, name); | ||
if let Some(n) = paginate { | ||
s = s + &format!("?n={}", n); | ||
}; | ||
try!(hyper::Uri::from_str(s.as_str())) | ||
hyper::Uri::from_str(s.as_str()).unwrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leftover unwrap.
@steveej this looks pretty good to me. There are only five leftover unwraps and then I think it's done. |
Yay, more unwraps to clear up 🎂 Thanks for catching these |
8498ddc
to
8452a89
Compare
d2bf4ac
to
a1fc31b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
`git grep -e 'unwrap()' --and --not -e '^//' -n src/` doesn't indicate any occurences left.
a1fc31b
to
8f0dabe
Compare
This change is motivated by an out-of-band discussion with @lucab and a comment on a related PR openshift/cincinnati#4 (comment) by @crawford, suggesting to use future chaining.
Before this PR all methods return a
Result<Future..>
chaining them requires tounwrap()
or slightly milderunwrap_or_else()
for every additional chain element, which adds significant boilerplate for the consumer. This PR aims to aid with this by removing the outerResult<Future..>
and returnFuture..
for all async public methods.I'd like to discuss this API breaking change with the intention to move forward with this or a similar approach for the whole crate. I've done a rewrite of the login example demonstrates the usage of the changed API.
examples/checkregistry.rs