Skip to content

Commit

Permalink
impl WordSplitter for Box<T> where T: WordSplitter
Browse files Browse the repository at this point in the history
This allows more generic use, and e.g. as Box<dyn WordSplitter + Send>.
  • Loading branch information
sdroege committed Dec 13, 2020
1 parent 45dc705 commit 9c647bb
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/splitting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,13 @@ pub trait WordSplitter: std::fmt::Debug {
fn split_points(&self, word: &str) -> Vec<usize>;
}

impl WordSplitter for Box<dyn WordSplitter> {
impl<S: WordSplitter + ?Sized> WordSplitter for Box<S> {
fn split_points(&self, word: &str) -> Vec<usize> {
use std::ops::Deref;
self.deref().split_points(word)
}
}
/* Alternative, also adds impls for specific Box<S> i.e. Box<HyphenSplitter>
impl<S: WordSplitter + ?Sized> WordSplitter for Box<S> {
fn split<'w>(&self, word: &'w str) -> Vec<(&'w str, &'w str, &'w str)> {
use std::ops::Deref;
self.deref().split(word)
}
}
*/

impl<T: ?Sized + WordSplitter> WordSplitter for &T {
fn split_points(&self, word: &str) -> Vec<usize> {
(*self).split_points(word)
Expand Down

0 comments on commit 9c647bb

Please sign in to comment.