Skip to content

Commit

Permalink
fix rust doc code
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran authored and ElykDeer committed May 9, 2024
1 parent fe710ff commit 456e7e8
Show file tree
Hide file tree
Showing 11 changed files with 109 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 @@ -1553,15 +1553,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
31 changes: 23 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 @@ -414,7 +426,10 @@ impl DebugInfo {
}

/// Returns a generator of all functions provided by a named DebugInfoParser
pub fn functions_by_name<S: BnStrCompatible>(&self, parser_name: S) -> Vec<DebugFunctionInfo> {
pub fn functions_by_name<S: BnStrCompatible>(
&self,
parser_name: S
) -> Vec<DebugFunctionInfo> {
let parser_name = parser_name.into_bytes_with_nul();

let mut count: usize = 0;
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 @@ -451,8 +451,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 @@ -469,15 +471,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
Loading

0 comments on commit 456e7e8

Please sign in to comment.