use std::marker::PhantomData;
use libbitcoinkernel_sys::{
btck_BlockTreeEntry, btck_block_tree_entry_get_block_hash, btck_block_tree_entry_get_height,
btck_block_tree_entry_get_previous,
};
use crate::{
core::block::BlockHashRef,
ffi::sealed::{AsPtr, FromPtr},
ChainstateManager,
};
#[derive(Debug)]
pub struct BlockTreeEntry<'a> {
inner: *const btck_BlockTreeEntry,
marker: PhantomData<&'a ChainstateManager>,
}
unsafe impl Send for BlockTreeEntry<'_> {}
unsafe impl Sync for BlockTreeEntry<'_> {}
impl<'a> BlockTreeEntry<'a> {
pub fn prev(self) -> Option<BlockTreeEntry<'a>> {
let inner = unsafe { btck_block_tree_entry_get_previous(self.inner) };
if inner.is_null() {
return None;
}
Some(unsafe { BlockTreeEntry::from_ptr(inner) })
}
pub fn height(&self) -> i32 {
unsafe { btck_block_tree_entry_get_height(self.inner) }
}
pub fn block_hash(&self) -> BlockHashRef<'_> {
let hash_ptr = unsafe { btck_block_tree_entry_get_block_hash(self.inner) };
unsafe { BlockHashRef::from_ptr(hash_ptr) }
}
}
impl<'a> AsPtr<btck_BlockTreeEntry> for BlockTreeEntry<'a> {
fn as_ptr(&self) -> *const btck_BlockTreeEntry {
self.inner
}
}
impl<'a> FromPtr<btck_BlockTreeEntry> for BlockTreeEntry<'a> {
unsafe fn from_ptr(ptr: *const btck_BlockTreeEntry) -> Self {
BlockTreeEntry {
inner: ptr,
marker: PhantomData,
}
}
}
impl<'a> Clone for BlockTreeEntry<'a> {
fn clone(&self) -> Self {
*self
}
}
impl<'a> Copy for BlockTreeEntry<'a> {}
#[cfg(test)]
mod tests {
use super::*;
use crate::ffi::test_utils::test_ref_trait_requirements;
test_ref_trait_requirements!(
test_blocktreeentry_implementations,
BlockTreeEntry<'static>,
btck_BlockTreeEntry
);
}