Skip to content

Commit 508d917

Browse files
committed
Common method for whether function pointers can trivially derive traits
Again, this logic was getting copied all over the place, and it is nice to have a single canonical definition.
1 parent c65cb37 commit 508d917

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

src/ir/function.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,26 @@ impl FunctionSig {
437437
// variadic functions without an initial argument.
438438
self.is_variadic && !self.argument_types.is_empty()
439439
}
440+
441+
/// Are function pointers with this signature able to derive Rust traits?
442+
/// Rust only supports deriving traits for function pointers with a limited
443+
/// number of parameters and a couple ABIs.
444+
///
445+
/// For more details, see:
446+
///
447+
/// * https://github.com/rust-lang-nursery/rust-bindgen/issues/547,
448+
/// * https://github.com/rust-lang/rust/issues/38848,
449+
/// * and https://github.com/rust-lang/rust/issues/40158
450+
pub fn function_pointers_can_derive(&self) -> bool {
451+
if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
452+
return false;
453+
}
454+
455+
match self.abi {
456+
Abi::C | Abi::Unknown(..) => true,
457+
_ => false,
458+
}
459+
}
440460
}
441461

442462
impl ClangSubItemParser for Function {
@@ -523,48 +543,20 @@ impl Trace for FunctionSig {
523543
}
524544
}
525545

526-
// Function pointers follow special rules, see:
527-
//
528-
// https://github.com/rust-lang-nursery/rust-bindgen/issues/547,
529-
// https://github.com/rust-lang/rust/issues/38848,
530-
// and https://github.com/rust-lang/rust/issues/40158
531-
//
532-
// Note that copy is always derived, so we don't need to implement it.
533546
impl CanTriviallyDeriveDebug for FunctionSig {
534547
fn can_trivially_derive_debug(&self) -> bool {
535-
if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
536-
return false;
537-
}
538-
539-
match self.abi {
540-
Abi::C | Abi::Unknown(..) => true,
541-
_ => false,
542-
}
548+
self.function_pointers_can_derive()
543549
}
544550
}
545551

546552
impl CanTriviallyDeriveHash for FunctionSig {
547553
fn can_trivially_derive_hash(&self) -> bool {
548-
if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
549-
return false;
550-
}
551-
552-
match self.abi {
553-
Abi::C | Abi::Unknown(..) => true,
554-
_ => false,
555-
}
554+
self.function_pointers_can_derive()
556555
}
557556
}
558557

559558
impl CanTriviallyDerivePartialEqOrPartialOrd for FunctionSig {
560559
fn can_trivially_derive_partialeq_or_partialord(&self) -> bool {
561-
if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
562-
return false;
563-
}
564-
565-
match self.abi {
566-
Abi::C | Abi::Unknown(..) => true,
567-
_ => false,
568-
}
560+
self.function_pointers_can_derive()
569561
}
570562
}

0 commit comments

Comments
 (0)