Skip to content

Commit

Permalink
style: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
Taowyoo committed Oct 20, 2023
1 parent f69f806 commit c40ed19
Show file tree
Hide file tree
Showing 53 changed files with 1,919 additions and 2,237 deletions.
48 changes: 30 additions & 18 deletions mbedtls-sys/build/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ struct MbedtlsParseCallbacks;

impl bindgen::callbacks::ParseCallbacks for MbedtlsParseCallbacks {
fn item_name(&self, original_item_name: &str) -> Option<String> {
Some(original_item_name.trim_start_matches("mbedtls_").trim_start_matches("MBEDTLS_").to_owned())
Some(
original_item_name
.trim_start_matches("mbedtls_")
.trim_start_matches("MBEDTLS_")
.to_owned(),
)
}

fn enum_variant_name(
&self,
_enum_name: Option<&str>,
original_variant_name: &str,
_variant_value: bindgen::callbacks::EnumVariantValue
_variant_value: bindgen::callbacks::EnumVariantValue,
) -> Option<String> {
self.item_name(original_variant_name)
}
Expand All @@ -39,7 +44,11 @@ impl bindgen::callbacks::ParseCallbacks for MbedtlsParseCallbacks {
}
}

fn blocklisted_type_implements_trait(&self, _name: &str, derive_trait: bindgen::callbacks::DeriveTrait) -> Option<bindgen::callbacks::ImplementsTrait> {
fn blocklisted_type_implements_trait(
&self,
_name: &str,
derive_trait: bindgen::callbacks::DeriveTrait,
) -> Option<bindgen::callbacks::ImplementsTrait> {
if derive_trait == bindgen::callbacks::DeriveTrait::Default {
Some(bindgen::callbacks::ImplementsTrait::Manually)
} else {
Expand All @@ -53,24 +62,29 @@ impl bindgen::callbacks::ParseCallbacks for MbedtlsParseCallbacks {
fn generate_deprecated_union_accessors(bindings: &str) -> String {
#[derive(Default)]
struct UnionImplBuilder {
impls: String
impls: String,
}

impl<'ast> syn::visit::Visit<'ast> for UnionImplBuilder {
fn visit_item_union(&mut self, i: &'ast syn::ItemUnion) {
let union_name = &i.ident;
let field_name = i.fields.named.iter().map(|field| field.ident.as_ref().unwrap());
let field_type = i.fields.named.iter().map(|field| &field.ty);
write!(self.impls, "{}", quote::quote! {
impl #union_name {
#(
#[deprecated]
pub unsafe fn #field_name(&mut self) -> *mut #field_type {
&mut self.#field_name
}
)*
write!(
self.impls,
"{}",
quote::quote! {
impl #union_name {
#(
#[deprecated]
pub unsafe fn #field_name(&mut self) -> *mut #field_type {
&mut self.#field_name
}
)*
}
}
}).unwrap();
)
.unwrap();
}
}

Expand Down Expand Up @@ -107,10 +121,7 @@ impl super::BuildConfig {
match output {
Ok(sysroot) => {
let path = std::str::from_utf8(&sysroot.stdout).expect("Malformed sysroot");
let trimmed_path = path
.strip_suffix("\r\n")
.or(path.strip_suffix("\n"))
.unwrap_or(&path);
let trimmed_path = path.strip_suffix("\r\n").or(path.strip_suffix("\n")).unwrap_or(&path);
cc.flag(&format!("--sysroot={}", trimmed_path));
}
_ => {} // skip toolchains without a configured sysroot
Expand Down Expand Up @@ -151,7 +162,8 @@ impl super::BuildConfig {
f.write_all(union_impls.as_bytes())?;
f.write_all(b"use crate::types::*;\n")?; // for FILE, time_t, etc.
Ok(())
}).expect("bindings.rs I/O error");
})
.expect("bindings.rs I/O error");

let mod_bindings = self.out_dir.join("mod-bindings.rs");
fs::write(mod_bindings, b"mod bindings;\n").expect("mod-bindings.rs I/O error");
Expand Down
19 changes: 4 additions & 15 deletions mbedtls-sys/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ mod mod_bindgen;
#[path = "cmake.rs"]
mod mod_cmake;

use features::FEATURES;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use features::FEATURES;

