Skip to content

Commit

Permalink
drop superfluous trait bounds (#19)
Browse files Browse the repository at this point in the history
* remove trait bounds from `Interner`

it's recommended to leave them out of struct definitions and instead
have them in impl blocks

https://stackoverflow.com/questions/49229332/should-trait-bounds-be-duplicated-in-struct-and-impl

* move methods not requiring the bounds to another impl block

* do the same for `InternedInput`

arguably less impactful, but nice-to-have nevertheless

* remove trait bounds from `InternedInput`

this was made ppossible by removing them from `Interner`

* remove trait bounds from `diff`

this was made possible by all the previous commits -- `InternedInput`
now doesn't require the traits by itself, nor does
`Interner::num_tokens`

* remove trait bounds from `UnifiedDiffBuilder` previously required by `Interner`
  • Loading branch information
ada4a authored Dec 15, 2024
1 parent fd5913f commit 226742f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
34 changes: 19 additions & 15 deletions src/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,20 @@ pub trait TokenSource {
///
/// While you can intern tokens yourself it is strongly recommended to use [`InternedInput`] module.
#[derive(Default)]
pub struct InternedInput<T: Eq + Hash> {
pub struct InternedInput<T> {
pub before: Vec<Token>,
pub after: Vec<Token>,
pub interner: Interner<T>,
}

impl<T> InternedInput<T> {
pub fn clear(&mut self) {
self.before.clear();
self.after.clear();
self.interner.clear();
}
}

impl<T: Eq + Hash> InternedInput<T> {
pub fn new<I: TokenSource<Token = T>>(before: I, after: I) -> Self {
let token_estimate_before = before.estimate_tokens() as usize;
Expand Down Expand Up @@ -86,23 +94,17 @@ impl<T: Eq + Hash> InternedInput<T> {
self.after
.extend(input.map(|token| self.interner.intern(token)));
}

pub fn clear(&mut self) {
self.before.clear();
self.after.clear();
self.interner.clear();
}
}

/// An interner that allows for fast access of tokens produced by a [`TokenSource`].
#[derive(Default)]
pub struct Interner<T: Hash + Eq> {
pub struct Interner<T> {
tokens: Vec<T>,
table: HashTable<Token>,
hasher: RandomState,
}

impl<T: Hash + Eq> Interner<T> {
impl<T> Interner<T> {
/// Create an Interner with an initial capacity calculated by summing the results of calling
/// [`estimate_tokens`](crate::intern::TokenSource::estimate_tokens) methods of `before` and `after`.
pub fn new_for_token_source<S: TokenSource<Token = T>>(before: &S, after: &S) -> Self {
Expand All @@ -124,6 +126,13 @@ impl<T: Hash + Eq> Interner<T> {
self.tokens.clear();
}

/// Returns to total number of **distinct** tokens currently interned.
pub fn num_tokens(&self) -> u32 {
self.tokens.len() as u32
}
}

impl<T: Hash + Eq> Interner<T> {
/// Intern `token` and return a the interned integer.
pub fn intern(&mut self, token: T) -> Token {
let hash = self.hasher.hash_one(&token);
Expand All @@ -142,11 +151,6 @@ impl<T: Hash + Eq> Interner<T> {
}
}

/// Returns to total number of **distinct** tokens currently interned.
pub fn num_tokens(&self) -> u32 {
self.tokens.len() as u32
}

/// Erases `first_erased_token` and any tokens interned afterward from the interner.
pub fn erase_tokens_after(&mut self, first_erased_token: Token) {
assert!(first_erased_token.0 <= self.tokens.len() as u32);
Expand Down Expand Up @@ -176,7 +180,7 @@ impl<T: Hash + Eq> Interner<T> {
}
}

impl<T: Hash + Eq> Index<Token> for Interner<T> {
impl<T> Index<Token> for Interner<T> {
type Output = T;
fn index(&self, index: Token) -> &Self::Output {
&self.tokens[index.0 as usize]
Expand Down
8 changes: 1 addition & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@
//! assert_eq!(changes.removals, 1);
//! ```
use std::hash::Hash;

#[cfg(feature = "unified_diff")]
pub use unified_diff::UnifiedDiffBuilder;

Expand Down Expand Up @@ -234,11 +232,7 @@ impl Algorithm {
/// Computes an edit-script that transforms `input.before` into `input.after` using
/// the specified `algorithm`
/// The edit-script is passed to `sink.process_change` while it is produced.
pub fn diff<S: Sink, T: Eq + Hash>(
algorithm: Algorithm,
input: &InternedInput<T>,
sink: S,
) -> S::Out {
pub fn diff<S: Sink, T>(algorithm: Algorithm, input: &InternedInput<T>, sink: S) -> S::Out {
diff_with_tokens(
algorithm,
&input.before,
Expand Down
9 changes: 4 additions & 5 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::fmt::{Display, Write};
use std::hash::Hash;
use std::ops::Range;

use crate::intern::{InternedInput, Interner, Token};
Expand All @@ -10,7 +9,7 @@ use crate::Sink;
pub struct UnifiedDiffBuilder<'a, W, T>
where
W: Write,
T: Hash + Eq + Display,
T: Display,
{
before: &'a [Token],
after: &'a [Token],
Expand All @@ -28,7 +27,7 @@ where

impl<'a, T> UnifiedDiffBuilder<'a, String, T>
where
T: Hash + Eq + Display,
T: Display,
{
/// Create a new `UnifiedDiffBuilder` for the given `input`,
/// that will return a [`String`].
Expand All @@ -51,7 +50,7 @@ where
impl<'a, W, T> UnifiedDiffBuilder<'a, W, T>
where
W: Write,
T: Hash + Eq + Display,
T: Display,
{
/// Create a new `UnifiedDiffBuilder` for the given `input`,
/// that will writes it output to the provided implementation of [`Write`].
Expand Down Expand Up @@ -111,7 +110,7 @@ where
impl<W, T> Sink for UnifiedDiffBuilder<'_, W, T>
where
W: Write,
T: Hash + Eq + Display,
T: Display,
{
type Out = W;

Expand Down

0 comments on commit 226742f

Please sign in to comment.