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

API change proposal for Songbird::join and Songbird::join_gateway #65

Closed
vilgotf opened this issue Apr 19, 2021 · 5 comments
Closed

API change proposal for Songbird::join and Songbird::join_gateway #65

vilgotf opened this issue Apr 19, 2021 · 5 comments
Labels
feature-request Request for addition of a new feature or improvement. gateway Relates to the gateway feature (Discord's gateway integration).

Comments

@vilgotf
Copy link
Contributor

vilgotf commented Apr 19, 2021

Not being able to use ? on Songbird::join and Songbird::join_gateway is bothering me, so I propose that join takes a call as an argument instead of returning a new one.

so

fn join(songbird: Songbird, channel_id: ChannelId, guild_id: GuildId) -> Result<()> {
  let (_, res) = songbird.join(guild_id, channel_id).await;
  res
}

would be

fn join(songbird: Songbird, channel_id: ChannelId, guild_id: GuildId) -> Result<()> {
  let call = songbird.get_or_insert(guild_id);
  songbird.join(channel_id, call).await
}

Thoughts?

@vilgotf
Copy link
Contributor Author

vilgotf commented Apr 19, 2021

As a real world example
This:

async fn _join(&self, channel_id: ChannelId) -> Result<()> {
  let call = self.bot.songbird.get_or_insert(self.guild_id.into());
  {
    let mut lock = call.lock().await;
    if !lock.is_deaf() {
      lock.deafen(true).await?
    }
  }
  let (_, result) = self.bot.songbird.join(self.guild_id, channel_id).await;
  result

would turn into this:

async fn _join(&self, channel_id: ChannelId) -> Result<()> {
  let call = self.bot.songbird.get_or_insert(self.guild_id.into());
  {
    let mut lock = call.lock().await;
    if !lock.is_deaf() {
      lock.deafen(true).await?
    }
  }
  self.bot.songbird.join(channel_id, call).await
}

@FelixMcFelix
Copy link
Member

I agree that being unable to just use ? to shortcut the return values for these methods is pretty bad. However, I think these suggestions also hurt ergonomics; users are now forced to get a Call before attempting to join a channel, at which point the Songbird manager object isn't actually needed any more from a functionality point of view: it just handles consistent lookup if GuildIDs->Calls, and this API might trick users into trying to make their own standalone Calls.

There might be cause for moving out the logic in Songbird::join/join_gateway into an associated function on Call (namely the risk of deadlock due to lock misuse):

impl Call {
    pub fn join_from_lock(self: &Arc<Mutex<Self>>) -> JoinResult<()> {
        // Safe handling of lock, await one, unlock, await other...
    }
}

...and then pushing users to prefer Songbird::join == Call::join_from_lock > Call::join.

@vilgotf
Copy link
Contributor Author

vilgotf commented Apr 20, 2021

Didn't think about new users fallaciously using Call::standalone()... I think a safe method on Call would be equally good although it kinda feels like a hack (why have a safe and an unsafe method?)

@FelixMcFelix
Copy link
Member

FelixMcFelix commented Apr 20, 2021

It's not an unsafe method; there are just caveats to using a Call directly, and we can't assume that every user will have it in an Arc<Mutex<...>>. Join/join_gateway methods which act on the objects themselves cover this niche.

@FelixMcFelix FelixMcFelix added feature-request Request for addition of a new feature or improvement. gateway Relates to the gateway feature (Discord's gateway integration). labels Apr 20, 2021
@vilgotf
Copy link
Contributor Author

vilgotf commented May 25, 2021

Another option is to not return Call

FelixMcFelix added a commit to FelixMcFelix/songbird that referenced this issue Jan 9, 2023
Replaces the annoying dual-return (i.e., created `Call` *and* `Result<x>`) with a single `Return<Call/ConnectionInfo>`. Users are now informed via that a `Call` is created -- thus, cleanup in event of connection failure is now their responsibility.

Tested using `cargo make ready`.

Closes serenity-rs#65.
FelixMcFelix added a commit that referenced this issue Jan 9, 2023
Replaces the annoying dual-return (i.e., created `Call` *and* `Result<x>`) with a single `Return<Call/ConnectionInfo>`. Users are now informed via that a `Call` is created -- thus, cleanup in event of connection failure is now their responsibility.

Tested using `cargo make ready`.

Closes #65.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for addition of a new feature or improvement. gateway Relates to the gateway feature (Discord's gateway integration).
Projects
None yet
Development

No branches or pull requests

2 participants