Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not derive Copy/Debug for some opaque types
When we treat a type as opaque, either because it is explicitly annotated as such or because it is a template that has a non-type parameter, we need to check if the size is larger than RUST_DERIVE_IN_ARRAY_LIMIT. Performing this check requires information that is up the stack, in the `Item` rather than in the `CompInfo` or `Type`. Therefore, I introduced two traits to encapsulate whether a thing can derive `Debug` and `Copy` (the `CanDeriveDebug` and `CanDeriveCopy` traits, respectively) and implemented them for ALL THE THINGS. This allowes us to perform our various checks at the level where we have access to the necessary info, and to let sub-levels do their own checks with their sub-info. Fixes #372.
- Loading branch information
Showing
10 changed files
with
436 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//! Traits for determining whether we can derive traits for a thing or not. | ||
|
||
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 { | ||
/// Implementations can define this type to get access to any extra | ||
/// information required to determine whether they can derive `Debug`. If | ||
/// extra information is unneeded, then this should simply be the unit type. | ||
type Extra; | ||
|
||
/// Return `true` if `Debug` can be derived for this thing, `false` | ||
/// otherwise. | ||
fn can_derive_debug(&self, | ||
ctx: &BindgenContext, | ||
extra: Self::Extra) | ||
-> bool; | ||
} | ||
|
||
/// A trait that encapsulates the logic for whether or not we can derive `Copy` | ||
/// for a given thing. | ||
pub trait CanDeriveCopy<'a> { | ||
/// Implementations can define this type to get access to any extra | ||
/// information required to determine whether they can derive `Copy`. If | ||
/// extra information is unneeded, then this should simply be the unit type. | ||
type Extra; | ||
|
||
/// Return `true` if `Copy` can be derived for this thing, `false` | ||
/// otherwise. | ||
fn can_derive_copy(&'a self, | ||
ctx: &'a BindgenContext, | ||
extra: Self::Extra) | ||
-> bool; | ||
|
||
/// For some reason, deriving copies of an array of a type that is not known | ||
/// to be `Copy` is a compile error. e.g.: | ||
/// | ||
/// ```rust | ||
/// #[derive(Copy, Clone)] | ||
/// struct A<T> { | ||
/// member: T, | ||
/// } | ||
/// ``` | ||
/// | ||
/// is fine, while: | ||
/// | ||
/// ```rust,ignore | ||
/// #[derive(Copy, Clone)] | ||
/// struct A<T> { | ||
/// member: [T; 1], | ||
/// } | ||
/// ``` | ||
/// | ||
/// is an error. | ||
/// | ||
/// That's the whole point of the existence of `can_derive_copy_in_array`. | ||
fn can_derive_copy_in_array(&'a self, | ||
ctx: &'a BindgenContext, | ||
extra: Self::Extra) | ||
-> bool; | ||
} |
Oops, something went wrong.