diff --git a/src/source.rs b/src/source.rs index 48b2ea3..b7ada7d 100644 --- a/src/source.rs +++ b/src/source.rs @@ -1,5 +1,5 @@ use std::{ - any::Any, + any::{Any, TypeId}, borrow::Cow, convert::{TryFrom, TryInto}, fmt, @@ -110,6 +110,7 @@ impl AsAny for T { pub trait DynEq { fn dyn_eq(&self, other: &dyn Any) -> bool; + fn type_id(&self) -> TypeId; } impl DynEq for E { @@ -120,10 +121,17 @@ impl DynEq for E { false } } + + fn type_id(&self) -> TypeId { + TypeId::of::() + } } impl PartialEq for dyn Source { fn eq(&self, other: &Self) -> bool { + if self.as_any().type_id() != other.as_any().type_id() { + return false; + } self.dyn_eq(other.as_any()) } } diff --git a/src/with_indices.rs b/src/with_indices.rs index f199e5b..103b46e 100644 --- a/src/with_indices.rs +++ b/src/with_indices.rs @@ -5,7 +5,7 @@ pub struct WithIndices> { /// line is a string reference pub line: T, /// the byte position of each `char` in `line` string slice . - pub indices_indexes: OnceCell>, + pub indices_indexes: OnceCell>, } impl> WithIndices { @@ -23,24 +23,19 @@ impl> WithIndices { return ""; } - let indices_indexes = self.indices_indexes.get_or_init(|| { - self - .line - .as_ref() - .char_indices() - .map(|(i, _)| i as u32) - .collect::>() - .into_boxed_slice() - }); + let line = self.line.as_ref(); + let indices_indexes = self + .indices_indexes + .get_or_init(|| line.char_indices().map(|(i, _)| i).collect::>()); - let str_len = self.line.as_ref().len() as u32; - let start = *indices_indexes.get(start_index).unwrap_or(&str_len) as usize; - let end = *indices_indexes.get(end_index).unwrap_or(&str_len) as usize; + let str_len = line.len(); + let start = *indices_indexes.get(start_index).unwrap_or(&str_len); + let end = *indices_indexes.get(end_index).unwrap_or(&str_len); unsafe { // SAFETY: Since `indices` iterates over the `CharIndices` of `self`, we can guarantee // that the indices obtained from it will always be within the bounds of `self` and they // will always lie on UTF-8 sequence boundaries. - self.line.as_ref().get_unchecked(start..end) + line.get_unchecked(start..end) } } }