Skip to content

Commit

Permalink
fix rust doc code
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed Apr 17, 2024
1 parent f408eb3 commit 1fb6fb1
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 59 deletions.
2 changes: 1 addition & 1 deletion rust/src/architecture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub trait FlagGroup: Sized + Clone + Copy {
/// Types to represent the different comparisons, so for `cr1_lt` we
/// would return a mapping along the lines of:
///
/// ```
/// ```text
/// cr1_signed -> LLFC_SLT,
/// cr1_unsigned -> LLFC_ULT,
/// ```
Expand Down
4 changes: 2 additions & 2 deletions rust/src/binaryview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,15 +1545,15 @@ pub type BinaryViewEventType = BNBinaryViewEventType;
///
/// # Example
///
/// ```rust
/// ```no_run
/// use binaryninja::binaryview::{BinaryView, BinaryViewEventHandler, BinaryViewEventType, register_binary_view_event};
///
/// struct EventHandlerContext {
/// // Context holding state available to event handler
/// }
///
/// impl BinaryViewEventHandler for EventHandlerContext {
/// fn on_event(&mut self, binary_view: &BinaryView) {
/// fn on_event(&self, binary_view: &BinaryView) {
/// // handle event
/// }
/// }
Expand Down
38 changes: 28 additions & 10 deletions rust/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
//!
//! All plugins need to provide one of the following functions for Binary Ninja to call:
//!
//! ```rust
//! pub extern "C" fn CorePluginInit() -> bool {}
//! ```no_run
//! pub extern "C" fn CorePluginInit() -> bool {
//! todo!();
//! }
//! ```
//!
//! ```rust
//! pub extern "C" fn UIPluginInit() -> bool {}
//! ```no_run
//! pub extern "C" fn UIPluginInit() -> bool {
//! todo!();
//! }
//! ```
//!
//! Both of these functions can call any of the following registration functions, though `CorePluginInit` is called during Binary Ninja core initialization, and `UIPluginInit` is called during Binary Ninja UI initialization.
Expand Down Expand Up @@ -62,7 +66,9 @@ where
/// The function call required for generic commands; commands added in this way will be in the `Plugins` submenu of the menu bar.
///
/// # Example
/// ```rust
/// ```no_run
/// # use binaryninja::command::Command;
/// # use binaryninja::binaryview::BinaryView;
/// struct MyCommand;
///
/// impl Command for MyCommand {
Expand All @@ -76,6 +82,7 @@ where
/// }
/// }
///
/// # use binaryninja::command::register;
/// #[no_mangle]
/// pub extern "C" fn CorePluginInit() -> bool {
/// register(
Expand Down Expand Up @@ -160,7 +167,9 @@ where
/// The function call required for generic commands; commands added in this way will be in the `Plugins` submenu of the menu bar.
///
/// # Example
/// ```rust
/// ```no_run
/// # use binaryninja::command::AddressCommand;
/// # use binaryninja::binaryview::BinaryView;
/// struct MyCommand;
///
/// impl AddressCommand for MyCommand {
Expand All @@ -174,6 +183,7 @@ where
/// }
/// }
///
/// # use binaryninja::command::register_for_address;
/// #[no_mangle]
/// pub extern "C" fn CorePluginInit() -> bool {
/// register_for_address(
Expand Down Expand Up @@ -258,10 +268,13 @@ where
/// The function call required for generic commands; commands added in this way will be in the `Plugins` submenu of the menu bar.
///
/// # Example
/// ```rust
/// ```no_run
/// # use std::ops::Range;
/// # use binaryninja::command::RangeCommand;
/// # use binaryninja::binaryview::BinaryView;
/// struct MyCommand;
///
/// impl AddressCommand for MyCommand {
/// impl RangeCommand for MyCommand {
/// fn action(&self, view: &BinaryView, range: Range<u64>) {
/// // Your code here
/// }
Expand All @@ -272,6 +285,7 @@ where
/// }
/// }
///
/// # use binaryninja::command::register_for_range;
/// #[no_mangle]
/// pub extern "C" fn CorePluginInit() -> bool {
/// register_for_range(
Expand Down Expand Up @@ -361,10 +375,14 @@ where
/// The function call required for generic commands; commands added in this way will be in the `Plugins` submenu of the menu bar.
///
/// # Example
/// ```rust
/// ```no_run
/// # use binaryninja::command::FunctionCommand;
/// # use binaryninja::binaryview::BinaryView;
/// # use binaryninja::function::Function;
/// # use binaryninja::command::register_for_function;
/// struct MyCommand;
///
/// impl AddressCommand for MyCommand {
/// impl FunctionCommand for MyCommand {
/// fn action(&self, view: &BinaryView, func: &Function) {
/// // Your code here
/// }
Expand Down
28 changes: 20 additions & 8 deletions rust/src/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! And finally calling `binaryninja::debuginfo::DebugInfoParser::register` to register it with the core.
//!
//! Here's a minimal, complete example boilerplate-plugin:
//! ```
//! ```no_run
//! use binaryninja::{
//! binaryview::BinaryView,
//! debuginfo::{CustomDebugInfoParser, DebugInfo, DebugInfoParser},
Expand All @@ -40,8 +40,9 @@
//! true
//! }
//!
//! fn parse_info(&self, _debug_info: &mut DebugInfo, _view: &BinaryView, _debug_file: &BinaryView, _progress: Box<dyn Fn(usize, usize) -> bool>) {
//! fn parse_info(&self, _debug_info: &mut DebugInfo, _view: &BinaryView, _debug_file: &BinaryView, _progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>) -> bool {
//! println!("Parsing info");
//! true
//! }
//! }
//!
Expand All @@ -53,11 +54,14 @@
//! ```
//!
//! `DebugInfo` will then be automatically applied to binary views that contain debug information (via the setting `analysis.debugInfo.internal`), binary views that provide valid external debug info files (`analysis.debugInfo.external`), or manually fetched/applied as below:
//! ```
//! let valid_parsers = DebugInfoParser::parsers_for_view(bv);
//! let parser = valid_parsers[0];
//! let debug_info = parser.parse_debug_info(bv);
//! bv.apply_debug_info(debug_info);
//! ```no_run
//! # use binaryninja::debuginfo::DebugInfoParser;
//! # use binaryninja::binaryview::BinaryViewExt;
//! let bv = binaryninja::load("example").unwrap();
//! let valid_parsers = DebugInfoParser::parsers_for_view(&bv);
//! let parser = valid_parsers.get(0);
//! let debug_info = parser.parse_debug_info(&bv, &bv, None, None).unwrap();
//! bv.apply_debug_info(&debug_info);
//! ```
//!
//! Multiple debug-info parsers can manually contribute debug info for a binary view by simply calling `parse_debug_info` with the
Expand Down Expand Up @@ -277,6 +281,14 @@ unsafe impl CoreOwnedArrayProvider for DebugInfoParser {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for DebugInfoParser {
type Wrapped = Guard<'a, DebugInfoParser>;

unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
Guard::new(DebugInfoParser { handle: *raw }, &())
}
}

///////////////////////
// DebugFunctionInfo

Expand Down Expand Up @@ -416,7 +428,7 @@ impl DebugInfo {
/// Returns a generator of all functions provided by a named DebugInfoParser
pub fn functions_by_name<S: BnStrCompatible>(
&self,
parser_name: S,
parser_name: S
) -> Vec<DebugFunctionInfo> {
let parser_name = parser_name.into_bytes_with_nul();

Expand Down
15 changes: 8 additions & 7 deletions rust/src/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ pub fn shutdown() {
}

/// Prelued-postlued helper function (calls [`init`] and [`shutdown`] for you)
/// ```rust
/// ```no_run
/// # use binaryninja::binaryview::BinaryViewExt;
/// binaryninja::headless::script_helper(|| {
/// binaryninja::load("/bin/cat")
/// .expect("Couldn't open `/bin/cat`")
/// .iter()
/// .for_each(|func| println!(" `{}`", func.symbol().full_name()));
/// let cat = binaryninja::load("/bin/cat").expect("Couldn't open `/bin/cat`");
/// for function in cat.functions().iter() {
/// println!(" `{}`", function.symbol().full_name());
/// }
/// });
/// ```
pub fn script_helper(func: fn()) {
Expand All @@ -119,7 +120,7 @@ impl Session {
Self {}
}

/// ```rust
/// ```no_run
/// let headless_session = binaryninja::headless::Session::new();
///
/// let bv = headless_session.load("/bin/cat").expect("Couldn't open `/bin/cat`");
Expand All @@ -128,7 +129,7 @@ impl Session {
crate::load(filename)
}

/// ```rust
/// ```no_run
/// let settings = [("analysis.linearSweep.autorun", false)].into();
/// let headless_session = binaryninja::headless::Session::new();
///
Expand Down
22 changes: 14 additions & 8 deletions rust/src/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,10 @@ impl FormInputBuilder {
///
/// This API is flexible and works both in the UI via a pop-up dialog and on the command-line.
///
/// ```
/// let responses = interaction::FormInputBuilder::new()
/// ```no_run
/// # use binaryninja::interaction::FormInputBuilder;
/// # use binaryninja::interaction::FormResponses;
/// let responses = FormInputBuilder::new()
/// .text_field("First Name", None)
/// .text_field("Last Name", None)
/// .choice_field(
Expand All @@ -467,15 +469,19 @@ impl FormInputBuilder {
/// .get_form_input("Form Title");
///
/// let food = match responses[2] {
/// Index(0) => "Pizza",
/// Index(1) => "Also Pizza",
/// Index(2) => "Also Pizza",
/// Index(3) => "Wrong Answer",
/// FormResponses::Index(0) => "Pizza",
/// FormResponses::Index(1) => "Also Pizza",
/// FormResponses::Index(2) => "Also Pizza",
/// FormResponses::Index(3) => "Wrong Answer",
/// _ => panic!("This person doesn't like pizza?!?"),
/// };
///
/// let interaction::FormResponses::String(last_name) = responses[0];
/// let interaction::FormResponses::String(first_name) = responses[1];
/// let FormResponses::String(last_name) = &responses[0] else {
/// unreachable!()
/// };
/// let FormResponses::String(first_name) = &responses[1] else {
/// unreachable!()
/// };
///
/// println!("{} {} likes {}", &first_name, &last_name, food);
/// ```
Expand Down
8 changes: 4 additions & 4 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
//!
//! Create a new library (`cargo new --lib <plugin-name>`) and include the following in your `Cargo.toml`:
//!
//! ```
//! ```toml
//! [lib]
//! crate-type = ["cdylib"]
//!
Expand All @@ -73,7 +73,7 @@
//!
//! ### `main.rs`
//! Standalone binaries need to initialize Binary Ninja before they can work. You can do this through [`headless::Session`], [`headless::script_helper`], or [`headless::init()`] at start and [`headless::shutdown()`] at shutdown.
//! ```rust
//! ```no_run
//! fn main() {
//! // This loads all the core architecture, platform, etc plugins
//! // Standalone executables need to call this, but plugins do not
Expand All @@ -87,7 +87,7 @@
//! ```
//!
//! ### `Cargo.toml`
//! ```
//! ```toml
//! [dependencies]
//! binaryninja = { git = "https://github.com/Vector35/binaryninja-api.git", branch = "dev"}
//! ```
Expand Down Expand Up @@ -219,7 +219,7 @@ pub fn load<S: BnStrCompatible>(filename: S) -> Option<rc::Ref<binaryview::Binar

/// The main way to open and load files (with options) into Binary Ninja. Make sure you've properly initialized the core before calling this function. See [`crate::headless::init()`]
///
/// ```rust
/// ```no_run
/// let settings = [("analysis.linearSweep.autorun", false)].into();
///
/// let bv = binaryninja::load_with_options("/bin/cat", true, Some(settings))
Expand Down
7 changes: 5 additions & 2 deletions rust/src/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ impl Section {

/// You need to create a section builder, customize that section, then add it to a binary view:
///
/// ```
/// bv.add_section(Section::new().align(4).entry_size(4))
/// ```no_run
/// # use binaryninja::section::Section;
/// # use binaryninja::binaryview::BinaryViewExt;
/// let bv = binaryninja::load("example").unwrap();
/// bv.add_section(Section::builder("example", 0..1024).align(4).entry_size(4))
/// ```
pub fn builder<S: BnStrCompatible>(name: S, range: Range<u64>) -> SectionBuilder<S> {
SectionBuilder::new(name, range)
Expand Down
7 changes: 5 additions & 2 deletions rust/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ impl Segment {

/// You need to create a segment builder, customize that segment, then add it to a binary view:
///
/// ```
/// bv.add_segment(Segment::new().align(4).entry_size(4))
/// ```no_run
/// # use binaryninja::segment::Segment;
/// # use binaryninja::binaryview::BinaryViewExt;
/// let bv = binaryninja::load("example").unwrap();
/// bv.add_segment(Segment::builder(0..0x1000).writable(true).readable(true))
/// ```
pub fn builder(ea_range: Range<u64>) -> SegmentBuilder {
SegmentBuilder::new(ea_range)
Expand Down
6 changes: 4 additions & 2 deletions rust/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ impl Symbol {

/// To create a new symbol, you need to create a symbol builder, customize that symbol, then add `SymbolBuilder::create` it into a `Ref<Symbol>`:
///
/// ```
/// Symbol::new().short_name("hello").full_name("hello").create();
/// ```no_run
/// # use binaryninja::symbol::Symbol;
/// # use binaryninja::symbol::SymbolType;
/// Symbol::builder(SymbolType::Data, "hello", 0x1337).short_name("hello").full_name("hello").create();
/// ```
pub fn builder(ty: SymbolType, raw_name: &str, addr: u64) -> SymbolBuilder {
SymbolBuilder::new(ty, raw_name, addr)
Expand Down
28 changes: 15 additions & 13 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,12 @@ pub struct Type {
pub(crate) handle: *mut BNType,
}

/// ```
/// use binaryninja::types::Type;
/// let bv = unsafe { BinaryView::from_raw(view) };
/// let my_custom_type_1 = Self::named_int(5, false, "my_w");
/// let my_custom_type_2 = Self::int(5, false);
/// ```no_run
/// # use crate::binaryninja::binaryview::BinaryViewExt;
/// # use binaryninja::types::Type;
/// let bv = binaryninja::load("example.bin").unwrap();
/// let my_custom_type_1 = Type::named_int(5, false, "my_w");
/// let my_custom_type_2 = Type::int(5, false);
/// bv.define_user_type("int_1", &my_custom_type_1);
/// bv.define_user_type("int_2", &my_custom_type_2);
/// ```
Expand Down Expand Up @@ -1649,9 +1650,10 @@ pub struct StructureBuilder {
pub(crate) handle: *mut BNStructureBuilder,
}

/// ```rust
/// ```no_run
/// // Includes
/// use binaryninja::types::{Structure, Type};
/// # use binaryninja::binaryview::BinaryViewExt;
/// use binaryninja::types::{Structure, StructureBuilder, Type, MemberAccess, MemberScope};
///
/// // Define struct, set size (in bytes)
/// let mut my_custom_struct = StructureBuilder::new();
Expand All @@ -1660,16 +1662,16 @@ pub struct StructureBuilder {
/// let field_3 = Type::int(8, false);
///
/// // Assign those fields
/// my_custom_struct.append(&field_1, "field_4");
/// my_custom_struct.insert(&field_1, "field_1", 0);
/// my_custom_struct.insert(&field_2, "field_2", 5);
/// my_custom_struct.insert(&field_3, "field_3", 9);
/// my_custom_struct.insert(&field_1, "field_1", 0, false, MemberAccess::PublicAccess, MemberScope::NoScope);
/// my_custom_struct.insert(&field_2, "field_2", 5, false, MemberAccess::PublicAccess, MemberScope::NoScope);
/// my_custom_struct.insert(&field_3, "field_3", 9, false, MemberAccess::PublicAccess, MemberScope::NoScope);
/// my_custom_struct.append(&field_1, "field_4", MemberAccess::PublicAccess, MemberScope::NoScope);
///
/// // Convert structure to type
/// let my_custom_structure_type = Self::structure_type(&mut my_custom_struct);
/// let my_custom_structure_type = Type::structure(&my_custom_struct.finalize());
///
/// // Add the struct to the binary view to use in analysis
/// let bv = unsafe { BinaryView::from_raw(view) };
/// let bv = binaryninja::load("example").unwrap();
/// bv.define_user_type("my_custom_struct", &my_custom_structure_type);
/// ```
impl StructureBuilder {
Expand Down

0 comments on commit 1fb6fb1

Please sign in to comment.