Giter VIP home page Giter VIP logo

Comments (2)

Marwes avatar Marwes commented on June 14, 2024

Not a problem!

Consider Take which uses PhantomData for the I : RangeStream parameter since Rust requires that all type paramters of a type is actually used in the type itself.

pub struct Take<I>(usize, PhantomData<fn(I) -> I>);
impl<I> Parser for Take<I>
    where I: RangeStream,
          I::Range: ::primitives::Range
{
    type Input = I;
    type Output = I::Range;

    fn parse_lazy(&mut self, mut input: Self::Input) -> ConsumedResult<Self::Output, Self::Input> {
        ...
    }
}

So why can't we just have pub struct Take(usize); ?

Well, the Parser implementation uses I for its associated Input type and all associated types must be fully specified from the Self type (Take<I>). If I did not appear in Take Rust rejects the impl as you could have Take : Parser<Input = &str> and Take : Parser<Input = &[u8]> etc.

It is actually possible to be a bit more clever and move Input to a trait parameter instead as below which will let use remove the I parameter and I have actually tried doing that in the past. However it turns out that if Parser is implemented as below rust can start failing to infer the correct type for Input in not to uncommon cases and the only way to resolve that is to force users to add type annotations which is not particularly user friendly.

impl<I> Parser<I> for Take
    where I: RangeStream,
          I::Range: ::primitives::Range
{
    type Output = I::Range;

    fn parse_lazy(&mut self, mut input: I) -> ConsumedResult<Self::Output, I> {
        ...
    }
}

Anyways, that is the gist of it. Do you have any reason other than curiosity towards this? I feel its more of an implementation detail so I am not sure it belongs in the README since it is really just an implementation detail. (Not that implementation details are worth documenting. I guess as long as it is not front and center it can belong there).

from combine.

ncalexan avatar ncalexan commented on June 14, 2024

@Marwes wow, great answer. Thanks for being so responsive!

Do you have any reason other than curiosity towards this?

Not really. I wrote a small combinator and really had to dig into the source to understand some subtleties (confounded by this being my "learn Rust!" project), so I wanted to really understand what was going on in the guts.

I feel its more of an implementation detail so I am not sure it belongs in the README since it is really just an implementation detail. (Not that implementation details are worth documenting. I guess as long as it is not front and center it can belong there).

I'm fine with this. I prefer to phrase tickets/issues as "complete this action" or "solve this problem", so I phrased my question as "document this curiousity" rather than "answer my question". But you've answered my question and we can link to this discussion rather than fold it into the README as you see fit.

from combine.

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.