libsdb/
traits.rs

1use std::any::Any;
2use std::cell::RefCell;
3use std::rc::{Rc, Weak};
4
5use super::stoppoint_collection::StoppointCollection;
6
7use super::sdb_error::SdbError;
8use super::{breakpoint_site::IdType, types::VirtualAddress};
9
10pub trait StoppointTrait: Any {
11    fn id(&self) -> IdType;
12
13    fn at_address(&self, addr: VirtualAddress) -> bool;
14
15    fn disable(&mut self) -> Result<(), SdbError>;
16
17    fn address(&self) -> VirtualAddress;
18
19    fn enable(&mut self) -> Result<(), SdbError>;
20
21    fn is_enabled(&self) -> bool;
22
23    fn in_range(&self, low: VirtualAddress, high: VirtualAddress) -> bool;
24
25    fn is_hardware(&self) -> bool;
26
27    fn is_internal(&self) -> bool;
28
29    fn breakpoint_sites(&self) -> StoppointCollection;
30
31    fn resolve(&mut self) -> Result<(), SdbError>;
32}
33
34pub trait MaybeRc {
35    fn get_rc(&self) -> Rc<RefCell<dyn StoppointTrait>>;
36}
37
38impl MaybeRc for Rc<RefCell<dyn StoppointTrait>> {
39    fn get_rc(&self) -> Rc<RefCell<dyn StoppointTrait>> {
40        self.clone()
41    }
42}
43
44impl MaybeRc for Weak<RefCell<dyn StoppointTrait>> {
45    fn get_rc(&self) -> Rc<RefCell<dyn StoppointTrait>> {
46        self.upgrade().unwrap()
47    }
48}
49
50pub trait FromLowerHexStr: Sized {
51    fn from_integral_lower_hex_radix(s: &str, radix: u32) -> Result<Self, SdbError>;
52
53    fn from_integral(s: &str) -> Result<Self, SdbError>;
54}
55
56macro_rules! impl_from_lower_hex {
57    ($($Ty:ty);+ $(;)?) => {
58        $(
59            impl FromLowerHexStr for $Ty {
60                fn from_integral_lower_hex_radix(text: &str, radix: u32) -> Result<$Ty, SdbError>{
61                    let digits = text.strip_prefix("0x").unwrap_or(text);
62                    <$Ty>::from_str_radix(digits, radix).map_err(|_|{SdbError::new_err("Invalid format")})
63                }
64
65                fn from_integral(text: &str) -> Result<$Ty, SdbError>{
66                    text.parse::<$Ty>().map_err(|_|{SdbError::new_err("Invalid format")})
67                }
68            }
69        )+
70    };
71}
72
73impl_from_lower_hex!(u8;u16;u32;u64;u128;usize;i8;i16;i32;i64;i128);