struct BuildConfig {
out_dir: PathBuf,
Expand Down Expand Up @@ -69,24 +69,13 @@ impl BuildConfig {

fn print_rerun_files(&self) {
println!("cargo:rerun-if-env-changed=RUST_MBEDTLS_SYS_SOURCE");
println!(
"cargo:rerun-if-changed={}",
self.mbedtls_src.join("CMakeLists.txt").display()
);
println!("cargo:rerun-if-changed={}", self.mbedtls_src.join("CMakeLists.txt").display());
let include = self.mbedtls_src.join(Path::new("include").join("mbedtls"));
for h in headers::enabled_ordered() {
println!("cargo:rerun-if-changed={}", include.join(h).display());
}
for f in self
.mbedtls_src
.join("library")
.read_dir()
.expect("read_dir failed")
{
println!(
"cargo:rerun-if-changed={}",
f.expect("DirEntry failed").path().display()
);
for f in self.mbedtls_src.join("library").read_dir().expect("read_dir failed") {
println!("cargo:rerun-if-changed={}", f.expect("DirEntry failed").path().display());
}
}

Expand Down
18 changes: 13 additions & 5 deletions mbedtls-sys/build/cmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ impl super::BuildConfig {
}

let target = std::env::var("TARGET").expect("TARGET environment variable should be set in build scripts");
// thumbv6m-none-eabi, thumbv7em-none-eabi, thumbv7em-none-eabihf, thumbv7m-none-eabi
// probably use arm-none-eabi-gcc which can cause the cmake compiler test to fail.
// thumbv6m-none-eabi, thumbv7em-none-eabi, thumbv7em-none-eabihf,
// thumbv7m-none-eabi probably use arm-none-eabi-gcc which can cause the
// cmake compiler test to fail.
if target.starts_with("thumbv") && target.contains("none-eabi") {
// When building on Linux, -rdynamic flag is added automatically. Changing the
// CMAKE_SYSTEM_NAME to Generic avoids this.
cmk.define("CMAKE_SYSTEM_NAME", "Generic");
// The compiler test requires _exit which is not available. By just trying to compile
// a library, we can fix it.
// The compiler test requires _exit which is not available. By just trying to
// compile a library, we can fix it.
cmk.define("CMAKE_TRY_COMPILE_TARGET_TYPE", "STATIC_LIBRARY");
}

Expand All @@ -52,7 +53,14 @@ impl super::BuildConfig {
println!("cargo:rustc-link-lib=mbedx509");
println!("cargo:rustc-link-lib=mbedcrypto");

println!("cargo:include={}", ::std::env::current_dir().unwrap().join(&self.mbedtls_include).to_str().expect("include/ UTF-8 error"));
println!(
"cargo:include={}",
::std::env::current_dir()
.unwrap()
.join(&self.mbedtls_include)
.to_str()
.expect("include/ UTF-8 error")
);
println!("cargo:config_h={}", self.config_h.to_str().expect("config.h UTF-8 error"));
}
}
23 changes: 15 additions & 8 deletions mbedtls-sys/build/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ impl Features {
let have_custom_threading = self.have_feature("custom_threading");
let have_custom_gmtime_r = self.have_feature("custom_gmtime_r");

if !self.have_feature("std") ||
env_have_target_cfg("env", "sgx") ||
env_have_target_cfg("os", "none") {
if !self.have_feature("std") || env_have_target_cfg("env", "sgx") || env_have_target_cfg("os", "none") {
self.with_feature("c_compiler").unwrap().insert("freestanding");
}
if let Some(components) = self.with_feature("threading") {
Expand Down Expand Up @@ -64,10 +62,17 @@ impl Features {
println!(r#"cargo:rustc-cfg={}_component="{}""#, feature, component);
}
}
println!("cargo:platform-components={}",
self.platform_components.iter().flat_map(|(feature, components)| {
components.iter().map(move |component| format!(r#"{}_component={}"#, feature, component))
} ).collect::<Vec<_>>().join(",")
println!(
"cargo:platform-components={}",
self.platform_components
.iter()
.flat_map(|(feature, components)| {
components
.iter()
.map(move |component| format!(r#"{}_component={}"#, feature, component))
})
.collect::<Vec<_>>()
.join(",")
);
}

Expand All @@ -80,7 +85,9 @@ impl Features {
}

pub fn have_platform_component(&self, feature: &'static str, component: &'static str) -> bool {
self.platform_components.get(feature).map_or(false, |feat| feat.contains(component))
self.platform_components
.get(feature)
.map_or(false, |feat| feat.contains(component))
}

pub fn have_feature(&self, feature: &'static str) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion mbedtls-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ pub use bindings::*;
/* This value is defined by a C function macro, something which is not supported by bindgen currently
https://github.com/rust-lang-nursery/rust-bindgen/issues/231
*/
pub const ECDSA_MAX_LEN : u32 = 141;
pub const ECDSA_MAX_LEN: u32 = 141;
10 changes: 6 additions & 4 deletions mbedtls/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ fn main() {
let mut b = cc::Build::new();
b.include(env::var_os("DEP_MBEDTLS_INCLUDE").unwrap());
let config_file = format!(r#""{}""#, env::var("DEP_MBEDTLS_CONFIG_H").unwrap());
b.define("MBEDTLS_CONFIG_FILE",
Some(config_file.as_str()));
b.define("MBEDTLS_CONFIG_FILE", Some(config_file.as_str()));
b.define("RUST_MBEDTLS_METADATA_HASH", Some(metadata_hash.as_str()));

b.file("src/mbedtls_malloc.c");
if sys_platform_components.get("c_compiler").map_or(false, |comps| comps.contains("freestanding")) {
if sys_platform_components
.get("c_compiler")
.map_or(false, |comps| comps.contains("freestanding"))
{
b.flag("-U_FORTIFY_SOURCE")
.define("_FORTIFY_SOURCE", Some("0"))
.flag("-ffreestanding");
Expand Down
7 changes: 1 addition & 6 deletions mbedtls/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,5 @@ fn result_main(addr: &str) -> TlsResult<()> {
fn main() {
let mut args = std::env::args();
args.next();
result_main(
&args
.next()
.expect("supply destination in command-line argument"),
)
.unwrap();
result_main(&args.next().expect("supply destination in command-line argument")).unwrap();
}
7 changes: 1 addition & 6 deletions mbedtls/examples/client_dtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,5 @@ fn result_main(addr: &str) -> TlsResult<()> {
fn main() {
let mut args = std::env::args();
args.next();
result_main(
&args
.next()
.expect("supply destination in command-line argument"),
)
.unwrap();
result_main(&args.next().expect("supply destination in command-line argument")).unwrap();
}
7 changes: 1 addition & 6 deletions mbedtls/examples/client_psk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,5 @@ fn result_main(addr: &str) -> TlsResult<()> {
fn main() {
let mut args = std::env::args();
args.next();
result_main(
&args
.next()
.expect("supply destination in command-line argument"),
)
.unwrap();
result_main(&args.next().expect("supply destination in command-line argument")).unwrap();
}
13 changes: 8 additions & 5 deletions mbedtls/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@
* according to those terms. */

use core::fmt;
use core::mem::ManuallyDrop;
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;
use core::ptr::drop_in_place;
use core::mem::ManuallyDrop;
use core::ptr::NonNull;

use mbedtls_sys::types::raw_types::c_void;

extern "C" {
#[link_name = concat!("\u{1}forward_mbedtls_free_", env!("RUST_MBEDTLS_METADATA_HASH"))]
pub(crate) fn mbedtls_free(n: *mut mbedtls_sys::types::raw_types::c_void);
#[link_name = concat!("\u{1}forward_mbedtls_calloc_", env!("RUST_MBEDTLS_METADATA_HASH"))]
pub(crate) fn mbedtls_calloc(n: mbedtls_sys::types::size_t, size: mbedtls_sys::types::size_t) -> *mut mbedtls_sys::types::raw_types::c_void;
pub(crate) fn mbedtls_calloc(
n: mbedtls_sys::types::size_t,
size: mbedtls_sys::types::size_t,
) -> *mut mbedtls_sys::types::raw_types::c_void;
}

#[repr(transparent)]
pub struct Box<T> {
pub(crate) inner: NonNull<T>
pub(crate) inner: NonNull<T>,
}

impl<T> Box<T> {
Expand Down Expand Up @@ -66,5 +69,5 @@ unsafe impl<T: Sync> Sync for Box<T> {}

#[repr(transparent)]
pub struct List<T> {
pub(crate) inner: Option<Box<T>>
pub(crate) inner: Option<Box<T>>,
}
Loading

0 comments on commit c40ed19

Please sign in to comment.