Git Product home page Git Product logo

Comments (5)

vilgotf avatar vilgotf commented on August 17, 2024

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
}

from songbird.

FelixMcFelix avatar FelixMcFelix commented on August 17, 2024

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.

from songbird.

vilgotf avatar vilgotf commented on August 17, 2024

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?)

from songbird.

FelixMcFelix avatar FelixMcFelix commented on August 17, 2024

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.

from songbird.

vilgotf avatar vilgotf commented on August 17, 2024

Another option is to not return Call

from songbird.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.