implement TextBuffer -> PostBuffer downcasting

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-02-03 17:29:36 -05:00
parent 07c6b27493
commit a3e975d133

View File

@@ -2,7 +2,9 @@ use egui::TextBuffer;
use enostr::{FullKeypair, Pubkey}; use enostr::{FullKeypair, Pubkey};
use nostrdb::{Note, NoteBuilder, NoteReply}; use nostrdb::{Note, NoteBuilder, NoteReply};
use std::{ use std::{
any::TypeId,
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
hash::{DefaultHasher, Hash, Hasher},
ops::Range, ops::Range,
}; };
use tracing::error; use tracing::error;
@@ -351,6 +353,18 @@ impl PostBuffer {
} }
} }
pub fn downcast_post_buffer(buffer: &dyn TextBuffer) -> Option<&PostBuffer> {
let mut hasher = DefaultHasher::new();
TypeId::of::<PostBuffer>().hash(&mut hasher);
let post_id = hasher.finish() as usize;
if buffer.type_id() == post_id {
unsafe { Some(&*(buffer as *const dyn TextBuffer as *const PostBuffer)) }
} else {
None
}
}
pub struct PostOutput { pub struct PostOutput {
pub text: String, pub text: String,
pub mentions: Vec<Pubkey>, pub mentions: Vec<Pubkey>,
@@ -555,6 +569,12 @@ impl TextBuffer for PostBuffer {
} }
} }
} }
fn type_id(&self) -> usize {
let mut hasher = DefaultHasher::new();
TypeId::of::<PostBuffer>().hash(&mut hasher);
hasher.finish() as usize
}
} }
fn first_is_desired_char(text: &str, desired: char) -> bool { fn first_is_desired_char(text: &str, desired: char) -> bool {