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

Add WordSeparator trait to allow customizing how words are found in a line of text #332

Merged
merged 1 commit into from
May 2, 2021

Conversation

mgeisler
Copy link
Owner

@mgeisler mgeisler commented May 1, 2021

The new trait is responsible for turning a line of text into an iterator of Words. The first implementation is AsciiSpace, which
simply splits the input text on ' '. This matches the current behavior.

The new trait shows up as an extra generic parameter on Options. This is quite invasive in the case of dynamic dispatch: when you could write

let options: Options = Options::with_splitter(80, NoHyphenation);

before to create a Options<Box<dyn WordSplitter>>, you now need to cast two types to trait objects to achive the same thing:

let mut options = Options::new(80)
    .splitter(Box::new(HyphenSplitter) as Box<dyn WordSplitter>)
    .word_separator(Box::new(AsciiSpace) as Box<dyn WordSeparator>);

@mgeisler mgeisler force-pushed the word-separator-trait branch 7 times, most recently from 430b181 to 7f5c9c0 Compare May 2, 2021 12:59
@mgeisler mgeisler changed the title Add WordSeparator trait which defines how words are found Add WordSeparator trait to allow customizing how words are found in a line of text May 2, 2021
The new trait is responsible for turning a line of text into an
iterator of `Word`s. The first implementation is `AsciiSpace`, which
simply splits the input text on `' '`. This matches the current
behavior.

The new trait shows up as an extra generic parameter on `Options`.
This is quite invasive in the case of dynamic dispatch: when you could
write

```rust
let options: Options = Options::with_splitter(80, NoHyphenation);
```

before to create a `Options<Box<dyn WordSplitter>>`, you now need to
cast two types to trait objects to achive the same thing:

```rust
let mut options = Options::new(80)
    .splitter(Box::new(HyphenSplitter) as Box<dyn WordSplitter>)
    .word_separator(Box::new(AsciiSpace) as Box<dyn WordSeparator>);
```
@mgeisler mgeisler enabled auto-merge May 2, 2021 13:03
@mgeisler mgeisler merged commit 48b9480 into master May 2, 2021
@mgeisler mgeisler deleted the word-separator-trait branch May 2, 2021 13:07
@mgeisler mgeisler linked an issue May 2, 2021 that may be closed by this pull request
This was referenced May 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.

Add a trait for finding words
1 participant