Commit 4fc05c7b40
Unregistered key
Layout: unified · split
src/message/mod.rs +23 −11
| @@ -4,7 +4,7 @@ use std::cmp::{Ord, Ordering, PartialOrd}; | |||
| 4 | use std::collections::hash_map::DefaultHasher; | 4 | use std::collections::hash_map::DefaultHasher; |
| 5 | use std::collections::hash_set; | 5 | use std::collections::hash_set; |
| 6 | use std::collections::BTreeMap; | 6 | use std::collections::BTreeMap; |
| 7 | use std::convert::TryFrom; | 7 | use std::convert::{TryFrom, TryInto}; |
| 8 | use std::fmt::{self, Display}; | 8 | use std::fmt::{self, Display}; |
| 9 | use std::hash::{Hash, Hasher}; | 9 | use std::hash::{Hash, Hasher}; |
| 10 | use std::ops::{Deref, DerefMut}; | 10 | use std::ops::{Deref, DerefMut}; |
| @@ -130,6 +130,24 @@ const MIN_MSG_LEN: usize = 30; | |||
| 130 | const TIME_GUTTER_EMPTY: &str = " "; | 130 | const TIME_GUTTER_EMPTY: &str = " "; |
| 131 | const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY); | 131 | const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY); |
| 132 | 132 | ||
| 133 | const USIZE_TOO_SMALL: bool = usize::BITS < u64::BITS; | ||
| 134 | |||
| 135 | /// Convert the [u64] hash to [usize] as needed. | ||
| 136 | fn hash_finish_usize(hasher: DefaultHasher) -> Option<usize> { | ||
| 137 | if USIZE_TOO_SMALL { | ||
| 138 | (hasher.finish() % usize::MAX as u64).try_into().ok() | ||
| 139 | } else { | ||
| 140 | hasher.finish().try_into().ok() | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | /// Hash an [EventId] into a [usize]. | ||
| 145 | fn hash_event_id(event_id: &EventId) -> Option<usize> { | ||
| 146 | let mut hasher = DefaultHasher::new(); | ||
| 147 | event_id.hash(&mut hasher); | ||
| 148 | hash_finish_usize(hasher) | ||
| 149 | } | ||
| 150 | |||
| 133 | /// Before the image is loaded, already display a placeholder frame of the image size. | 151 | /// Before the image is loaded, already display a placeholder frame of the image size. |
| 134 | fn placeholder_frame( | 152 | fn placeholder_frame( |
| 135 | text: Option<&str>, | 153 | text: Option<&str>, |
| @@ -313,17 +331,14 @@ impl MessageCursor { | |||
| 313 | } | 331 | } |
| 314 | 332 | ||
| 315 | pub fn from_cursor(cursor: &Cursor, thread: &Messages) -> Option<Self> { | 333 | pub fn from_cursor(cursor: &Cursor, thread: &Messages) -> Option<Self> { |
| 316 | let ev_hash = u64::try_from(cursor.get_x()).ok()?; | 334 | let ev_hash = cursor.get_x(); |
| 317 | let ev_term = OwnedEventId::try_from("$").ok()?; | 335 | let ev_term = OwnedEventId::try_from("$").ok()?; |
| 318 | 336 | ||
| 319 | let ts_start = MessageTimeStamp::try_from(cursor.get_y()).ok()?; | 337 | let ts_start = MessageTimeStamp::try_from(cursor.get_y()).ok()?; |
| 320 | let start = (ts_start, ev_term); | 338 | let start = (ts_start, ev_term); |
| 321 | 339 | ||
| 322 | for ((ts, event_id), _) in thread.range(&start..) { | 340 | for ((ts, event_id), _) in thread.range(&start..) { |
| 323 | let mut hasher = DefaultHasher::new(); | 341 | if hash_event_id(event_id)? == ev_hash { |
| 324 | event_id.hash(&mut hasher); | ||
| 325 | |||
| 326 | if hasher.finish() == ev_hash { | ||
| 327 | return Self::from((*ts, event_id.clone())).into(); | 342 | return Self::from((*ts, event_id.clone())).into(); |
| 328 | } | 343 | } |
| 329 | 344 | ||
| @@ -342,11 +357,8 @@ impl MessageCursor { | |||
| 342 | pub fn to_cursor(&self, thread: &Messages) -> Option<Cursor> { | 357 | pub fn to_cursor(&self, thread: &Messages) -> Option<Cursor> { |
| 343 | let (ts, event_id) = self.to_key(thread)?; | 358 | let (ts, event_id) = self.to_key(thread)?; |
| 344 | 359 | ||
| 345 | let y: usize = usize::try_from(ts).ok()?; | 360 | let y = usize::try_from(ts).ok()?; |
| 346 | 361 | let x = hash_event_id(event_id)?; | |
| 347 | let mut hasher = DefaultHasher::new(); | ||
| 348 | event_id.hash(&mut hasher); | ||
| 349 | let x = usize::try_from(hasher.finish()).ok()?; | ||
| 350 | 362 | ||
| 351 | Cursor::new(y, x).into() | 363 | Cursor::new(y, x).into() |
| 352 | } | 364 | } |