Skip to content

Commit 5871ab8

Browse files
committed
fixed formatting
1 parent 371d453 commit 5871ab8

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

program/rust/build.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ mod build_utils;
22
use bindgen::Builder;
33
use std::vec::Vec;
44

5-
65
fn main() {
76
println!("cargo:rustc-link-search=../c/target");
8-
9-
let borsh_derives: Vec<String> = vec!["BorshSerialize".to_string(), "BorshDeserialize".to_string()];
7+
8+
let borsh_derives: Vec<String> =
9+
vec!["BorshSerialize".to_string(), "BorshDeserialize".to_string()];
1010

1111
//make a parser and to it type, traits pairs
1212
let mut parser = build_utils::DeriveAdderParserCallback::new();
1313
parser.register_traits("cmd_hdr", borsh_derives);
1414

1515
//generate and write bindings
16-
let bindings = Builder::default().header("./src/bindings.h").parse_callbacks(Box::new(parser)).rustfmt_bindings(true).generate().expect("Unable to generate bindings");
17-
bindings.write_to_file("./bindings.rs").expect("Couldn't write bindings!");
16+
let bindings = Builder::default()
17+
.header("./src/bindings.h")
18+
.parse_callbacks(Box::new(parser))
19+
.rustfmt_bindings(true)
20+
.generate()
21+
.expect("Unable to generate bindings");
22+
bindings
23+
.write_to_file("./bindings.rs")
24+
.expect("Couldn't write bindings!");
1825
}

program/rust/build_utils.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
use bindgen::callbacks::ParseCallbacks;
2-
use std::panic::UnwindSafe;
32
use std::collections::HashMap;
3+
use std::panic::UnwindSafe;
44
///This type stores a hashmap from structnames
55
///to vectors of trait names, and ensures
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
99
#[derive(Debug, Default)]
10-
pub struct DeriveAdderParserCallback<'a>{
11-
pub types_to_traits: HashMap<&'a str, Vec<String>> ,
10+
pub struct DeriveAdderParserCallback<'a> {
11+
pub types_to_traits: HashMap<&'a str, Vec<String>>,
1212
}
1313

14-
impl <'a> DeriveAdderParserCallback<'a>{
14+
impl<'a> DeriveAdderParserCallback<'a> {
1515
///create a parser that does not add any traits
1616
pub fn new() -> Self {
1717
Default::default()
1818
}
1919
//add pairs of types and their desired traits
20-
pub fn register_traits(&mut self, type_name: &'a str ,traits: Vec<String>){
20+
pub fn register_traits(&mut self, type_name: &'a str, traits: Vec<String>) {
2121
self.types_to_traits.insert(&type_name, traits);
2222
}
23-
2423
}
2524

2625
//this is required to implement the callback trait
2726
impl UnwindSafe for DeriveAdderParserCallback<'_> {}
2827

29-
impl ParseCallbacks for DeriveAdderParserCallback<'_>{
30-
fn add_derives(&self, _name: &str) -> Vec<String>{
31-
self.types_to_traits.get(_name).unwrap_or(&Vec::<String>::new()).to_vec()
28+
impl ParseCallbacks for DeriveAdderParserCallback<'_> {
29+
fn add_derives(&self, _name: &str) -> Vec<String> {
30+
self.types_to_traits
31+
.get(_name)
32+
.unwrap_or(&Vec::<String>::new())
33+
.to_vec()
3234
}
33-
}
35+
}

program/rust/src/c_oracle_header.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(unused_variables)]
66
#![allow(dead_code)]
77
//All the custom trait imports should go here
8-
use borsh::{BorshSerialize, BorshDeserialize};
8+
use borsh::{BorshDeserialize, BorshSerialize};
99
//bindings.rs is generated by build.rs to include
1010
//things defined in bindings.h
11-
include!("../bindings.rs");
11+
include!("../bindings.rs");

program/rust/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod c_oracle_header;
22
mod time_machine_types;
33

4-
54
//Below is a high lever description of the rust/c setup.
65

76
//As we migrate from C to Rust, our Rust code needs to be able to interract with C
@@ -19,8 +18,7 @@ mod time_machine_types;
1918
//the only limitation of our set up is that we can not unit test in rust, anything that calls
2019
//a c function. Though we can test functions that use constants/types defined in oracle.h
2120

22-
23-
//do not link with C during unit tests (which are built in native architecture, unlike libpyth.o)
21+
//do not link with C during unit tests (which are built in native architecture, unlike libpyth.o)
2422
#[cfg(target_arch = "bpf")]
2523
#[link(name = "cpyth")]
2624
extern "C" {
@@ -42,4 +40,4 @@ pub extern "C" fn entrypoint(input: *mut u8) -> u64 {
4240
} else {
4341
return c_ret_val;
4442
}
45-
}
43+
}

0 commit comments

Comments
 (0)