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

Finish the pagination implementation #201

Merged
merged 13 commits into from
Apr 22, 2021
Merged

Finish the pagination implementation #201

merged 13 commits into from
Apr 22, 2021

Conversation

marioortizmanero
Copy link
Collaborator

@marioortizmanero marioortizmanero commented Apr 19, 2021

Description

This finishes what was left at #166

Closes #124

&self,
artist_id: &ArtistId,
album_type: Option<AlbumType>,
market: Option<Market>,
market: Option<&Market>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've had to make some parameters a reference, like Market, because when using automatic pagination we iterate multiple times and would have to clone the parameter.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, it makes sense. As I mentioned in another PR, it might be a chance to pass parameters by reference

@marioortizmanero marioortizmanero marked this pull request as draft April 19, 2021 15:43
&self,
playlist_id: &PlaylistId,
fields: Option<&str>,
market: Option<&Market>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

While I was at it I made these parameters consistent; limit and offset always go last.

@marioortizmanero
Copy link
Collaborator Author

I've also tried to implement a macro for these but completely failed at doing so. It's really complicated with macro_rules; if anything we would need a proc macro, and even in that case it might not be worth it. I'll just leave it for the future.

@marioortizmanero
Copy link
Collaborator Author

marioortizmanero commented Apr 19, 2021

I've added a pagination_chunks parameter to the client that configures the second parameter of pagination instead of hardcoding it to 50.

@marioortizmanero marioortizmanero marked this pull request as ready for review April 19, 2021 18:23
@marioortizmanero
Copy link
Collaborator Author

This should be ready for review now, @ramsayleung

@marioortizmanero marioortizmanero changed the title [WIP] Finish the pagination implementation Finish the pagination implementation Apr 19, 2021
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-users-saved-albums)
pub fn current_user_saved_albums(&self) -> impl Paginator<ClientResult<SavedAlbum>> + '_ {
Copy link
Owner

Choose a reason for hiding this comment

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

Is it necessary to have an anonymous lifetime annotation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yup, otherwise you get this:

   Compiling rspotify v0.10.0 (/home/mario/Programming/rspotify)
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirem
ent
    --> src/client.rs:1005:13
     |
1003 |     pub fn current_user_saved_albums(&self) -> impl Paginator<ClientResult<SavedAlbum>> {
     |                                      ----- this data with an anonymous lifetime `'_`...
1004 |         paginate(
1005 |             move |limit, offset| self.current_user_saved_albums_manual(Some(limit), Some(offset)),
     |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...is captured here...
     |
note: ...and is required to live as long as `'static` here
    --> src/client.rs:1003:48
     |
1003 |     pub fn current_user_saved_albums(&self) -> impl Paginator<ClientResult<SavedAlbum>> {
     |                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to declare that the `impl Trait` captures data from argument `self`, you can add an explicit `'_` l
ifetime bound
     |
1003 |     pub fn current_user_saved_albums(&self) -> impl Paginator<ClientResult<SavedAlbum>> + '_ {
     |                                                                                         ^^^^

error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirem
ent
    --> src/client.rs:1005:39
     |
1003 |     pub fn current_user_saved_albums(&self) -> impl Paginator<ClientResult<SavedAlbum>> {
     |                                      ----- this data with an anonymous lifetime `'_`...
1004 |         paginate(
1005 |             move |limit, offset| self.current_user_saved_albums_manual(Some(limit), Some(offset)),
     |                                  ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |                                  |
     |                                  ...is captured here...
     |
note: ...and is required to live as long as `'static` here
    --> src/client.rs:1003:48
     |
1003 |     pub fn current_user_saved_albums(&self) -> impl Paginator<ClientResult<SavedAlbum>> {
     |                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to declare that the `impl Trait` captures data from argument `self`, you can add an explicit `'_` l
ifetime bound
     |
1003 |     pub fn current_user_saved_albums(&self) -> impl Paginator<ClientResult<SavedAlbum>> + '_ {
     |                                                                                         ^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0759`.
error: could not compile `rspotify`

To learn more, run the command again with --verbose.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Wait, but I can remove it from the paginate function. Give me a sec.

Copy link
Owner

Choose a reason for hiding this comment

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

The self has an anonymous lifetime '_, in order to explicitly indicate the return value has can live as long as the self, so we just simply add an '_ for the return value? is it right?

Copy link
Collaborator Author

@marioortizmanero marioortizmanero Apr 21, 2021

Choose a reason for hiding this comment

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

Yup, the thing is that paginate captures self, which has a borrowed lifetime of '_. Thus, we must indicate that the returned value won't outlive self with + '_. I thought this was done automatically by the compiler but being an impl it makes sense that it's forcing us to specify the lifetime.

@ramsayleung
Copy link
Owner

Beside the small nitpicks I commented, everything else looks good to me. And I think it's ready to merge :)

@marioortizmanero
Copy link
Collaborator Author

Ok @ramsayleung I've relaxed the lifetimes as much as possible and the tests have passed. Take a look and merge if you're ok with this PR then.

@ramsayleung ramsayleung merged commit 81459de into master Apr 22, 2021
@ramsayleung ramsayleung deleted the finish-pagination branch April 22, 2021 01:29
@ramsayleung
Copy link
Owner

Merged :)

@icewind1991
Copy link
Contributor

Thanks for picking this up while I was busy/distracted!

@marioortizmanero
Copy link
Collaborator Author

No problem! If you have any concerns/suggestions about the implementation details, do let us know :)

@ramsayleung
Copy link
Owner

Thanks @icewind1991 and @marioortizmanero again for your great works :)

I just wrote a post to demonstrate this brilliant feature and how it works https://0x709394.me/Let's-make%20everything%20iterable

@marioortizmanero
Copy link
Collaborator Author

Great! I didn't know you had a blog, saved it. I plan on making one myself when we finally release 0.11 at https://nullderef.com :D

I enjoyed it, very visual. A few comments:

  • Shouldn't the fetch diagram increase the offset by 4 instead of by 1 per request?
  • Typo: First of all, the itrator pattern allows we -> First of all, the iterator pattern allows us

@ramsayleung
Copy link
Owner

ramsayleung commented Apr 29, 2021

Great! I didn't know you had a blog, saved it. I plan on making one myself when we finally release 0.11 at nullderef.com :D

Yep, we are stepping forward to v0.11. If you don't mind, I would like to add your blog to my blogroll :)

A few comments:

Oooh, Great catch, thanks for your heads-up.

@marioortizmanero
Copy link
Collaborator Author

Yep, we are stepping forward to v0.11. If you don't mind, I would like to add your blog to my blogroll :)

Cool! I don't have one yet but I'll definitely add you too when I do :)

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.

Add unlimited endpoints
3 participants