lldb/
data.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use crate::{sys, SBError};
8
9/// A block of data.
10#[derive(Debug)]
11pub struct SBData {
12    /// The underlying raw `SBDataRef`.
13    pub raw: sys::SBDataRef,
14}
15
16impl SBData {
17    /// Construct a new `SBData`.
18    pub(crate) fn wrap(raw: sys::SBDataRef) -> SBData {
19        SBData { raw }
20    }
21
22    /// Construct a new `Some(SBData)` or `None`.
23    pub(crate) fn maybe_wrap(raw: sys::SBDataRef) -> Option<SBData> {
24        if unsafe { sys::SBDataIsValid(raw) } {
25            Some(SBData { raw })
26        } else {
27            None
28        }
29    }
30
31    /// Check whether or not this is a valid `SBData` value.
32    pub fn is_valid(&self) -> bool {
33        unsafe { sys::SBDataIsValid(self.raw) }
34    }
35
36    /// Get address of the specified offset in this data region
37    pub fn get_address(&self, offset: sys::lldb_offset_t) -> Result<sys::lldb_addr_t, SBError> {
38        let error = SBError::default();
39        let result = unsafe { sys::SBDataGetAddress(self.raw, error.raw, offset) };
40        if error.is_success() {
41            Ok(result)
42        } else {
43            Err(error)
44        }
45    }
46
47    /// Reads the data at specified offset to the buffer.
48    pub fn read_raw_data(
49        &self,
50        offset: sys::lldb_offset_t,
51        buffer: &mut [u8],
52    ) -> Result<(), SBError> {
53        let error = SBError::default();
54        unsafe {
55            sys::SBDataReadRawData(
56                self.raw,
57                error.raw,
58                offset,
59                buffer.as_mut_ptr() as *mut _,
60                buffer.len(),
61            );
62        }
63        if error.is_success() {
64            Ok(())
65        } else {
66            Err(error)
67        }
68    }
69}
70
71impl Clone for SBData {
72    fn clone(&self) -> SBData {
73        SBData {
74            raw: unsafe { sys::CloneSBData(self.raw) },
75        }
76    }
77}
78
79impl Drop for SBData {
80    fn drop(&mut self) {
81        unsafe { sys::DisposeSBData(self.raw) };
82    }
83}
84
85unsafe impl Send for SBData {}
86unsafe impl Sync for SBData {}