summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ir/analysis/derive_partial_eq_or_partial_ord.rs89
-rw-r--r--src/ir/context.rs11
-rw-r--r--src/ir/derive.rs121
-rw-r--r--src/ir/function.rs54
-rw-r--r--src/ir/layout.rs29
-rw-r--r--src/lib.rs18
6 files changed, 145 insertions, 177 deletions
diff --git a/src/ir/analysis/derive_partial_eq_or_partial_ord.rs b/src/ir/analysis/derive_partial_eq_or_partial_ord.rs
index 1c3ab059..f34a256a 100644
--- a/src/ir/analysis/derive_partial_eq_or_partial_ord.rs
+++ b/src/ir/analysis/derive_partial_eq_or_partial_ord.rs
@@ -1,4 +1,5 @@
-//! Determining which types for which we can emit `#[derive(PartialEq)]`.
+//! Determining which types for which we cannot emit `#[derive(PartialEq,
+//! PartialOrd)]`.
use super::{ConstrainResult, MonotoneFramework, generate_dependencies};
use ir::comp::CompKind;
@@ -13,31 +14,39 @@ use ir::ty::TypeKind;
use std::collections::HashMap;
use std::collections::HashSet;
-/// An analysis that finds for each IR item whether partialeq or partialord cannot be derived.
+/// An analysis that finds for each IR item whether `PartialEq`/`PartialOrd`
+/// cannot be derived.
///
-/// We use the monotone constraint function `cannot_derive_partialeq_or_partialord`, defined as
-/// follows:
+/// We use the monotone constraint function
+/// `cannot_derive_partialeq_or_partialord`, defined as follows:
///
/// * If T is Opaque and layout of the type is known, get this layout as opaque
/// type and check whether it can be derived using trivial checks.
-/// * If T is Array type, partialeq or partialord cannot be derived if the length of
-/// the array is larger than the limit or the type of data the array contains cannot derive
-/// partialeq or partialord.
+///
+/// * If T is Array type, `PartialEq` or partialord cannot be derived if the
+/// length of the array is larger than the limit or the type of data the array
+/// contains cannot derive `PartialEq`/`PartialOrd`.
+///
/// * If T is a type alias, a templated alias or an indirection to another type,
-/// partialeq or partialord cannot be derived if the type T refers to cannot be derived partialeq or partialord.
-/// * If T is a compound type, partialeq or partialord cannot be derived if any of its base member
-/// or field cannot be derived partialeq or partialord.
-/// * If T is a pointer, T cannot be derived partialeq or partialord if T is a function pointer
-/// and the function signature cannot be derived partialeq or partialord.
+/// `PartialEq`/`PartialOrd` cannot be derived if the type T refers to cannot be
+/// derived `PartialEq`/`PartialOrd`.
+///
+/// * If T is a compound type, `PartialEq`/`PartialOrd` cannot be derived if any
+/// of its base member or field cannot be derived `PartialEq`/`PartialOrd`.
+///
+/// * If T is a pointer, T cannot be derived `PartialEq`/`PartialOrd` if T is a
+/// function pointer and the function signature cannot be derived
+/// `PartialEq`/`PartialOrd`.
+///
/// * If T is an instantiation of an abstract template definition, T cannot be
-/// derived partialeq or partialord if any of the template arguments or template definition
-/// cannot derive partialeq or partialord.
+/// derived `PartialEq`/`PartialOrd` if any of the template arguments or
+/// template definition cannot derive `PartialEq`/`PartialOrd`.
#[derive(Debug, Clone)]
pub struct CannotDerivePartialEqOrPartialOrd<'ctx> {
ctx: &'ctx BindgenContext,
// The incremental result of this analysis's computation. Everything in this
- // set cannot derive partialeq or partialord.
+ // set cannot derive `PartialEq`/`PartialOrd`.
cannot_derive_partialeq_or_partialord: HashSet<ItemId>,
// Dependencies saying that if a key ItemId has been inserted into the
@@ -46,7 +55,7 @@ pub struct CannotDerivePartialEqOrPartialOrd<'ctx> {
//
// This is a subset of the natural IR graph with reversed edges, where we
// only include the edges from the IR graph that can affect whether a type
- // can derive partialeq or partialord.
+ // can derive `PartialEq`/`PartialOrd`.
dependencies: HashMap<ItemId, Vec<ItemId>>,
}
@@ -54,7 +63,7 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> {
fn consider_edge(kind: EdgeKind) -> bool {
match kind {
// These are the only edges that can affect whether a type can derive
- // partialeq or partialord.
+ // `PartialEq`/`PartialOrd`.
EdgeKind::BaseMember |
EdgeKind::Field |
EdgeKind::TypeReference |
@@ -115,7 +124,7 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
trace!("constrain: {:?}", id);
if self.cannot_derive_partialeq_or_partialord.contains(&id) {
- trace!(" already know it cannot derive PartialEq or PartialOrd");
+ trace!(" already know it cannot derive `PartialEq`/`PartialOrd`");
return ConstrainResult::Same;
}
@@ -140,10 +149,10 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
return if layout_can_derive &&
!(ty.is_union() &&
self.ctx.options().rust_features().untagged_union()) {
- trace!(" we can trivially derive PartialEq or PartialOrd for the layout");
+ trace!(" we can trivially derive `PartialEq`/`PartialOrd` for the layout");
ConstrainResult::Same
} else {
- trace!(" we cannot derive PartialEq or PartialOrd for the layout");
+ trace!(" we cannot derive `PartialEq`/`PartialOrd` for the layout");
self.insert(id)
};
}
@@ -176,24 +185,24 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
TypeKind::ObjCInterface(..) |
TypeKind::ObjCId |
TypeKind::ObjCSel => {
- trace!(" simple type that can always derive PartialEq or PartialOrd");
+ trace!(" simple type that can always derive `PartialEq`/`PartialOrd`");
ConstrainResult::Same
}
TypeKind::Array(t, len) => {
if self.cannot_derive_partialeq_or_partialord.contains(&t) {
trace!(
- " arrays of T for which we cannot derive PartialEq or PartialOrd \
- also cannot derive PartialEq or PartialOrd"
+ " arrays of T for which we cannot derive `PartialEq`/`PartialOrd` \
+ also cannot derive `PartialEq`/`PartialOrd`"
);
return self.insert(id);
}
if len <= RUST_DERIVE_IN_ARRAY_LIMIT {
- trace!(" array is small enough to derive PartialEq or PartialOrd");
+ trace!(" array is small enough to derive `PartialEq`/`PartialOrd`");
ConstrainResult::Same
} else {
- trace!(" array is too large to derive PartialEq or PartialOrd");
+ trace!(" array is too large to derive `PartialEq`/`PartialOrd`");
self.insert(id)
}
}
@@ -204,7 +213,7 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
if let TypeKind::Function(ref sig) = *inner_type.kind() {
if !sig.can_trivially_derive_partialeq_or_partialord() {
trace!(
- " function pointer that can't trivially derive PartialEq or PartialOrd"
+ " function pointer that can't trivially derive `PartialEq`/`PartialOrd`"
);
return self.insert(id);
}
@@ -216,11 +225,11 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
TypeKind::Function(ref sig) => {
if !sig.can_trivially_derive_partialeq_or_partialord() {
trace!(
- " function that can't trivially derive PartialEq or PartialOrd"
+ " function that can't trivially derive `PartialEq`/`PartialOrd`"
);
return self.insert(id);
}
- trace!(" function can derive PartialEq or PartialOrd");
+ trace!(" function can derive `PartialEq`/`PartialOrd`");
ConstrainResult::Same
}
@@ -230,13 +239,13 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
if self.cannot_derive_partialeq_or_partialord.contains(&t) {
trace!(
" aliases and type refs to T which cannot derive \
- PartialEq or PartialOrd also cannot derive PartialEq or PartialOrd"
+ `PartialEq`/`PartialOrd` also cannot derive `PartialEq`/`PartialOrd`"
);
self.insert(id)
} else {
trace!(
" aliases and type refs to T which can derive \
- PartialEq or PartialOrd can also derive PartialEq or PartialOrd"
+ `PartialEq`/`PartialOrd` can also derive `PartialEq`/`PartialOrd`"
);
ConstrainResult::Same
}
@@ -250,7 +259,7 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
if info.kind() == CompKind::Union {
if self.ctx.options().rust_features().untagged_union() {
- trace!(" cannot derive PartialEq or PartialOrd for Rust unions");
+ trace!(" cannot derive `PartialEq`/`PartialOrd` for Rust unions");
return self.insert(id);
}
@@ -259,11 +268,11 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
})
{
trace!(
- " union layout can trivially derive PartialEq or PartialOrd"
+ " union layout can trivially derive `PartialEq`/`PartialOrd`"
);
return ConstrainResult::Same;
} else {
- trace!(" union layout cannot derive PartialEq or PartialOrd");
+ trace!(" union layout cannot derive `PartialEq`/`PartialOrd`");
return self.insert(id);
}
}
@@ -275,7 +284,7 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
});
if bases_cannot_derive {
trace!(
- " base members cannot derive PartialEq or PartialOrd, so we can't \
+ " base members cannot derive `PartialEq`/`PartialOrd`, so we can't \
either"
);
return self.insert(id);
@@ -312,7 +321,7 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
});
if fields_cannot_derive {
trace!(
- " fields cannot derive PartialEq or PartialOrd, so we can't either"
+ " fields cannot derive `PartialEq`/`PartialOrd`, so we can't either"
);
return self.insert(id);
}
@@ -328,8 +337,8 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
});
if args_cannot_derive {
trace!(
- " template args cannot derive PartialEq or PartialOrd, so \
- insantiation can't either"
+ " template args cannot derive `PartialEq`/`PartialOrd`, so \
+ insantiation can't either"
);
return self.insert(id);
}
@@ -343,13 +352,13 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
);
if def_cannot_derive {
trace!(
- " template definition cannot derive PartialEq or PartialOrd, so \
- insantiation can't either"
+ " template definition cannot derive `PartialEq`/`PartialOrd`, so \
+ insantiation can't either"
);
return self.insert(id);
}
- trace!(" template instantiation can derive PartialEq or PartialOrd");
+ trace!(" template instantiation can derive `PartialEq`/`PartialOrd`");
ConstrainResult::Same
}
diff --git a/src/ir/context.rs b/src/ir/context.rs
index 337e925e..b540d152 100644
--- a/src/ir/context.rs
+++ b/src/ir/context.rs
@@ -239,8 +239,9 @@ pub struct BindgenContext {
/// The set of (`ItemId`s of) types that can't derive hash.
///
- /// This is populated when we enter codegen by `compute_can_derive_partialeq`
- /// and is always `None` before that and `Some` after.
+ /// This is populated when we enter codegen by
+ /// `compute_cannot_derive_partialord_partialeq_or_eq` and is always `None`
+ /// before that and `Some` after.
cannot_derive_partialeq_or_partialord: Option<HashSet<ItemId>>,
/// The set of (`ItemId's of`) types that has vtable.
@@ -2163,8 +2164,7 @@ impl BindgenContext {
}
}
- /// Look up whether the item with `id` can
- /// derive partialeq or partialord.
+ /// Look up whether the item with `id` can derive `Partial{Eq,Ord}`.
pub fn lookup_item_id_can_derive_partialeq_or_partialord(&self, id: ItemId) -> bool {
assert!(
self.in_codegen_phase(),
@@ -2176,8 +2176,7 @@ impl BindgenContext {
!self.cannot_derive_partialeq_or_partialord.as_ref().unwrap().contains(&id)
}
- /// Look up whether the item with `id` can
- /// derive copy or not.
+ /// Look up whether the item with `id` can derive `Copy` or not.
pub fn lookup_item_id_can_derive_copy(&self, id: ItemId) -> bool {
assert!(
self.in_codegen_phase(),
diff --git a/src/ir/derive.rs b/src/ir/derive.rs
index f518152a..cafbd3b1 100644
--- a/src/ir/derive.rs
+++ b/src/ir/derive.rs
@@ -1,27 +1,32 @@
//! Traits for determining whether we can derive traits for a thing or not.
+//!
+//! These traits tend to come in pairs:
+//!
+//! 1. A "trivial" version, whose implementations aren't allowed to recursively
+//! look at other types or the results of fix point analyses.
+//!
+//! 2. A "normal" version, whose implementations simply query the results of a
+//! fix point analysis.
+//!
+//! The former is used by the analyses when creating the results queried by the
+//! second.
use super::context::BindgenContext;
/// A trait that encapsulates the logic for whether or not we can derive `Debug`
/// for a given thing.
-///
-/// This should ideally be a no-op that just returns `true`, but instead needs
-/// to be a recursive method that checks whether all the proper members can
-/// derive debug or not, because of the limit rust has on 32 items as max in the
-/// array.
pub trait CanDeriveDebug {
/// Return `true` if `Debug` can be derived for this thing, `false`
/// otherwise.
fn can_derive_debug(&self, ctx: &BindgenContext) -> bool;
}
-/// A trait that encapsulates the logic for whether or not we can derive `Debug`.
-/// The difference between this trait and the CanDeriveDebug is that the type
-/// implementing this trait cannot use recursion or lookup result from fix point
-/// analysis. It's a helper trait for fix point analysis.
+/// A trait that encapsulates the logic for whether or not we can trivially
+/// derive `Debug` without looking at any other types or the results of a fix
+/// point analysis. This is a helper trait for the fix point analysis.
pub trait CanTriviallyDeriveDebug {
- /// Return `true` if `Debug` can be derived for this thing, `false`
- /// otherwise.
+ /// Return `true` if `Debug` can trivially be derived for this thing,
+ /// `false` otherwise.
fn can_trivially_derive_debug(&self) -> bool;
}
@@ -33,72 +38,50 @@ pub trait CanDeriveCopy<'a> {
fn can_derive_copy(&'a self, ctx: &'a BindgenContext) -> bool;
}
-/// A trait that encapsulates the logic for whether or not we can derive `Copy`.
-/// The difference between this trait and the CanDeriveCopy is that the type
-/// implementing this trait cannot use recursion or lookup result from fix point
-/// analysis. It's a helper trait for fix point analysis.
+/// A trait that encapsulates the logic for whether or not we can trivially
+/// derive `Copy` without looking at any other types or results of fix point
+/// analsyses. This is a helper trait for fix point analysis.
pub trait CanTriviallyDeriveCopy {
- /// Return `true` if `Copy` can be derived for this thing, `false`
+ /// Return `true` if `Copy` can be trivially derived for this thing, `false`
/// otherwise.
fn can_trivially_derive_copy(&self) -> bool;
}
-/// A trait that encapsulates the logic for whether or not we can derive `Default`
-/// for a given thing.
-///
-/// This should ideally be a no-op that just returns `true`, but instead needs
-/// to be a recursive method that checks whether all the proper members can
-/// derive default or not, because of the limit rust has on 32 items as max in the
-/// array.
+/// A trait that encapsulates the logic for whether or not we can derive
+/// `Default` for a given thing.
pub trait CanDeriveDefault {
/// Return `true` if `Default` can be derived for this thing, `false`
/// otherwise.
fn can_derive_default(&self, ctx: &BindgenContext) -> bool;
}
-/// A trait that encapsulates the logic for whether or not we can derive `Default`.
-/// The difference between this trait and the CanDeriveDefault is that the type
-/// implementing this trait cannot use recursion or lookup result from fix point
-/// analysis. It's a helper trait for fix point analysis.
+/// A trait that encapsulates the logic for whether or not we can trivially
+/// derive `Default` without looking at any other types or results of fix point
+/// analyses. This is a helper trait for the fix point analysis.
pub trait CanTriviallyDeriveDefault {
- /// Return `true` if `Default` can be derived for this thing, `false`
+ /// Return `true` if `Default` can trivially derived for this thing, `false`
/// otherwise.
fn can_trivially_derive_default(&self) -> bool;
}
/// A trait that encapsulates the logic for whether or not we can derive `Hash`
/// for a given thing.
-///
-/// This should ideally be a no-op that just returns `true`, but instead needs
-/// to be a recursive method that checks whether all the proper members can
-/// derive default or not, because of the limit rust has on 32 items as max in the
-/// array.
pub trait CanDeriveHash {
- /// Return `true` if `Default` can be derived for this thing, `false`
+ /// Return `true` if `Hash` can be derived for this thing, `false`
/// otherwise.
fn can_derive_hash(&self, ctx: &BindgenContext) -> bool;
}
-/// A trait that encapsulates the logic for whether or not we can derive `PartialEq`
-/// for a given thing.
-///
-/// This should ideally be a no-op that just returns `true`, but instead needs
-/// to be a recursive method that checks whether all the proper members can
-/// derive default or not, because of the limit rust has on 32 items as max in the
-/// array.
+/// A trait that encapsulates the logic for whether or not we can derive
+/// `PartialEq` for a given thing.
pub trait CanDerivePartialEq {
/// Return `true` if `PartialEq` can be derived for this thing, `false`
/// otherwise.
fn can_derive_partialeq(&self, ctx: &BindgenContext) -> bool;
}
-/// A trait that encapsulates the logic for whether or not we can derive `PartialOrd`
-/// for a given thing.
-///
-/// This should ideally be a no-op that just returns `true`, but instead needs
-/// to be a recursive method that checks whether all the proper members can
-/// derive partial ord or not, because of the limit rust has on 32 items as max in the
-/// array.
+/// A trait that encapsulates the logic for whether or not we can derive
+/// `PartialOrd` for a given thing.
pub trait CanDerivePartialOrd {
/// Return `true` if `PartialOrd` can be derived for this thing, `false`
/// otherwise.
@@ -107,50 +90,32 @@ pub trait CanDerivePartialOrd {
/// A trait that encapsulates the logic for whether or not we can derive `Eq`
/// for a given thing.
-///
-/// This should ideally be a no-op that just returns `true`, but instead needs
-/// to be a recursive method that checks whether all the proper members can
-/// derive eq or not, because of the limit rust has on 32 items as max in the
-/// array.
pub trait CanDeriveEq {
-
- /// Return `true` if `Eq` can be derived for this thing, `false`
- /// otherwise.
- fn can_derive_eq(&self,
- ctx: &BindgenContext)
- -> bool;
+ /// Return `true` if `Eq` can be derived for this thing, `false` otherwise.
+ fn can_derive_eq(&self, ctx: &BindgenContext) -> bool;
}
/// A trait that encapsulates the logic for whether or not we can derive `Ord`
/// for a given thing.
-///
-/// This should ideally be a no-op that just returns `true`, but instead needs
-/// to be a recursive method that checks whether all the proper members can
-/// derive ord or not, because of the limit rust has on 32 items as max in the
-/// array.
pub trait CanDeriveOrd {
- /// Return `true` if `Ord` can be derived for this thing, `false`
- /// otherwise.
+ /// Return `true` if `Ord` can be derived for this thing, `false` otherwise.
fn can_derive_ord(&self, ctx: &BindgenContext) -> bool;
}
-/// A trait that encapsulates the logic for whether or not we can derive `Hash`.
-/// The difference between this trait and the CanDeriveHash is that the type
-/// implementing this trait cannot use recursion or lookup result from fix point
-/// analysis. It's a helper trait for fix point analysis.
+/// A trait that encapsulates the logic for whether or not we can derive `Hash`
+/// without looking at any other types or the results of any fix point
+/// analyses. This is a helper trait for the fix point analysis.
pub trait CanTriviallyDeriveHash {
- /// Return `true` if `Hash` can be derived for this thing, `false`
+ /// Return `true` if `Hash` can trivially be derived for this thing, `false`
/// otherwise.
fn can_trivially_derive_hash(&self) -> bool;
}
-/// A trait that encapsulates the logic for whether or not we can derive `PartialEq`
-/// or `PartialOrd`.
-/// The difference between this trait and the CanDerivePartialEq is that the type
-/// implementing this trait cannot use recursion or lookup result from fix point
-/// analysis. It's a helper trait for fix point analysis.
+/// A trait that encapsulates the logic for whether or not we can trivially
+/// derive `PartialEq` or `PartialOrd` without looking at any other types or
+/// results of fix point analyses. This is a helper for the fix point analysis.
pub trait CanTriviallyDerivePartialEqOrPartialOrd {
- /// Return `true` if `PartialEq` or `PartialOrd` can be derived for this thing, `false`
- /// otherwise.
+ /// Return `true` if `PartialEq` or `PartialOrd` can trivially be derived
+ /// for this thing, `false` otherwise.
fn can_trivially_derive_partialeq_or_partialord(&self) -> bool;
}
diff --git a/src/ir/function.rs b/src/ir/function.rs
index f37f0f3c..402a0230 100644
--- a/src/ir/function.rs
+++ b/src/ir/function.rs
@@ -437,6 +437,26 @@ impl FunctionSig {
// variadic functions without an initial argument.
self.is_variadic && !self.argument_types.is_empty()
}
+
+ /// Are function pointers with this signature able to derive Rust traits?
+ /// Rust only supports deriving traits for function pointers with a limited
+ /// number of parameters and a couple ABIs.
+ ///
+ /// For more details, see:
+ ///
+ /// * https://github.com/rust-lang-nursery/rust-bindgen/issues/547,
+ /// * https://github.com/rust-lang/rust/issues/38848,
+ /// * and https://github.com/rust-lang/rust/issues/40158
+ pub fn function_pointers_can_derive(&self) -> bool {
+ if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
+ return false;
+ }
+
+ match self.abi {
+ Abi::C | Abi::Unknown(..) => true,
+ _ => false,
+ }
+ }
}
impl ClangSubItemParser for Function {
@@ -523,48 +543,20 @@ impl Trace for FunctionSig {
}
}
-// Function pointers follow special rules, see:
-//
-// https://github.com/rust-lang-nursery/rust-bindgen/issues/547,
-// https://github.com/rust-lang/rust/issues/38848,
-// and https://github.com/rust-lang/rust/issues/40158
-//
-// Note that copy is always derived, so we don't need to implement it.
impl CanTriviallyDeriveDebug for FunctionSig {
fn can_trivially_derive_debug(&self) -> bool {
- if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
- return false;
- }
-
- match self.abi {
- Abi::C | Abi::Unknown(..) => true,
- _ => false,
- }
+ self.function_pointers_can_derive()
}
}
impl CanTriviallyDeriveHash for FunctionSig {
fn can_trivially_derive_hash(&self) -> bool {
- if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
- return false;
- }
-
- match self.abi {
- Abi::C | Abi::Unknown(..) => true,
- _ => false,
- }
+ self.function_pointers_can_derive()
}
}
impl CanTriviallyDerivePartialEqOrPartialOrd for FunctionSig {
fn can_trivially_derive_partialeq_or_partialord(&self) -> bool {
- if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
- return false;
- }
-
- match self.abi {
- Abi::C | Abi::Unknown(..) => true,
- _ => false,
- }
+ self.function_pointers_can_derive()
}
}
diff --git a/src/ir/layout.rs b/src/ir/layout.rs
index 298fe54a..2df15ef2 100644
--- a/src/ir/layout.rs
+++ b/src/ir/layout.rs
@@ -104,44 +104,43 @@ impl Opaque {
None
}
}
-}
-impl CanTriviallyDeriveDebug for Opaque {
- fn can_trivially_derive_debug(&self) -> bool {
+ /// Return `true` if this opaque layout's array size will fit within the
+ /// maximum number of array elements that Rust allows deriving traits
+ /// with. Return `false` otherwise.
+ pub fn array_size_within_derive_limit(&self) -> bool {
self.array_size().map_or(false, |size| {
size <= RUST_DERIVE_IN_ARRAY_LIMIT
})
}
}
+impl CanTriviallyDeriveDebug for Opaque {
+ fn can_trivially_derive_debug(&self) -> bool {
+ self.array_size_within_derive_limit()
+ }
+}
+
impl CanTriviallyDeriveDefault for Opaque {
fn can_trivially_derive_default(&self) -> bool {
- self.array_size().map_or(false, |size| {
- size <= RUST_DERIVE_IN_ARRAY_LIMIT
- })
+ self.array_size_within_derive_limit()
}
}
impl CanTriviallyDeriveCopy for Opaque {
fn can_trivially_derive_copy(&self) -> bool {
- self.array_size().map_or(false, |size| {
- size <= RUST_DERIVE_IN_ARRAY_LIMIT
- })
+ self.array_size_within_derive_limit()
}
}
impl CanTriviallyDeriveHash for Opaque {
fn can_trivially_derive_hash(&self) -> bool {
- self.array_size().map_or(false, |size| {
- size <= RUST_DERIVE_IN_ARRAY_LIMIT
- })
+ self.array_size_within_derive_limit()
}
}
impl CanTriviallyDerivePartialEqOrPartialOrd for Opaque {
fn can_trivially_derive_partialeq_or_partialord(&self) -> bool {
- self.array_size().map_or(false, |size| {
- size <= RUST_DERIVE_IN_ARRAY_LIMIT
- })
+ self.array_size_within_derive_limit()
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 0eb6bbae..dbb9c268 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -857,8 +857,9 @@ impl Builder {
}
/// Set whether `PartialEq` should be derived by default.
- /// If we don't compute partialeq, we also cannot compute
- /// eq. Set the derive_eq to `false` when doit is `false`.
+ ///
+ /// If we don't derive `PartialEq`, we also cannot derive `Eq`, so deriving
+ /// `Eq` is also disabled when `doit` is `false`.
pub fn derive_partialeq(mut self, doit: bool) -> Self {
self.options.derive_partialeq = doit;
if !doit {
@@ -868,16 +869,19 @@ impl Builder {
}
/// Set whether `Eq` should be derived by default.
- /// We can't compute Eq without computing PartialEq, so
- /// we set the same option to derive_partialeq.
+ ///
+ /// We can't derive `Eq` without also deriving `PartialEq`, so we also
+ /// enable deriving `PartialEq` when `doit` is `true`.
pub fn derive_eq(mut self, doit: bool) -> Self {
self.options.derive_eq = doit;
- self.options.derive_partialeq = doit;
+ if doit {
+ self.options.derive_partialeq = doit;
+ }
self
}
- /// Set whether or not to time bindgen phases, and print
- /// information to stderr.
+ /// Set whether or not to time bindgen phases, and print information to
+ /// stderr.
pub fn time_phases(mut self, doit: bool) -> Self {
self.options.time_phases = doit;
self