Commit 4fc05c7b40

4fc05c7b40b1d5cbeed1b0f1d1d0ba2cd46afda7

parent: 3003f0a528

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-08-18 08:31 UTC
committer: <noreply@github.com>

Handle message marks on non-64-bit platforms (#329)

Layout: unified · split

src/message/mod.rs +23 −11
@@ -4,7 +4,7 @@ use std::cmp::{Ord, Ordering, PartialOrd};
4use std::collections::hash_map::DefaultHasher; 4use std::collections::hash_map::DefaultHasher;
5use std::collections::hash_set; 5use std::collections::hash_set;
6use std::collections::BTreeMap; 6use std::collections::BTreeMap;
7use std::convert::TryFrom; 7use std::convert::{TryFrom, TryInto};
8use std::fmt::{self, Display}; 8use std::fmt::{self, Display};
9use std::hash::{Hash, Hasher}; 9use std::hash::{Hash, Hasher};
10use std::ops::{Deref, DerefMut}; 10use std::ops::{Deref, DerefMut};
@@ -130,6 +130,24 @@ const MIN_MSG_LEN: usize = 30;
130const TIME_GUTTER_EMPTY: &str = " "; 130const TIME_GUTTER_EMPTY: &str = " ";
131const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY); 131const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY);
132 132
133const USIZE_TOO_SMALL: bool = usize::BITS < u64::BITS;
134
135/// Convert the [u64] hash to [usize] as needed.
136fn 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].
145fn 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.
134fn placeholder_frame( 152fn 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 }