Code Monkey home page Code Monkey logo

Comments (6)

kangalio avatar kangalio commented on July 17, 2024 1

The custom trait would also probably have to be async.

Probably, yes.

For 1, would it be a good idea to implement the custom trait for a few commonly used standard library types like the ints, floats, bool, String, etc., and some serenity models, and let the user implement the trait for other types they need?

With a blanket implementation impl<T: FromStr> ArgumentParse for T { ... }, all types with FromStr implemented would automatically have ArgumentParse implemented as well. This includes ints, floats, bool, String, even IpAddr, and also all third-party types with a FromStr implementation, for example url::Url or regex::Regex.


Anyways, I started to implement the trait but got hit by orphan rules complications:

error[E0119]: conflicting implementations of trait `argument::ArgumentParse` for type `serenity::model::guild::Member`:
  --> /home/kangalioo/dev/rust/_downloaded/serenity-framework/framework/src/argument.rs:34:1
   |
19 | impl<T: std::str::FromStr> ArgumentParse for T {
   | ---------------------------------------------- first implementation here
...
34 | impl ArgumentParse for Member {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `serenity::model::guild::Member`
   |
   = note: upstream crates may add a new impl of trait `std::str::FromStr` for type `serenity::model::guild::Member` in future versions

The only viable way to workaround this is to have the ArgumentParse trait and its implementations in the serenity crate itself (alternatives would be either waiting for specialization, or manually implementing ArgumentParse for all types, both of which have significant issues).

Does someone have a better idea? Otherwise I'd go ahead and create PRs for the serenity crate, and after that, for this crate to integrate with the new trait.

from framework.

arqunis avatar arqunis commented on July 17, 2024

To comment on the 1st way, if we went with that approach it would deny implementing the ArgumentParse trait on types that have a FromStr implementation, as they would conflict with one another. Specialisation would fix this, but it's currently unstable and unlikely to stabilise soon.

from framework.

kangalio avatar kangalio commented on July 17, 2024

Isn't it redundant anyways to implement the ArgumentParse trait on types with a FromStr implementation, when the blanket implementation exists?

from framework.

arqunis avatar arqunis commented on July 17, 2024

Yes, but if people wanted separate implementations for FromStr and ArgumentParse, this would prevent them from doing so. It's not a big concern, I'm just pointing an issue 1 could cause.

from framework.

kangalio avatar kangalio commented on July 17, 2024

I just realized that 2 is impossible, actually. Some parsing functions need extra context, for example parsing Member needs information about the members in a guild. Therefore, a custom trait is actually required, and it would probably need to look something like this:

pub trait ArgumentParse {
    type Err;
    fn parse(ctx: &crate::context::Context, msg: &Message, s: &str) -> Result<Self, Self::Err>;
}

impl<T: std::str::FromStr> ArgumentParse for T {
    type Err = <T as std::str::FromStr>::Err;
    fn parse(_ctx: &crate::context::Context, _msg: &Message, s: &str) -> Result<Self, Self::Err> {
        s.parse()
    }
}

from framework.

AriusX7 avatar AriusX7 commented on July 17, 2024

The custom trait would also probably have to be async.

For 1, would it be a good idea to implement the custom trait for a few commonly used standard library types like the ints, floats, bool, String, etc., and some serenity models, and let the user implement the trait for other types they need?

from framework.

Related Issues (13)

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.