Skip to content

Commit

Permalink
Fix more clippy issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfbiedert committed Nov 25, 2024
1 parent 921cb08 commit 335c407
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 45 deletions.
2 changes: 1 addition & 1 deletion crates/backend_c/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl CTypeConverter for Converter {

fn fnpointer_to_typename(&self, x: &FnPointerType) -> String {
let prefixed = format!("{}fptr", self.config().prefix);
vec![prefixed, safe_name(&x.internal_name())].join("_")
[prefixed, safe_name(&x.internal_name())].join("_")
}

fn to_type_specifier(&self, x: &CType) -> String {
Expand Down
1 change: 1 addition & 0 deletions crates/backend_c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
//! #endif /* example_library */
//!
//! ```
#![allow(clippy::test_attr_in_doctest)]

use interoptopus::writer::IndentWriter;
use interoptopus::Interop;
Expand Down
7 changes: 3 additions & 4 deletions crates/backend_c/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub trait CWriter {

let mut params = Vec::new();

for (_, p) in function.signature().params().iter().enumerate() {
for p in function.signature().params().iter() {
match p.the_type() {
CType::Array(a) => {
params.push(format!(
Expand Down Expand Up @@ -141,17 +141,16 @@ pub trait CWriter {

let mut params = Vec::new();

for (_, p) in function.signature().params().iter().enumerate() {
for p in function.signature().params().iter() {
match p.the_type() {
CType::Array(a) => {
params.push(format!("{} [{}]", self.converter().to_type_specifier(a.array_type()), a.len(),));
}
_ => {
params.push(format!("{}", self.converter().to_type_specifier(p.the_type()),));
params.push(self.converter().to_type_specifier(p.the_type()).to_string());
}
}
}

indented!(w, r#"typedef {} (*{})({});"#, rval, name, params.join(", "))?;

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions crates/backend_cpython/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
//!
//! ```
#![allow(clippy::test_attr_in_doctest)]

use interoptopus::writer::IndentWriter;
use interoptopus::Interop;
use interoptopus::{Error, Inventory};
Expand Down
5 changes: 1 addition & 4 deletions crates/backend_cpython/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ pub trait PythonWriter {
indented!(w, [_], r#""""{}""""#, documentation)?;
}

match c.repr().layout() {
Layout::Packed => indented!(w, [_], r#"_pack_ = 1"#)?,
_ => {}
}
if c.repr().layout() == Layout::Packed { indented!(w, [_], r#"_pack_ = 1"#)? }

let alignment = c.repr().alignment();
if let Some(align) = alignment {
Expand Down
2 changes: 1 addition & 1 deletion crates/backend_csharp/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub trait CSharpTypeConverter {

/// Converts an Rust `fn()` to a C# delegate name such as `InteropDelegate`.
fn fnpointer_to_typename(&self, x: &FnPointerType) -> String {
vec!["InteropDelegate".to_string(), safe_name(&x.internal_name())].join("_")
["InteropDelegate".to_string(), safe_name(&x.internal_name())].join("_")
}

/// Converts the `u32` part in a Rust field `x: u32` to a C# equivalent. Might convert pointers to `IntPtr`.
Expand Down
2 changes: 2 additions & 0 deletions crates/backend_csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
//! }
//! ```
#![allow(clippy::test_attr_in_doctest)]

use interoptopus::writer::IndentWriter;
use interoptopus::Interop;
use interoptopus::{Error, Inventory};
Expand Down
2 changes: 1 addition & 1 deletion crates/backend_csharp/src/overloads/dotnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl OverloadWriter for DotNet {
};

let mut params = Vec::new();
for (_, p) in function.signature().params().iter().enumerate() {
for p in function.signature().params().iter() {
let name = p.name();
let native = self.pattern_to_native_in_signature(&h, p);
let the_type = h.converter.function_parameter_to_csharp_typename(p);
Expand Down
12 changes: 6 additions & 6 deletions crates/backend_csharp/src/overloads/unity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Unity {
TypePattern::Slice(p) => {
let element_type = p
.fields()
.get(0)
.first()
.expect("First parameter must exist")
.the_type()
.try_deref_pointer()
Expand All @@ -70,7 +70,7 @@ impl Unity {
TypePattern::SliceMut(p) => {
let element_type = p
.fields()
.get(0)
.first()
.expect("First parameter must exist")
.the_type()
.try_deref_pointer()
Expand All @@ -85,7 +85,7 @@ impl Unity {
TypePattern::Slice(p) => {
let element_type = p
.fields()
.get(0)
.first()
.expect("First parameter must exist")
.the_type()
.try_deref_pointer()
Expand All @@ -96,7 +96,7 @@ impl Unity {
TypePattern::SliceMut(p) => {
let element_type = p
.fields()
.get(0)
.first()
.expect("First parameter must exist")
.the_type()
.try_deref_pointer()
Expand Down Expand Up @@ -125,7 +125,7 @@ impl Unity {
);

let mut params = Vec::new();
for (_, p) in function.signature().params().iter().enumerate() {
for p in function.signature().params().iter() {
let name = p.name();
let the_type = match p.the_type() {
CType::FnPointer(_) => "IntPtr".to_string(),
Expand Down Expand Up @@ -248,7 +248,7 @@ impl OverloadWriter for Unity {
};

let mut params = Vec::new();
for (_, p) in function.signature().params().iter().enumerate() {
for p in function.signature().params().iter() {
let name = p.name();
let native = self.pattern_to_native_in_signature(&h, p, function.signature());
let the_type = h.converter.function_parameter_to_csharp_typename(p);
Expand Down
2 changes: 1 addition & 1 deletion crates/backend_csharp/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub trait CSharpWriter {
);

let mut params = Vec::new();
for (_, p) in function.signature().params().iter().enumerate() {
for p in function.signature().params().iter() {
let the_type = self.converter().function_parameter_to_csharp_typename(p);
let name = p.name();

Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@
//!
//! - Submit small bug fixes directly. Major changes should be issues first.
//! - Anything that makes previously working bindings change behavior or stop compiling
//! is a major change;
//! is a major change;
//! - This doesn't mean we're opposed to breaking stuff just that
//! we'd like to talk about it before it happens.
//! we'd like to talk about it before it happens.
//! - New features or patterns must be materialized in the reference project and accompanied by
//! an interop test (i.e., a backend test running C# / Python against a DLL invoking that code)
//! in at least one included backend.
//! an interop test (i.e., a backend test running C# / Python against a DLL invoking that code)
//! in at least one included backend.
//!
//! [Latest Version]: https://img.shields.io/crates/v/interoptopus.svg
//! [crates.io]: https://crates.io/crates/interoptopus
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/patterns/api_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
//!
//! - is based on the signatures of the involved functions, types and constants,
//! - is expected to change when the API changes, e.g., functions, types, fields, ... are added
//! changed or removed,
//! changed or removed,
//! - will even react to benign API changes (e.g., just adding functions),
//! - might even react to documentation changes (subject to change; feedback welcome).
//!
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/patterns/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
//! The macro [**`callback`**](crate::callback) enables two use cases:
//!
//! - On the **Rust** side it will generate a new function-pointer type with better compatibility
//! with respect to lifetimes in signatures, and accepting an unlimited number of args.
//!- On the **FFI side** a _properly named_ callback (delegate, function pointer ...) type can be
//! produced (e.g., `MyCallback`), instead of one where it's name is just a generic concatenation
//! of all used parameters (e.g., `InteropDelegate_fn_i32_i32`).
//! with respect to lifetimes in signatures, and accepting an unlimited number of args.
//! - On the **FFI side** a _properly named_ callback (delegate, function pointer ...) type can be
//! produced (e.g., `MyCallback`), instead of one where it's name is just a generic concatenation
//! of all used parameters (e.g., `InteropDelegate_fn_i32_i32`).
//!
//!
//! # Why we need the macro `callback!`
Expand Down Expand Up @@ -127,7 +127,7 @@ impl NamedCallback {

/// Creates a new named callback with the given meta.
pub fn with_meta(callback: FnPointerType, meta: Meta) -> Self {
if let None = callback.name() {
if callback.name().is_none() {
panic!("The pointer provided to a named callback must have a name.")
}
Self { fnpointer: callback, meta }
Expand Down
16 changes: 8 additions & 8 deletions crates/core/src/patterns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@
//! That means a backend will handle a pattern in one of three ways:
//!
//! - The pattern is **supported** and the backend will generate the raw, underlying type and / or
//! a language-specific abstraction that safely and conveniently handles it. Examples
//! include converting an [`CStrPointer`](string) to a C# `string`, or a [`service`](crate::patterns::service)
//! to a Python `class`.
//! a language-specific abstraction that safely and conveniently handles it. Examples
//! include converting an [`CStrPointer`](string) to a C# `string`, or a [`service`](crate::patterns::service)
//! to a Python `class`.
//!
//! - The pattern is not supported and will be **omitted, if the pattern was merely an aggregate** of
//! existing items. Examples include the [`service`](crate::patterns::service) pattern in C which will not
//! be emitted. However, this will not pose a problem as all constituent types and methods (functions)
//! are still available as raw bindings.
//! existing items. Examples include the [`service`](crate::patterns::service) pattern in C which will not
//! be emitted. However, this will not pose a problem as all constituent types and methods (functions)
//! are still available as raw bindings.
//!
//! - The pattern is not supported and will be **replaced with a fallback type**. Examples include
//! the [`CStrPointer`](string) which will become a regular `*const char` in C.
//! the [`CStrPointer`](string) which will become a regular `*const char` in C.
//!
//! In other words, regardless of which pattern was used, the involved methods and types will always
//! be accessible from any language.
//!
//! # Pattern Composition
//!
//! Due do a lack of expressiveness in other languages, pattern composition is often limited. Things that work
//! Due to a lack of expressiveness in other languages, pattern composition is often limited. Things that work
//! easily in Rust (e.g., a nested `FFISlice<FFIOption<CStrPointer>>`), aren't supported in other languages.
//! You therefore should rather err on the side of conservatism when designing APIs.
//!
Expand Down
7 changes: 2 additions & 5 deletions crates/core/src/patterns/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,7 @@ impl FFIErrorEnum {
/// (and probably gracefully shutdown or restart), as any subsequent call risks causing a
/// process abort.
#[allow(unused_variables)]
pub fn panics_and_errors_to_ffi_enum<E: Debug, FE: FFIError>(f: impl FnOnce() -> Result<(), E>, error_context: &str) -> FE
where
FE: From<E>,
{
pub fn panics_and_errors_to_ffi_enum<E: Debug, FE: FFIError + From<E>>(f: impl FnOnce() -> Result<(), E>, error_context: &str) -> FE {
let result: Result<(), E> = match std::panic::catch_unwind(AssertUnwindSafe(f)) {
Ok(x) => x,
Err(e) => {
Expand All @@ -221,7 +218,7 @@ where
/// Extracts a string message from a panic unwind.
pub fn get_panic_message(pan: &(dyn Any + Send)) -> &str {
match pan.downcast_ref::<&'static str>() {
Some(s) => *s,
Some(s) => s,
None => match pan.downcast_ref::<String>() {
Some(s) => s,
None => "Any { .. }",
Expand Down
6 changes: 2 additions & 4 deletions examples/real_project_layout/core_library_ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ pub enum Error {
}

impl From<interoptopus::Error> for Error {
fn from(x: interoptopus::Error) -> Self {
match x {
_ => Self::Bad,
}
fn from(_: interoptopus::Error) -> Self {
Self::Bad
}
}

Expand Down

0 comments on commit 335c407

Please sign in to comment.