Skip to content

Commit 2a8e45e

Browse files
committed
added register_traits method
1 parent 1d385ee commit 2a8e45e

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

program/rust/build.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mod build_utils;
22
use bindgen::Builder;
3-
use std::collections::HashMap;
43
use std::vec::Vec;
54

65

@@ -9,11 +8,9 @@ fn main() {
98

109
let borsh_derives: Vec<String> = vec!["BorshSerialize".to_string(), "BorshDeserialize".to_string()];
1110

12-
let parser = build_utils::DeriveAdderParserCallback{types_to_traits: HashMap::from([
13-
("cmd_hdr", borsh_derives)
14-
//map more struct names to traits here, be sure that the definitions of your traites
15-
//are included in c_oracle_header.rs
16-
])};
11+
//make a parser and to it type, traits pairs
12+
let mut parser = build_utils::DeriveAdderParserCallback::new();
13+
parser.register_traits("cmd_hdr", borsh_derives);
1714

1815
//generate and write bindings
1916
let bindings = Builder::default().header("./src/bindings.h").parse_callbacks(Box::new(parser)).rustfmt_bindings(true).generate().expect("Unable to generate bindings");

program/rust/build_utils.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@ use std::collections::HashMap;
66
///that the traits of each struct are added to its
77
///definition when an instance of this struct
88
///is provided as a ParseCallback for bindgen
9-
#[derive(Debug)]
9+
#[derive(Debug, Default)]
1010
pub struct DeriveAdderParserCallback<'a>{
1111
pub types_to_traits: HashMap<&'a str, Vec<String>> ,
1212
}
1313

14+
impl <'a> DeriveAdderParserCallback<'a>{
15+
///create a parser that does not add any traits
16+
pub fn new() -> Self {
17+
Default::default()
18+
}
19+
//add pairs of types and their desired traits
20+
pub fn register_traits(&mut self, type_name: &'a str ,traits: Vec<String>){
21+
self.types_to_traits.insert(&type_name, traits);
22+
}
23+
24+
}
25+
1426
//this is required to implement the callback trait
1527
impl UnwindSafe for DeriveAdderParserCallback<'_> {}
1628

0 commit comments

Comments
 (0)