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

Introduce a InputSplit trait #1453

Closed
wants to merge 3 commits into from
Closed

Introduce a InputSplit trait #1453

wants to merge 3 commits into from

Conversation

Stargateur
Copy link
Contributor

@Stargateur Stargateur commented Oct 30, 2021

Closes #1450

I choice to introduce a new trait to allow a transition and avoid mistake. As we can see this produce a much better code for anychar() removing one step (maybe save CPU cycles)

This also allow to introduce a nice take_one() combinator by updating #1364 (we could also think about remove anychar and have a generic take_one() combinator)

Maybe change to just introduce a SplitFirst trait

@coveralls
Copy link

coveralls commented Oct 30, 2021

Coverage Status

Coverage increased (+0.2%) to 81.29% when pulling 0179f2f on Stargateur:split_fisrt into 02433c2 on Geal:main.

@Stargateur
Copy link
Contributor Author

Stargateur commented Oct 30, 2021

Maybe do:

/// Abstracts split_first
pub trait InputSplit: Sized {
  /// The current input type is a sequence of that `Item` type.
  ///
  /// Example: `u8` for `&[u8]` or `char` for `&str`
  type Item;

  /// Divides one input into two at an index.
  fn split_at(self, mid: usize) -> Result<(Self, Self), (Needed, Self)>;

  /// Returns the first and all the rest of the elements of the slice, or None if it is empty.
  fn split_first(self) -> Result<(Self::Item, Self), Self>;

  /// Returns the last and all the rest of the elements of the slice, or None if it is empty.
  fn split_last(self) -> Result<(Self::Item, Self), Self>;
}

Allowing consuming self. (that could allow nice implementation for VecDeque who would use a VecDeque for nom ? no idea or just an input that is costly to create)

@Stargateur Stargateur closed this Oct 30, 2021
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.

take_split return is not intuitive
2 participants