Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ fn generate_tests() {
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut dst = File::create(Path::new(&out_dir).join("tests.rs")).unwrap();
let mut dst = File::create(out_dir.join("tests.rs")).unwrap();

let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let tests_dir = manifest_dir.join("tests").join("rust");
Expand Down Expand Up @@ -55,10 +55,10 @@ fn generate_depfile_tests() {
use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut dst = File::create(Path::new(&out_dir).join("depfile_tests.rs")).unwrap();
let mut dst = File::create(out_dir.join("depfile_tests.rs")).unwrap();

let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let tests_dir = manifest_dir.join("tests").join("depfile");
Expand Down
13 changes: 4 additions & 9 deletions src/bindgen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::{BufWriter, Read, Write};
use std::io::{BufWriter, Write};
use std::path;
use std::rc::Rc;

Expand Down Expand Up @@ -243,7 +243,7 @@ impl Bindings {

// Don't compare files if we've never written this file before
if !path.as_ref().is_file() {
if let Some(parent) = path::Path::new(path.as_ref()).parent() {
if let Some(parent) = path.as_ref().parent() {
fs::create_dir_all(parent).unwrap();
}
self.write(File::create(path).unwrap());
Expand All @@ -253,15 +253,10 @@ impl Bindings {
let mut new_file_contents = Vec::new();
self.write(&mut new_file_contents);

let mut old_file_contents = Vec::new();
{
let mut old_file = File::open(&path).unwrap();
old_file.read_to_end(&mut old_file_contents).unwrap();
}
let old_file_contents = std::fs::read(&path).unwrap();

if old_file_contents != new_file_contents {
let mut new_file = File::create(&path).unwrap();
new_file.write_all(&new_file_contents).unwrap();
std::fs::write(&path, &new_file_contents).unwrap();
true
} else {
false
Expand Down
4 changes: 2 additions & 2 deletions src/bindgen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,11 @@ impl Builder {
result.extend_with(&parser::parse_src(x, &self.config)?);
}

if let Some((lib_dir, binding_lib_name)) = self.lib.clone() {
if let Some((lib_dir, binding_lib_name)) = &self.lib {
let lockfile = self.lockfile.as_deref();

let cargo = Cargo::load(
&lib_dir,
lib_dir,
lockfile,
binding_lib_name.as_deref(),
self.config.parse.parse_deps,
Expand Down
40 changes: 15 additions & 25 deletions src/bindgen/cargo/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::bindgen::ir::Cfg;
fn parse_dep_string(dep_string: &str) -> (&str, Option<&str>) {
let split: Vec<&str> = dep_string.split_whitespace().collect();

(split[0], split.get(1).cloned())
(split[0], split.get(1).copied())
}

/// A collection of metadata for a library from cargo.
Expand Down Expand Up @@ -207,14 +207,8 @@ impl Cargo {
/// Finds the directory for a specified package reference.
#[allow(unused)]
pub(crate) fn find_crate_dir(&self, package: &PackageRef) -> Option<PathBuf> {
self.metadata
.packages
.get(package)
.and_then(|meta_package| {
Path::new(&meta_package.manifest_path)
.parent()
.map(|x| x.to_owned())
})
let meta_package = self.metadata.packages.get(package)?;
Some(Path::new(&meta_package.manifest_path).parent()?.to_owned())
}

/// Finds `src/lib.rs` for a specified package reference.
Expand All @@ -225,22 +219,18 @@ impl Cargo {
let kind_cdylib = String::from("cdylib");
let kind_dylib = String::from("dylib");

self.metadata
.packages
.get(package)
.and_then(|meta_package| {
for target in &meta_package.targets {
if target.kind.contains(&kind_lib)
|| target.kind.contains(&kind_staticlib)
|| target.kind.contains(&kind_rlib)
|| target.kind.contains(&kind_cdylib)
|| target.kind.contains(&kind_dylib)
{
return Some(PathBuf::from(&target.src_path));
}
}
None
})
let meta_package = self.metadata.packages.get(package)?;
for target in &meta_package.targets {
if target.kind.contains(&kind_lib)
|| target.kind.contains(&kind_staticlib)
|| target.kind.contains(&kind_rlib)
|| target.kind.contains(&kind_cdylib)
|| target.kind.contains(&kind_dylib)
{
return Some(PathBuf::from(&target.src_path));
}
}
None
}

pub(crate) fn expand_crate(
Expand Down
9 changes: 1 addition & 8 deletions src/bindgen/cargo/cargo_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,7 @@ pub fn expand(
cmd.arg(manifest_path);
if let Some(features) = expand_features {
cmd.arg("--features");
let mut features_str = String::new();
for (index, feature) in features.iter().enumerate() {
if index != 0 {
features_str.push(' ');
}
features_str.push_str(feature);
}
cmd.arg(features_str);
cmd.arg(features.join(" "));
}
if expand_all_features {
cmd.arg("--all-features");
Expand Down
13 changes: 4 additions & 9 deletions src/bindgen/cargo/cargo_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::fs::File;
use std::io;
use std::io::Read;
use std::path::Path;

#[derive(Debug)]
/// Possible errors that can occur during Cargo.toml parsing.
/// Possible errors that can occur during Cargo.lock parsing.
pub enum Error {
/// Error during reading of Cargo.toml
/// Error during reading of Cargo.lock
#[allow(dead_code)]
Io(io::Error),
/// Deserialization error
Expand Down Expand Up @@ -43,11 +41,8 @@ pub struct Package {
pub dependencies: Option<Vec<String>>,
}

/// Parse the Cargo.toml for a given path
/// Parse the Cargo.lock for a given path
pub fn lock(manifest_path: &Path) -> Result<Lock, Error> {
let mut s = String::new();
let mut f = File::open(manifest_path)?;
f.read_to_string(&mut s)?;

let s = std::fs::read_to_string(manifest_path)?;
toml::from_str::<Lock>(&s).map_err(|x| x.into())
}
7 changes: 1 addition & 6 deletions src/bindgen/cargo/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use std::error;
use std::fmt;
use std::fs::File;
use std::io;
use std::io::Read;
use std::path::Path;

#[derive(Debug)]
Expand Down Expand Up @@ -59,9 +57,6 @@ pub struct Package {

/// Parse the Cargo.toml for a given path
pub fn manifest(manifest_path: &Path) -> Result<Manifest, Error> {
let mut s = String::new();
let mut f = File::open(manifest_path)?;
f.read_to_string(&mut s)?;

let s = std::fs::read_to_string(manifest_path)?;
toml::from_str::<Manifest>(&s).map_err(|x| x.into())
}
126 changes: 46 additions & 80 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,15 @@ impl Default for FunctionConfig {

impl FunctionConfig {
pub(crate) fn prefix(&self, annotations: &AnnotationSet) -> Option<String> {
if let Some(x) = annotations.atom("prefix") {
return x;
}
self.prefix.clone()
annotations
.atom("prefix")
.unwrap_or_else(|| self.prefix.clone())
}

pub(crate) fn postfix(&self, annotations: &AnnotationSet) -> Option<String> {
if let Some(x) = annotations.atom("postfix") {
return x;
}
self.postfix.clone()
annotations
.atom("postfix")
.unwrap_or_else(|| self.postfix.clone())
}
}

Expand Down Expand Up @@ -506,52 +504,32 @@ pub struct StructConfig {

impl StructConfig {
pub(crate) fn derive_constructor(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-constructor") {
return x;
}
self.derive_constructor
annotations
.bool("derive-constructor")
.unwrap_or(self.derive_constructor)
}
pub(crate) fn derive_eq(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-eq") {
return x;
}
self.derive_eq
annotations.bool("derive-eq").unwrap_or(self.derive_eq)
}
pub(crate) fn derive_neq(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-neq") {
return x;
}
self.derive_neq
annotations.bool("derive-neq").unwrap_or(self.derive_neq)
}
pub(crate) fn derive_lt(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-lt") {
return x;
}
self.derive_lt
annotations.bool("derive-lt").unwrap_or(self.derive_lt)
}
pub(crate) fn derive_lte(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-lte") {
return x;
}
self.derive_lte
annotations.bool("derive-lte").unwrap_or(self.derive_lte)
}
pub(crate) fn derive_gt(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-gt") {
return x;
}
self.derive_gt
annotations.bool("derive-gt").unwrap_or(self.derive_gt)
}
pub(crate) fn derive_gte(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-gte") {
return x;
}
self.derive_gte
annotations.bool("derive-gte").unwrap_or(self.derive_gte)
}
pub(crate) fn derive_ostream(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-ostream") {
return x;
}
self.derive_ostream
annotations
.bool("derive-ostream")
.unwrap_or(self.derive_ostream)
}
}

Expand Down Expand Up @@ -640,67 +618,55 @@ impl Default for EnumConfig {

impl EnumConfig {
pub(crate) fn add_sentinel(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("add-sentinel") {
return x;
}
self.add_sentinel
annotations
.bool("add-sentinel")
.unwrap_or(self.add_sentinel)
}
pub(crate) fn derive_helper_methods(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-helper-methods") {
return x;
}
self.derive_helper_methods
annotations
.bool("derive-helper-methods")
.unwrap_or(self.derive_helper_methods)
}
pub(crate) fn derive_const_casts(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-const-casts") {
return x;
}
self.derive_const_casts
annotations
.bool("derive-const-casts")
.unwrap_or(self.derive_const_casts)
}
pub(crate) fn derive_mut_casts(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-mut-casts") {
return x;
}
self.derive_mut_casts
annotations
.bool("derive-mut-casts")
.unwrap_or(self.derive_mut_casts)
}
pub(crate) fn derive_tagged_enum_destructor(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-tagged-enum-destructor") {
return x;
}
self.derive_tagged_enum_destructor
annotations
.bool("derive-tagged-enum-destructor")
.unwrap_or(self.derive_tagged_enum_destructor)
}
pub(crate) fn derive_tagged_enum_copy_constructor(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-tagged-enum-copy-constructor") {
return x;
}
self.derive_tagged_enum_copy_constructor
annotations
.bool("derive-tagged-enum-copy-constructor")
.unwrap_or(self.derive_tagged_enum_copy_constructor)
}
pub(crate) fn derive_tagged_enum_copy_assignment(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-tagged-enum-copy-assignment") {
return x;
}
self.derive_tagged_enum_copy_assignment
annotations
.bool("derive-tagged-enum-copy-assignment")
.unwrap_or(self.derive_tagged_enum_copy_assignment)
}
pub(crate) fn derive_ostream(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("derive-ostream") {
return x;
}
self.derive_ostream
annotations
.bool("derive-ostream")
.unwrap_or(self.derive_ostream)
}
pub(crate) fn enum_class(&self, annotations: &AnnotationSet) -> bool {
if let Some(x) = annotations.bool("enum-class") {
return x;
}
self.enum_class
annotations.bool("enum-class").unwrap_or(self.enum_class)
}
pub(crate) fn private_default_tagged_enum_constructor(
&self,
annotations: &AnnotationSet,
) -> bool {
if let Some(x) = annotations.bool("private-default-tagged-enum-constructor") {
return x;
}
self.private_default_tagged_enum_constructor
annotations
.bool("private-default-tagged-enum-constructor")
.unwrap_or(self.private_default_tagged_enum_constructor)
}
}

Expand Down
Loading
Loading