summaryrefslogtreecommitdiff
path: root/rust/src/typearchive.rs
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2024-12-22 01:21:02 -0500
committerMason Reed <mason@vector35.com>2025-01-25 00:52:29 -0500
commit6a63c17853009ce7a0071458e39c140a09440230 (patch)
treead59ab3b356b0a2c60423e55f62c996891788788 /rust/src/typearchive.rs
parentbfa1d409b2d5e734804a5fdb4c68c90a93712f1f (diff)
Rust refactor
Moves a bunch of stuff out of src/types.rs that did not belong: - Confidence - Variable - Function specific stuff - Refactored InstructionInfo, see the msp430 and riscv examples. - Renamed Function::from_raw to Function::ref_from_raw and fixed places where the ref was incremented twice - Fixed FunctionRecognizer leaking functions (see above) - Fixed some apis incorrectly returning Result where Option is clearer - Started to move destructured types to the From trait for converting to an from ffi types, see Location for an example - Started to remove bad module level imports (importing std modules like mem all over the place) - Moved some wrapper handle types to named handle field (this improves readability), see CoreArchitecture for an example - Removed some unintuitive getters, this is bad practice for Rust code, just access the field directly, see DataVariable for an example - General code cleanup, purposely did not run rustfmt, that will be a single seperate commit More rust cleanup - Fixed invalid views being able to invoke UB when dealing with databases - Cleaned up some helper code in dwarf_import - Fixed inverted is_null checks causing crashes! Oops! More rust cleanup Still a WIP, I think branch info is still invalid, need to figure out the issue there. - Fixed some invalid Ref lifetimes when constructing indirectly, see Array<DataVariable> for example - Added some more comments - Removed some "magic" functions like MLIL Function::ssa_variables There are still a bunch of invalid lifetimes that aren't crashing us due to the usage of those API's not living long enough. But they ARE an issue. More rust cleanup Trying to comment more TODO's as I go along. - Renamed llil::Function to llil::LowLevelILFunction for clarity and consistency - Take base structures by ref in StructureBuilder::set_base_structures to prevent premature drops - Added more raw to wrapper conversions - Removed UB prone apis - Getting rid of more core module references, use std! - Removed improper Guard usage in array impls for wrapper types with no context - Untangling the UB of the Label api, still only half done :/ More rust cleanup - Misc formatting - Made Logger ref counted - Fixed leaking name of logger every time something was logged - Fixed the last (hopefully) of the unresolved labels - Simplified some code Fix leak in DebugInfo::AddType componentArray was never freed Add more HLIL related functions to rust More rust cleanup improve the CustomBinaryView init process Canonicalize path in `create_delete_empty` test Link core in rust When linking you must depend on the -sys crate. This is because linker arguments (what says where to find binaryninjacore) are NOT transitive. The top level application crate MUST provide it. For more information see: - https://github.com/rust-lang/cargo/issues/9554#issuecomment-857882964 - https://github.com/oxidecomputer/omicron/pull/225 Remove vendored pdb crate Use cargo to manage the git repo ref instead Fix misc rustdoc warnings Move actual plugins out of `rust/examples` and into `plugins` This is where all shipped public plugins that are not arch/view/platform/lang will be at from now on Originally they were in the rust workspace, meaning they all shared a Cargo.lock which is ill-advised. More rust cleanup - More clarification on plugin/executable requirements - Made examples actually rust examples - Add Display impl for InstructionTextToken - Renamed feature "noexports" to "no_exports" Move under_construction.png to assets directory This really did bother me Remove unneeded `extern crate bindgen` Replace nop'd log::info with println We don't register a compatible log sink so they will just get sent into the void Move inline tests into tests directory This is being done in the hopes of curbing the multi-thousand line files that will occur once we flesh out the tests Format rust code Update rust ci Still need to add support for running tests in ci More rust cleanup - Architecture id's are now typed accordingly - Fix some clippy lints - Make instruction index public in LLIL - Removed useless helper functions - LLIL expressions and instruction indexes are now typed accordingly Generate binaryninjacore-sys documentation This should show binaryninjacore-sys alongside binaryninja crate More rust cleanup - Remove lazy_static dependency - Remove hacky impl Debug for Type and just use the display impl - Add more debug impls - Reorder some top level namespace items - Add first type test Remove unneeded script helper in rust api More rust cleanup - Added main thread handler api - Register a headless main thread handler by default in initialization - Refactor QualifiedName to be properly owned - Loosened some type constraints on some apis involving QualifiedName - Fixed some apis that were crashing due to incorrect param types - Removed extern crate cruft for log crate - Simplified headless initialization using more wrapper apis - Fixed segments leaking because of no ref wrapper, see BinaryViewExt::segment_at - Added rstest to manage headless init in unit tests - Added some more unit tests - Refactored demangler api to be more ergonomic - Fixed minidump plugin not building More rust cleanup - Fixup usage of QualifiedName in plugins - Make QualifiedName more usable Implement rust TypeParser fix Platform TypeParser related functions separate User and System implementations of TypeParserResult Implement rust TypeContainer More rust cleanup - Hopefully fixed the rust.yml CI - Added TypePrinter API (this is still WIP and will crash) - Added TypeParser API - Added TypeContainer API - More work around QualifiedName apis Oh your suppose to do this Add workflow_dispatch trigger to rust.yml More rust fixes - Swapped some usage of raw 255 to MAX_CONFIDENCE, no one likes magic numbers - New InstructionTextToken API, complete with owned data, this still needs a lot of testing. - InstructionTextTokenKind describes a destructured InstructionTextToken, this should make token usage much clearer, some docs pending - Added some misc Default and Debug impls - Updated TypePrinter and architectures to use new InstructionTextToken API Misc formatting changes More rust cleanup - Fixed MANY memory leaks (most due to QualifiedName) - Made StructureBuilder more builder and less structure - Fixed CMakeLists.txt that were globbing entire api, resulting in 100 second slowdown on cmake generation - Added more Debug impl's - Added some more tests - Fixed TypeParserResult UB - Moved the From impls to blank impl for clarity, we have multiple different variants of core to rust for some structures, it should be explicit which one you are choosing. - PossibleValueSet should now be able to allocate so we can go from rust to core with those variants that require allocation - Misc doc code formatting Misc clippy lints and clippy CI Co-authored-by: Michael Krasnitski <michael.krasnitski@gmail.com> Fix typo in rust CI Misc rust formatting Fix misc typos and add typos to rust CI Add cargo workspace This will help tooling and external contributors get a map of the rust crates within binaryninja-api More rust cleanup - Format all rust plugins - Fix some tests that were out of date - Simplify WARP tests to only binaries, building object files from source is a pain - Link to core in all rust plugins - Fix some memory leaks - Update warp insta snapshots - Fix some misc clippy lints Run rust tests in CI This commit also coincides with the creation of the "testing" environment which exposes a BN_SERIAL secret for pulling a headless Binary Ninja Install missing wayland dependency in github CI Apparently its needed for linux file picker for the WARP integration Set the BINARYNINJADIR so rust can find binaryninjacore in CI The chances of this working are low Misc remove unused dependency Rust misc formatting fixes Improve initialization in rust headless scripts Provide sensible errors and validation to rust headless scripts, solves https://github.com/Vector35/binaryninja-api/issues/5796 Add BN_LICENSE environment variable to rust CI We pass the serial to download binary ninja, but we never provided the license for core initialization Fix typo More rust cleanup - Improved binary view initialization (see init_with_opts) - Allow floating license to free itself before initialization - Add initialization unit test - Add better Debug impls for some common types - Use Path api for opening binary views, this is not breaking as it uses the AsRef impl - Bump rayon dependency and constrain dependencies to x.x Update readme and include that directly in the rustdocs More rust documentation changes Add format comment to InitializationOptions::with_license Misc formatting and clippy lint allow More rust cleanup - Remove under_construction.png from the build.rs it has been removed - Use InstructionIndex in more places - Add missing PartialOrd and Ord impls for id types More rust cleanup - Make workflow cloning explicit - Add workflow tests - Add missing property string list getting for settings - Remove IntoActivityName (see https://github.com/Vector35/binaryninja-api/pull/6257) More rust cleanup This commit is half done Misc rust formatting More rust cleanup - Renamed common name conflictions (I will put my justification in the PR) - Fixed invalid instruction retrieval for LLIL - Added common aliases for llil function, instruction and expression types (see my comment in the PR) - Refactored the instruction retrieval for LLIL, MLIL and HLIL - Added instruction index types to MLIL and HLIL - Moved llil module to lowlevelil module (mlil and hlil will be moved as well) - Added preliminary LLIL unit testing Fix typos Misc clippy fixes More rust cleanup - Normalized modules - Split some code out into own files - Fixed some UB in type archive and projects - Improved API around type archives and projects substantially - Added ProgressExecutor abstraction for functions which have a progress callback - Improved background task documentation and added unit tests - Added worker thread api and unit tests - Moved some owned types to ref types, this is still not complete, but this is the path forward. - Add external location/library accessors to the binary view - Added some misc documentation - Replaced mod.rs with the module name source file Still need to normalize some paths and also update some documentation surrounding that change. Update some tests and examples Fix background task tests colliding We were creating multiple background tasks with the same progress text on multiple threads More rust cleanup - Fixed progress executor freeing itself after one iteration - Updated the last of the doc imports - Moved mainthread to main_thread - Made project creation and opening failable We could probably AsRef<ProgressExecutor> to get around the allocation and free inside the function bodies, not high priority as those functions are long running anyways. Move binary view initialization into function body for LLIL test Normalize test file names More rust cleanup - Updated README to clarify offline documentation - Refactored settings api - Added settings example to show dumping settings value and specific properties - Use the workspace to depend on binaryninja and binaryninjacore-sys - Remove binaryninjacore-sys as a workspace member (its not really required) Update workflow test to new settings api More rust cleanup - Rename Label to LowLevelILLabel - Update the functions label map automatically This fixed a major api blunder where the label map is returned as a reference and originally resulted in UB prone lifetime semantics. It was temporarily "fixed" with explicit label updates in the architecture implementation code. But that was less than ideal and was easy to mess up. Now the label map will be updated automatically as the location of labels is now tracked. Misc clippy lints More rust cleanup - Get rid of RawFunctionViewType - Add better Debug impl for Function More rust cleanup - Fixed the documentation icon using local files (thank you @mkrasnitski) - Fixed labels being updated and overwriting the label location used to update the label map More rust cleanup - Added unit tests for MLIL and HLIL - "Fixed" MLIL, LLIL, and HLIL having issues regarding Instruction vs Expression indexes - Renamed CallingConvention to CoreCallingConvention and removed architecture generic - Renamed CallingConventionBase to CallingConvention - Simplified calling convention code and fixed some bugs with implicit registers - Added impl Debug to MLIL and HLIL instructions Still need to at some point add an Expression to MLIL and HLIL. We also might want to look into having the Instruction kind just return the expression kind. Misc clippy lint More rust cleanup - Allow calling conventions to be registered for multiple architectures - Swapped a unreachable statement to an unimplemented statement More rust cleanup - Fixed the issue with PDB types, this has caused me an insane amount of grief - Fixed LLIL visit_tree missing LLIL_LOAD expressions - Added LLIL visitor test - Made all WARP file pickers use the rfd crate Use the dev branch of Binary Ninja in rust CI Misc rust fmt More rust cleanup - Refactored BinaryReader and BinaryWriter - Added some warnings to high_level_il and medium_level_il modules that they are unstable - Add BinaryReader and BinaryWriter tests - Changed BinaryView len to return u64 (that is what the core returns) - Misc formatting changes - Remove extern uses in lib.rs Add impl Debug for BinaryReader and BinaryWriter Turn off broken tests Add more info to the rust README.md More rust cleanup - Make EdgeStyle type not wrap raw - Regression tests for WARP will run on all bins in the out dir now impl rust Collaboration and Remote API Fix typo Update collaboration API Makes collaboration more in line with the refactor. Still a lot of work to do. Namely still need: - Proper errors - _with_opts functions - More ergonomic api - Better connection procedure - Updated documentation - A LOT of unit tests - An example - Typed id's for everything (i dont want BnString as the id!!!) - NEED to refactor the progress callbacks into the new progress api, but we should pull in some of the stuff the collab progress has - Elimination of apis that are dumb helpers Separate out the rust testing and use pull_request_target pull_request_target allows PR's to access the headless license, for this to be safe we need to prevent people from running the job. To prevent the job from being ran we add an environment requirement on testing that a reviewer must review the code and then manually approve it to run. More rust cleanup - Use GroupId instead of u64 - Use ProgressCallback in place of ProgressExecutor - Misc cleanup of FileMetadata - Add `save_to_path` and `save_to_accessor` to save modified binaries - Added binary_view unit tests - Added collaboration unit tests - Fixed a few issues with the collaboration apis - Renamed Command registration functions so that there is no import ambiguity - Split out RemoteUndoEntry - Collaboration apis now have a explicit `_with_progress` set of apis - Misc clippy lint fixes Fix some typos More rust cleanup - Add extra info to README.md - Refactor components api - Add components unit test Add testing and documentation to contributing section in README.md Fix misc doc comments
Diffstat (limited to 'rust/src/typearchive.rs')
-rw-r--r--rust/src/typearchive.rs947
1 files changed, 0 insertions, 947 deletions
diff --git a/rust/src/typearchive.rs b/rust/src/typearchive.rs
deleted file mode 100644
index 71a7058e..00000000
--- a/rust/src/typearchive.rs
+++ /dev/null
@@ -1,947 +0,0 @@
-use core::{ffi, mem, ptr};
-
-use binaryninjacore_sys::*;
-
-use crate::databuffer::DataBuffer;
-use crate::metadata::Metadata;
-use crate::platform::Platform;
-use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Ref};
-use crate::string::{BnStrCompatible, BnString};
-use crate::types::{QualifiedName, QualifiedNameAndType, QualifiedNameTypeAndId, Type};
-
-/// Type Archives are a collection of types which can be shared between different analysis
-/// sessions and are backed by a database file on disk. Their types can be modified, and
-/// a history of previous versions of types is stored in snapshots in the archive.
-#[repr(transparent)]
-pub struct TypeArchive {
- handle: ptr::NonNull<BNTypeArchive>,
-}
-
-impl Drop for TypeArchive {
- fn drop(&mut self) {
- unsafe { BNFreeTypeArchiveReference(self.as_raw()) }
- }
-}
-
-impl Clone for TypeArchive {
- fn clone(&self) -> Self {
- unsafe {
- Self::from_raw(ptr::NonNull::new(BNNewTypeArchiveReference(self.as_raw())).unwrap())
- }
- }
-}
-
-impl PartialEq for TypeArchive {
- fn eq(&self, other: &Self) -> bool {
- self.id() == other.id()
- }
-}
-impl Eq for TypeArchive {}
-
-impl core::hash::Hash for TypeArchive {
- fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
- (self.handle.as_ptr() as usize).hash(state);
- }
-}
-
-impl core::fmt::Debug for TypeArchive {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- let path = self.path().map(|x| x.to_string());
- f.debug_struct("TypeArchive").field("path", &path).finish()
- }
-}
-
-impl TypeArchive {
- pub(crate) unsafe fn from_raw(handle: ptr::NonNull<BNTypeArchive>) -> Self {
- Self { handle }
- }
-
- pub(crate) unsafe fn ref_from_raw(handle: &*mut BNTypeArchive) -> &Self {
- assert!(!handle.is_null());
- mem::transmute(handle)
- }
-
- #[allow(clippy::mut_from_ref)]
- pub(crate) unsafe fn as_raw(&self) -> &mut BNTypeArchive {
- &mut *self.handle.as_ptr()
- }
-
- /// Open the Type Archive at the given path, if it exists.
- pub fn open<S: BnStrCompatible>(path: S) -> Option<TypeArchive> {
- let path = path.into_bytes_with_nul();
- let handle = unsafe { BNOpenTypeArchive(path.as_ref().as_ptr() as *const ffi::c_char) };
- ptr::NonNull::new(handle).map(|handle| unsafe { TypeArchive::from_raw(handle) })
- }
-
- /// Create a Type Archive at the given path, returning None if it could not be created.
- pub fn create<S: BnStrCompatible>(path: S, platform: &Platform) -> Option<TypeArchive> {
- let path = path.into_bytes_with_nul();
- let handle = unsafe {
- BNCreateTypeArchive(
- path.as_ref().as_ptr() as *const ffi::c_char,
- platform.handle,
- )
- };
- ptr::NonNull::new(handle).map(|handle| unsafe { TypeArchive::from_raw(handle) })
- }
-
- /// Create a Type Archive at the given path and id, returning None if it could not be created.
- pub fn create_with_id<P: BnStrCompatible, I: BnStrCompatible>(
- path: P,
- id: I,
- platform: &Platform,
- ) -> Option<TypeArchive> {
- let path = path.into_bytes_with_nul();
- let id = id.into_bytes_with_nul();
- let handle = unsafe {
- BNCreateTypeArchiveWithId(
- path.as_ref().as_ptr() as *const ffi::c_char,
- platform.handle,
- id.as_ref().as_ptr() as *const ffi::c_char,
- )
- };
- ptr::NonNull::new(handle).map(|handle| unsafe { TypeArchive::from_raw(handle) })
- }
-
- /// Get a reference to the Type Archive with the known id, if one exists.
- pub fn lookup_by_id<S: BnStrCompatible>(id: S) -> Option<TypeArchive> {
- let id = id.into_bytes_with_nul();
- let handle = unsafe { BNLookupTypeArchiveById(id.as_ref().as_ptr() as *const ffi::c_char) };
- ptr::NonNull::new(handle).map(|handle| unsafe { TypeArchive::from_raw(handle) })
- }
-
- /// Get the path to the Type Archive's file
- pub fn path(&self) -> Option<BnString> {
- let result = unsafe { BNGetTypeArchivePath(self.as_raw()) };
- (!result.is_null()).then(|| unsafe { BnString::from_raw(result) })
- }
-
- /// Get the guid for a Type Archive
- pub fn id(&self) -> Option<BnString> {
- let result = unsafe { BNGetTypeArchiveId(self.as_raw()) };
- (!result.is_null()).then(|| unsafe { BnString::from_raw(result) })
- }
-
- /// Get the associated Platform for a Type Archive
- pub fn platform(&self) -> Ref<Platform> {
- let result = unsafe { BNGetTypeArchivePlatform(self.as_raw()) };
- assert!(!result.is_null());
- unsafe { Platform::ref_from_raw(result) }
- }
-
- /// Get the id of the current snapshot in the type archive
- pub fn current_snapshot_id(&self) -> BnString {
- let result = unsafe { BNGetTypeArchiveCurrentSnapshotId(self.as_raw()) };
- assert!(!result.is_null());
- unsafe { BnString::from_raw(result) }
- }
-
- /// Revert the type archive's current snapshot to the given snapshot
- pub fn set_current_snapshot_id<S: BnStrCompatible>(&self, id: S) {
- let id = id.into_bytes_with_nul();
- unsafe {
- BNSetTypeArchiveCurrentSnapshot(
- self.as_raw(),
- id.as_ref().as_ptr() as *const ffi::c_char,
- )
- }
- }
-
- /// Get a list of every snapshot's id
- pub fn all_snapshot_ids(&self) -> Array<BnString> {
- let mut count = 0;
- let result = unsafe { BNGetTypeArchiveAllSnapshotIds(self.as_raw(), &mut count) };
- assert!(!result.is_null());
- unsafe { Array::new(result, count, ()) }
- }
-
- /// Get the ids of the parents to the given snapshot
- pub fn get_snapshot_parent_ids<S: BnStrCompatible>(
- &self,
- snapshot: S,
- ) -> Option<Array<BnString>> {
- let snapshot = snapshot.into_bytes_with_nul();
- let mut count = 0;
- let result = unsafe {
- BNGetTypeArchiveSnapshotParentIds(
- self.as_raw(),
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- &mut count,
- )
- };
- (!result.is_null()).then(|| unsafe { Array::new(result, count, ()) })
- }
-
- /// Get the ids of the children to the given snapshot
- pub fn get_snapshot_child_ids<S: BnStrCompatible>(
- &self,
- snapshot: S,
- ) -> Option<Array<BnString>> {
- let snapshot = snapshot.into_bytes_with_nul();
- let mut count = 0;
- let result = unsafe {
- BNGetTypeArchiveSnapshotChildIds(
- self.as_raw(),
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- &mut count,
- )
- };
- (!result.is_null()).then(|| unsafe { Array::new(result, count, ()) })
- }
-
- /// Add named types to the type archive. Type must have all dependant named types added
- /// prior to being added, or this function will fail.
- /// If the type already exists, it will be overwritten.
- ///
- /// * `name` - Name of new type
- /// * `type` - Definition of new type
- pub fn add_type(&self, name: &QualifiedNameAndType) {
- self.add_types(core::slice::from_ref(name))
- }
-
- /// Add named types to the type archive. Types must have all dependant named
- /// types prior to being added, or included in the list, or this function will fail.
- /// Types already existing with any added names will be overwritten.
- ///
- /// * `new_types` - Names and definitions of new types
- pub fn add_types(&self, new_types: &[QualifiedNameAndType]) {
- // SAFETY BNQualifiedNameAndType and QualifiedNameAndType are transparent
- let new_types_raw: &[BNQualifiedNameAndType] = unsafe { mem::transmute(new_types) };
- let result = unsafe {
- BNAddTypeArchiveTypes(self.as_raw(), new_types_raw.as_ptr(), new_types.len())
- };
- assert!(result);
- }
-
- /// Change the name of an existing type in the type archive.
- ///
- /// * `old_name` - Old type name in archive
- /// * `new_name` - New type name
- pub fn rename_type(&self, old_name: &QualifiedName, new_name: &QualifiedNameAndType) {
- let id = self
- .get_type_id(old_name, self.current_snapshot_id())
- .unwrap();
- return self.rename_type_by_id(id, new_name.name());
- }
-
- /// Change the name of an existing type in the type archive.
- ///
- /// * `id` - Old id of type in archive
- /// * `new_name` - New type name
- pub fn rename_type_by_id<S: BnStrCompatible>(&self, id: S, new_name: &QualifiedName) {
- let id = id.into_bytes_with_nul();
- let result = unsafe {
- BNRenameTypeArchiveType(
- self.as_raw(),
- id.as_ref().as_ptr() as *const ffi::c_char,
- &new_name.0,
- )
- };
- assert!(result);
- }
-
- /// Delete an existing type in the type archive.
- pub fn delete_type(&self, name: &QualifiedName) {
- let id = self.get_type_id(name, self.current_snapshot_id());
- let Some(id) = id else {
- panic!("Unknown type {}", name.string())
- };
- self.delete_type_by_id(id);
- }
-
- /// Delete an existing type in the type archive.
- pub fn delete_type_by_id<S: BnStrCompatible>(&self, id: S) {
- let id = id.into_bytes_with_nul();
- let result = unsafe {
- BNDeleteTypeArchiveType(self.as_raw(), id.as_ref().as_ptr() as *const ffi::c_char)
- };
- assert!(result);
- }
-
- /// Retrieve a stored type in the archive
- ///
- /// * `name` - Type name
- /// * `snapshot` - Snapshot id to search for types
- pub fn get_type_by_name<S: BnStrCompatible>(
- &self,
- name: &QualifiedName,
- snapshot: S,
- ) -> Option<Ref<Type>> {
- let snapshot = snapshot.into_bytes_with_nul();
- let result = unsafe {
- BNGetTypeArchiveTypeByName(
- self.as_raw(),
- &name.0,
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- )
- };
- (!result.is_null()).then(|| unsafe { Type::ref_from_raw(result) })
- }
-
- /// Retrieve a stored type in the archive by id
- ///
- /// * `id` - Type id
- /// * `snapshot` - Snapshot id to search for types
- pub fn get_type_by_id<I: BnStrCompatible, S: BnStrCompatible>(
- &self,
- id: I,
- snapshot: S,
- ) -> Option<Ref<Type>> {
- let snapshot = snapshot.into_bytes_with_nul();
- let id = id.into_bytes_with_nul();
- let result = unsafe {
- BNGetTypeArchiveTypeById(
- self.as_raw(),
- id.as_ref().as_ptr() as *const ffi::c_char,
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- )
- };
- (!result.is_null()).then(|| unsafe { Type::ref_from_raw(result) })
- }
-
- /// Retrieve a type's name by its id
- ///
- /// * `id` - Type id
- /// * `snapshot` - Snapshot id to search for types
- pub fn get_type_name_by_id<I: BnStrCompatible, S: BnStrCompatible>(
- &self,
- id: I,
- snapshot: S,
- ) -> QualifiedName {
- let snapshot = snapshot.into_bytes_with_nul();
- let id = id.into_bytes_with_nul();
- let result = unsafe {
- BNGetTypeArchiveTypeName(
- self.as_raw(),
- id.as_ref().as_ptr() as *const ffi::c_char,
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- )
- };
- QualifiedName(result)
- }
-
- /// Retrieve a type's id by its name
- ///
- /// * `name` - Type name
- /// * `snapshot` - Snapshot id to search for types
- pub fn get_type_id<S: BnStrCompatible>(
- &self,
- name: &QualifiedName,
- snapshot: S,
- ) -> Option<BnString> {
- let snapshot = snapshot.into_bytes_with_nul();
- let result = unsafe {
- BNGetTypeArchiveTypeId(
- self.as_raw(),
- &name.0,
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- )
- };
- (!result.is_null()).then(|| unsafe { BnString::from_raw(result) })
- }
-
- /// Retrieve all stored types in the archive at a snapshot
- ///
- /// * `snapshot` - Snapshot id to search for types
- pub fn get_types_and_ids<S: BnStrCompatible>(
- &self,
- snapshot: S,
- ) -> Array<QualifiedNameTypeAndId> {
- let snapshot = snapshot.into_bytes_with_nul();
- let mut count = 0;
- let result = unsafe {
- BNGetTypeArchiveTypes(
- self.as_raw(),
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- &mut count,
- )
- };
- assert!(!result.is_null());
- unsafe { Array::new(result, count, ()) }
- }
-
- /// Get a list of all types' ids in the archive at a snapshot
- ///
- /// * `snapshot` - Snapshot id to search for types
- pub fn get_type_ids<S: BnStrCompatible>(&self, snapshot: S) -> Array<BnString> {
- let snapshot = snapshot.into_bytes_with_nul();
- let mut count = 0;
- let result = unsafe {
- BNGetTypeArchiveTypeIds(
- self.as_raw(),
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- &mut count,
- )
- };
- assert!(!result.is_null());
- unsafe { Array::new(result, count, ()) }
- }
-
- /// Get a list of all types' names in the archive at a snapshot
- ///
- /// * `snapshot` - Snapshot id to search for types
- pub fn get_type_names<S: BnStrCompatible>(&self, snapshot: S) -> Array<QualifiedName> {
- let snapshot = snapshot.into_bytes_with_nul();
- let mut count = 0;
- let result = unsafe {
- BNGetTypeArchiveTypeNames(
- self.as_raw(),
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- &mut count,
- )
- };
- assert!(!result.is_null());
- unsafe { Array::new(result, count, ()) }
- }
-
- /// Get a list of all types' names and ids in the archive at a current snapshot
-
- /// * `snapshot` - Snapshot id to search for types
- pub fn get_type_names_and_ids<S: BnStrCompatible>(
- &self,
- snapshot: S,
- ) -> (Array<QualifiedName>, Array<BnString>) {
- let snapshot = snapshot.into_bytes_with_nul();
- let mut count = 0;
- let mut names = ptr::null_mut();
- let mut ids = ptr::null_mut();
- let result = unsafe {
- BNGetTypeArchiveTypeNamesAndIds(
- self.as_raw(),
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- &mut names,
- &mut ids,
- &mut count,
- )
- };
- assert!(result);
- (unsafe { Array::new(names, count, ()) }, unsafe {
- Array::new(ids, count, ())
- })
- }
-
- /// Get all types a given type references directly
- ///
- /// * `id` - Source type id
- /// * `snapshot` - Snapshot id to search for types
- pub fn get_outgoing_direct_references<I: BnStrCompatible, S: BnStrCompatible>(
- &self,
- id: I,
- snapshot: S,
- ) -> Array<BnString> {
- let snapshot = snapshot.into_bytes_with_nul();
- let id = id.into_bytes_with_nul();
- let mut count = 0;
- let result = unsafe {
- BNGetTypeArchiveOutgoingDirectTypeReferences(
- self.as_raw(),
- id.as_ref().as_ptr() as *const ffi::c_char,
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- &mut count,
- )
- };
- assert!(!result.is_null());
- unsafe { Array::new(result, count, ()) }
- }
-
- /// Get all types a given type references, and any types that the referenced types reference
- ///
- /// :param id: Source type id
- /// :param snapshot: Snapshot id to search for types
- pub fn get_outgoing_recursive_references<I: BnStrCompatible, S: BnStrCompatible>(
- &self,
- id: I,
- snapshot: S,
- ) -> Array<BnString> {
- let snapshot = snapshot.into_bytes_with_nul();
- let id = id.into_bytes_with_nul();
- let mut count = 0;
- let result = unsafe {
- BNGetTypeArchiveOutgoingRecursiveTypeReferences(
- self.as_raw(),
- id.as_ref().as_ptr() as *const ffi::c_char,
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- &mut count,
- )
- };
- assert!(!result.is_null());
- unsafe { Array::new(result, count, ()) }
- }
-
- /// Get all types that reference a given type
- ///
- /// * `id` - Target type id
- /// * `snapshot` - Snapshot id to search for types
- pub fn get_incoming_direct_references<I: BnStrCompatible, S: BnStrCompatible>(
- &self,
- id: I,
- snapshot: S,
- ) -> Array<BnString> {
- let snapshot = snapshot.into_bytes_with_nul();
- let id = id.into_bytes_with_nul();
- let mut count = 0;
- let result = unsafe {
- BNGetTypeArchiveIncomingDirectTypeReferences(
- self.as_raw(),
- id.as_ref().as_ptr() as *const ffi::c_char,
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- &mut count,
- )
- };
- assert!(!result.is_null());
- unsafe { Array::new(result, count, ()) }
- }
-
- /// Get all types that reference a given type, and all types that reference them, recursively
- ///
- /// * `id` - Target type id
- /// * `snapshot` - Snapshot id to search for types, or empty string to search the latest snapshot
- pub fn get_incoming_recursive_references<I: BnStrCompatible, S: BnStrCompatible>(
- &self,
- id: I,
- snapshot: S,
- ) -> Array<BnString> {
- let snapshot = snapshot.into_bytes_with_nul();
- let id = id.into_bytes_with_nul();
- let mut count = 0;
- let result = unsafe {
- BNGetTypeArchiveIncomingRecursiveTypeReferences(
- self.as_raw(),
- id.as_ref().as_ptr() as *const ffi::c_char,
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- &mut count,
- )
- };
- assert!(!result.is_null());
- unsafe { Array::new(result, count, ()) }
- }
-
- /// Look up a metadata entry in the archive
- pub fn query_metadata<S: BnStrCompatible>(&self, key: S) -> Option<Ref<Metadata>> {
- let key = key.into_bytes_with_nul();
- let result = unsafe {
- BNTypeArchiveQueryMetadata(self.as_raw(), key.as_ref().as_ptr() as *const ffi::c_char)
- };
- (!result.is_null()).then(|| unsafe { Metadata::ref_from_raw(result) })
- }
-
- /// Store a key/value pair in the archive's metadata storage
- ///
- /// * `key` - key value to associate the Metadata object with
- /// * `md` - object to store.
- pub fn store_metadata<S: BnStrCompatible>(&self, key: S, md: &Metadata) {
- let key = key.into_bytes_with_nul();
- let result = unsafe {
- BNTypeArchiveStoreMetadata(
- self.as_raw(),
- key.as_ref().as_ptr() as *const ffi::c_char,
- md.handle,
- )
- };
- assert!(result);
- }
-
- /// Delete a given metadata entry in the archive from the `key`
- pub fn remove_metadata<S: BnStrCompatible>(&self, key: S) -> bool {
- let key = key.into_bytes_with_nul();
- unsafe {
- BNTypeArchiveRemoveMetadata(self.as_raw(), key.as_ref().as_ptr() as *const ffi::c_char)
- }
- }
-
- /// Turn a given `snapshot` id into a data stream
- pub fn serialize_snapshot<S: BnStrCompatible>(&self, snapshot: S) -> DataBuffer {
- let snapshot = snapshot.into_bytes_with_nul();
- let result = unsafe {
- BNTypeArchiveSerializeSnapshot(
- self.as_raw(),
- snapshot.as_ref().as_ptr() as *const ffi::c_char,
- )
- };
- assert!(!result.is_null());
- DataBuffer::from_raw(result)
- }
-
- /// Take a serialized snapshot `data` stream and create a new snapshot from it
- pub fn deserialize_snapshot(&self, data: &DataBuffer) -> BnString {
- let result = unsafe { BNTypeArchiveDeserializeSnapshot(self.as_raw(), data.as_raw()) };
- assert!(!result.is_null());
- unsafe { BnString::from_raw(result) }
- }
-
- /// Register a notification listener
- pub fn register_notification_callback<T: TypeArchiveNotificationCallback>(
- &self,
- callback: T,
- ) -> TypeArchiveCallbackHandle<T> {
- // SAFETY free on [TypeArchiveCallbackHandle::Drop]
- let callback = Box::leak(Box::new(callback));
- let mut notification = BNTypeArchiveNotification {
- context: callback as *mut T as *mut ffi::c_void,
- typeAdded: Some(cb_type_added::<T>),
- typeUpdated: Some(cb_type_updated::<T>),
- typeRenamed: Some(cb_type_renamed::<T>),
- typeDeleted: Some(cb_type_deleted::<T>),
- };
- unsafe { BNRegisterTypeArchiveNotification(self.as_raw(), &mut notification) }
- TypeArchiveCallbackHandle {
- callback,
- type_archive: self.clone(),
- }
- }
-
- // NOTE NotificationClosure is left private, there is no need for the user
- // to know or use it.
- #[allow(private_interfaces)]
- pub fn register_notification_closure<A, U, R, D>(
- &self,
- type_added: A,
- type_updated: U,
- type_renamed: R,
- type_deleted: D,
- ) -> TypeArchiveCallbackHandle<NotificationClosure<A, U, R, D>>
- where
- A: FnMut(&TypeArchive, &str, &Type),
- U: FnMut(&TypeArchive, &str, &Type, &Type),
- R: FnMut(&TypeArchive, &str, &QualifiedName, &QualifiedName),
- D: FnMut(&TypeArchive, &str, &Type),
- {
- self.register_notification_callback(NotificationClosure {
- fun_type_added: type_added,
- fun_type_updated: type_updated,
- fun_type_renamed: type_renamed,
- fun_type_deleted: type_deleted,
- })
- }
-
- /// Close a type archive, disconnecting it from any active views and closing
- /// any open file handles
- pub fn close(self) {
- unsafe { BNCloseTypeArchive(self.as_raw()) }
- // NOTE self must be dropped after, don't make it `&self`
- }
-
- /// Determine if `file` is a Type Archive
- pub fn is_type_archive<P: BnStrCompatible>(file: P) -> bool {
- let file = file.into_bytes_with_nul();
- unsafe { BNIsTypeArchive(file.as_ref().as_ptr() as *const ffi::c_char) }
- }
-
- // TODO implement TypeContainer
- ///// Get the TypeContainer interface for this Type Archive, presenting types
- ///// at the current snapshot in the archive.
- //pub fn type_container(&self) -> TypeContainer {
- // let result = unsafe { BNGetTypeArchiveTypeContainer(self.as_raw()) };
- // unsafe { TypeContainer::from_raw(ptr::NonNull::new(result).unwrap()) }
- //}
-
- /// Do some function in a transaction making a new snapshot whose id is passed to func. If func throws,
- /// the transaction will be rolled back and the snapshot will not be created.
- ///
- /// * `func` - Function to call
- /// * `parents` - Parent snapshot ids
- ///
- /// Returns Created snapshot id
- pub fn new_snapshot_transaction<P, F>(&self, mut function: F, parents: &[BnString]) -> BnString
- where
- P: BnStrCompatible,
- F: FnMut(&str) -> bool,
- {
- unsafe extern "C" fn cb_callback<F: FnMut(&str) -> bool>(
- ctxt: *mut ffi::c_void,
- id: *const ffi::c_char,
- ) -> bool {
- let fun: &mut F = &mut *(ctxt as *mut F);
- fun(&ffi::CStr::from_ptr(id).to_string_lossy())
- }
-
- // SAFETY BnString and `*const ffi::c_char` are transparent
- let parents_raw = parents.as_ptr() as *const *const ffi::c_char;
-
- let result = unsafe {
- BNTypeArchiveNewSnapshotTransaction(
- self.as_raw(),
- Some(cb_callback::<F>),
- &mut function as *mut F as *mut ffi::c_void,
- parents_raw,
- parents.len(),
- )
- };
- assert!(!result.is_null());
- unsafe { BnString::from_raw(result) }
- }
-
- /// Merge two snapshots in the archive to produce a new snapshot
- ///
- /// * `base_snapshot` - Common ancestor of snapshots
- /// * `first_snapshot` - First snapshot to merge
- /// * `second_snapshot` - Second snapshot to merge
- /// * `merge_conflicts` - List of all conflicting types, id <-> target snapshot
- /// * `progress` - Function to call for progress updates
- ///
- /// Returns Snapshot id, if merge was successful, otherwise the List of
- /// conflicting type ids
- pub fn merge_snapshots<B, F, S, P, M, MI, MK>(
- &self,
- base_snapshot: B,
- first_snapshot: F,
- second_snapshot: S,
- merge_conflicts: M,
- mut progress: P,
- ) -> Result<BnString, Array<BnString>>
- where
- B: BnStrCompatible,
- F: BnStrCompatible,
- S: BnStrCompatible,
- P: FnMut(usize, usize) -> bool,
- M: IntoIterator<Item = (MI, MK)>,
- MI: BnStrCompatible,
- MK: BnStrCompatible,
- {
- unsafe extern "C" fn cb_callback<F: FnMut(usize, usize) -> bool>(
- ctxt: *mut ffi::c_void,
- progress: usize,
- total: usize,
- ) -> bool {
- let ctxt: &mut F = &mut *(ctxt as *mut F);
- ctxt(progress, total)
- }
-
- let base_snapshot = base_snapshot.into_bytes_with_nul();
- let first_snapshot = first_snapshot.into_bytes_with_nul();
- let second_snapshot = second_snapshot.into_bytes_with_nul();
- let (merge_keys, merge_values): (Vec<BnString>, Vec<BnString>) = merge_conflicts
- .into_iter()
- .map(|(k, v)| (BnString::new(k), BnString::new(v)))
- .unzip();
- // SAFETY BnString and `*const ffi::c_char` are transparent
- let merge_keys_raw = merge_keys.as_ptr() as *const *const ffi::c_char;
- let merge_values_raw = merge_values.as_ptr() as *const *const ffi::c_char;
-
- let mut conflicts_errors = ptr::null_mut();
- let mut conflicts_errors_count = 0;
-
- let mut result = ptr::null_mut();
-
- let success = unsafe {
- BNTypeArchiveMergeSnapshots(
- self.as_raw(),
- base_snapshot.as_ref().as_ptr() as *const ffi::c_char,
- first_snapshot.as_ref().as_ptr() as *const ffi::c_char,
- second_snapshot.as_ref().as_ptr() as *const ffi::c_char,
- merge_keys_raw,
- merge_values_raw,
- merge_keys.len(),
- &mut conflicts_errors,
- &mut conflicts_errors_count,
- &mut result,
- Some(cb_callback::<P>),
- (&mut progress) as *mut P as *mut ffi::c_void,
- )
- };
- if success {
- assert!(!result.is_null());
- Ok(unsafe { BnString::from_raw(result) })
- } else {
- assert!(!conflicts_errors.is_null());
- Err(unsafe { Array::new(conflicts_errors, conflicts_errors_count, ()) })
- }
- }
-}
-
-impl CoreArrayProvider for TypeArchive {
- type Raw = *mut BNTypeArchive;
- type Context = ();
- type Wrapped<'a> = &'a TypeArchive;
-}
-
-unsafe impl CoreArrayProviderInner for TypeArchive {
- unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
- BNFreeTypeArchiveList(raw, count)
- }
-
- unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
- Self::ref_from_raw(raw)
- }
-}
-
-pub struct TypeArchiveCallbackHandle<T: TypeArchiveNotificationCallback> {
- callback: *mut T,
- type_archive: TypeArchive,
-}
-
-impl<T: TypeArchiveNotificationCallback> Drop for TypeArchiveCallbackHandle<T> {
- fn drop(&mut self) {
- let mut notification = BNTypeArchiveNotification {
- context: self.callback as *mut ffi::c_void,
- typeAdded: Some(cb_type_added::<T>),
- typeUpdated: Some(cb_type_updated::<T>),
- typeRenamed: Some(cb_type_renamed::<T>),
- typeDeleted: Some(cb_type_deleted::<T>),
- };
- // unregister the notification callback
- unsafe {
- BNUnregisterTypeArchiveNotification(self.type_archive.as_raw(), &mut notification)
- }
- // free the context created at [TypeArchive::register_notification_callback]
- drop(unsafe { Box::from_raw(self.callback) });
- }
-}
-
-pub trait TypeArchiveNotificationCallback {
- /// Called when a type is added to the archive
- ///
- /// * `archive` - Source Type archive
- /// * `id` - Id of type added
- /// * `definition` - Definition of type
- fn type_added(&mut self, _archive: &TypeArchive, _id: &str, _definition: &Type) {}
-
- /// Called when a type in the archive is updated to a new definition
- ///
- /// * `archive` - Source Type archive
- /// * `id` - Id of type
- /// * `old_definition` - Previous definition
- /// * `new_definition` - Current definition
- fn type_updated(
- &mut self,
- _archive: &TypeArchive,
- _id: &str,
- _old_definition: &Type,
- _new_definition: &Type,
- ) {
- }
-
- /// Called when a type in the archive is renamed
- ///
- /// * `archive` - Source Type archive
- /// * `id` - Type id
- /// * `old_name` - Previous name
- /// * `new_name` - Current name
- fn type_renamed(
- &mut self,
- _archive: &TypeArchive,
- _id: &str,
- _old_name: &QualifiedName,
- _new_name: &QualifiedName,
- ) {
- }
-
- /// Called when a type in the archive is deleted from the archive
- ///
- /// * `archive` - Source Type archive
- /// * `id` - Id of type deleted
- /// * `definition` - Definition of type deleted
- fn type_deleted(&mut self, _archive: &TypeArchive, _id: &str, _definition: &Type) {}
-}
-
-struct NotificationClosure<A, U, R, D>
-where
- A: FnMut(&TypeArchive, &str, &Type),
- U: FnMut(&TypeArchive, &str, &Type, &Type),
- R: FnMut(&TypeArchive, &str, &QualifiedName, &QualifiedName),
- D: FnMut(&TypeArchive, &str, &Type),
-{
- fun_type_added: A,
- fun_type_updated: U,
- fun_type_renamed: R,
- fun_type_deleted: D,
-}
-
-impl<A, U, R, D> TypeArchiveNotificationCallback for NotificationClosure<A, U, R, D>
-where
- A: FnMut(&TypeArchive, &str, &Type),
- U: FnMut(&TypeArchive, &str, &Type, &Type),
- R: FnMut(&TypeArchive, &str, &QualifiedName, &QualifiedName),
- D: FnMut(&TypeArchive, &str, &Type),
-{
- fn type_added(&mut self, archive: &TypeArchive, id: &str, definition: &Type) {
- (self.fun_type_added)(archive, id, definition)
- }
-
- fn type_updated(
- &mut self,
- archive: &TypeArchive,
- id: &str,
- old_definition: &Type,
- new_definition: &Type,
- ) {
- (self.fun_type_updated)(archive, id, old_definition, new_definition)
- }
-
- fn type_renamed(
- &mut self,
- archive: &TypeArchive,
- id: &str,
- old_name: &QualifiedName,
- new_name: &QualifiedName,
- ) {
- (self.fun_type_renamed)(archive, id, old_name, new_name)
- }
-
- fn type_deleted(&mut self, archive: &TypeArchive, id: &str, definition: &Type) {
- (self.fun_type_deleted)(archive, id, definition)
- }
-}
-
-unsafe extern "C" fn cb_type_added<T: TypeArchiveNotificationCallback>(
- ctxt: *mut ::std::os::raw::c_void,
- archive: *mut BNTypeArchive,
- id: *const ::std::os::raw::c_char,
- definition: *mut BNType,
-) {
- let ctxt: &mut T = &mut *(ctxt as *mut T);
- ctxt.type_added(
- unsafe { TypeArchive::ref_from_raw(&archive) },
- unsafe { ffi::CStr::from_ptr(id).to_string_lossy().as_ref() },
- &Type { handle: definition },
- )
-}
-unsafe extern "C" fn cb_type_updated<T: TypeArchiveNotificationCallback>(
- ctxt: *mut ::std::os::raw::c_void,
- archive: *mut BNTypeArchive,
- id: *const ::std::os::raw::c_char,
- old_definition: *mut BNType,
- new_definition: *mut BNType,
-) {
- let ctxt: &mut T = &mut *(ctxt as *mut T);
- ctxt.type_updated(
- unsafe { TypeArchive::ref_from_raw(&archive) },
- unsafe { ffi::CStr::from_ptr(id).to_string_lossy().as_ref() },
- &Type {
- handle: old_definition,
- },
- &Type {
- handle: new_definition,
- },
- )
-}
-unsafe extern "C" fn cb_type_renamed<T: TypeArchiveNotificationCallback>(
- ctxt: *mut ::std::os::raw::c_void,
- archive: *mut BNTypeArchive,
- id: *const ::std::os::raw::c_char,
- old_name: *const BNQualifiedName,
- new_name: *const BNQualifiedName,
-) {
- let ctxt: &mut T = &mut *(ctxt as *mut T);
- let old_name = mem::ManuallyDrop::new(QualifiedName(*old_name));
- let new_name = mem::ManuallyDrop::new(QualifiedName(*new_name));
- ctxt.type_renamed(
- unsafe { TypeArchive::ref_from_raw(&archive) },
- unsafe { ffi::CStr::from_ptr(id).to_string_lossy().as_ref() },
- &old_name,
- &new_name,
- )
-}
-unsafe extern "C" fn cb_type_deleted<T: TypeArchiveNotificationCallback>(
- ctxt: *mut ::std::os::raw::c_void,
- archive: *mut BNTypeArchive,
- id: *const ::std::os::raw::c_char,
- definition: *mut BNType,
-) {
- let ctxt: &mut T = &mut *(ctxt as *mut T);
- ctxt.type_deleted(
- unsafe { TypeArchive::ref_from_raw(&archive) },
- unsafe { ffi::CStr::from_ptr(id).to_string_lossy().as_ref() },
- &Type { handle: definition },
- )
-}