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};
44use std::collections::hash_map::DefaultHasher;
55use std::collections::hash_set;
66use std::collections::BTreeMap;
7use std::convert::TryFrom;
7use std::convert::{TryFrom, TryInto};
88use std::fmt::{self, Display};
99use std::hash::{Hash, Hasher};
1010use std::ops::{Deref, DerefMut};
@@ -130,6 +130,24 @@ const MIN_MSG_LEN: usize = 30;
130130const TIME_GUTTER_EMPTY: &str = " ";
131131const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY);
132132
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
133151/// Before the image is loaded, already display a placeholder frame of the image size.
134152fn placeholder_frame(
135153 text: Option<&str>,
@@ -313,17 +331,14 @@ impl MessageCursor {
313331 }
314332
315333 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();
317335 let ev_term = OwnedEventId::try_from("$").ok()?;
318336
319337 let ts_start = MessageTimeStamp::try_from(cursor.get_y()).ok()?;
320338 let start = (ts_start, ev_term);
321339
322340 for ((ts, event_id), _) in thread.range(&start..) {
323 let mut hasher = DefaultHasher::new();
324 event_id.hash(&mut hasher);
325
326 if hasher.finish() == ev_hash {
341 if hash_event_id(event_id)? == ev_hash {
327342 return Self::from((*ts, event_id.clone())).into();
328343 }
329344
@@ -342,11 +357,8 @@ impl MessageCursor {
342357 pub fn to_cursor(&self, thread: &Messages) -> Option<Cursor> {
343358 let (ts, event_id) = self.to_key(thread)?;
344359
345 let y: usize = usize::try_from(ts).ok()?;
346
347 let mut hasher = DefaultHasher::new();
348 event_id.hash(&mut hasher);
349 let x = usize::try_from(hasher.finish()).ok()?;
360 let y = usize::try_from(ts).ok()?;
361 let x = hash_event_id(event_id)?;
350362
351363 Cursor::new(y, x).into()
352364 }