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

change Bucket::new() Implementation #14

Merged
merged 6 commits into from Jun 15, 2021

Conversation

SKTT1Ryze
Copy link
Contributor

For the Bucket::new() func, I think the better implementation is use the generic S: AsRef<str>, which will improve the experience of the users using this crate, because they can specify the parameters with both the String and &str.
So I create this pull request.
hope for being merged, thanks.

Copy link
Owner

@paolobarbolini paolobarbolini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I left a few suggestions. Also, let's see what @guerinoni thinks of it

src/bucket.rs Outdated
@@ -49,20 +49,21 @@ pub struct Bucket {

impl Bucket {
/// Construct a new S3 bucket
pub fn new(endpoint: Url, path_style: bool, name: String, region: String) -> Option<Self> {
// 这里可以有 pr
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you revert this doc change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

src/bucket.rs Outdated
@@ -49,20 +49,21 @@ pub struct Bucket {

impl Bucket {
/// Construct a new S3 bucket
pub fn new(endpoint: Url, path_style: bool, name: String, region: String) -> Option<Self> {
// 这里可以有 pr
pub fn new<S: AsRef<str>>(endpoint: Url, path_style: bool, name: S, region: S) -> Option<Self> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these get converted to String later in the function it would make sense to take ToString or Into<String>. I'd also go for a different generic types between name and region, since they could originate from different sources with different types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, your opinion makes sense. I will think about it.
It's 4:00 in my time zone now, I need to sleep. Tomorrow I will back to discuss this implementation.
Thanks for your patience.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I'm back.
In my opinion, the role of Bucket::new is to constructt a new S3 bucket, the name and region will be converted to String In the end. So the problem is when we convert them.
The old implementation requested the API user to specify the two parameters as String, while the new implementation allow the user use String or &str or any other types which implement AsRef<str>, and the job of conversion is placed inside Bucket::new function.
So I think the advantage of the new implementation is increasing flexibility of the code, even if it brings some additional cost of runtime. Compared to the little additional cost, I prefer the greater flexibility.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Into<String> would be able to achieve the same thing as AsRef<str> though, but with the advantage of not allocating if the generic type were to be a String.

Here's a demo to demonstrate it:

Both cases are able to accept both String and &str, but the second one is better because it doesn't make a new allocation if the type is a String

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! I have fixed it. Thanks for your suggestions and patience!

@SKTT1Ryze
Copy link
Contributor Author

It seems that pr didn't pass the coverage CI, but I can't find the reason. What should I do to fix the code?

@paolobarbolini
Copy link
Owner

It seems that pr didn't pass the coverage CI, but I can't find the reason. What should I do to fix the code?

It's fine to leave it as is. Something in the configuration probably broke it

@guerinoni
Copy link
Collaborator

For me it's ok, very nice implementation with Into<String> and happy to have a ctor that takes String and str... Very handy!!
Now I'm trying to update codecov action with latest version because I saw many improvements and maybe will fix coverage with simple rebase :)

@paolobarbolini paolobarbolini merged commit 8e412df into paolobarbolini:main Jun 15, 2021
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.

None yet

3 participants