-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the problem with deriving Debug with other attributes (#15)
* derive: Remove Debug from derive list ... in case we are emitting custom Debug impl trait implementation. Otherwise it would conflict with the default Debug implementation. Signed-off-by: Jinank Jain <[email protected]> * tests: Add new tests for derive Debug There are two new things added: 1. OpenEnum which mimics the failing build scenario 2. Struct which embeds that enum inside it and also derive Debug on top of it. By doing so we verify first of all the code compiles with multiple derive attributes and secondly the embedded debug derives works as expected. Signed-off-by: Jinank Jain <[email protected]> * Improve stdlib-derive detection logic It will now check against a (module, Name) pair to determine how to intercept PartialEq, Eq, and Debug. Fixes #14. --------- Signed-off-by: Jinank Jain <[email protected]> Co-authored-by: Alyssa Haroldsen <[email protected]>
- Loading branch information
1 parent
9c1f1b3
commit c9a5730
Showing
3 changed files
with
187 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright © 2023 Microsoft Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
extern crate open_enum; | ||
use open_enum::*; | ||
|
||
#[open_enum] | ||
#[derive( | ||
Debug, Clone, Copy, PartialEq, Eq, zerocopy::AsBytes, zerocopy::FromBytes, zerocopy::FromZeroes, | ||
)] | ||
#[repr(u32)] | ||
pub enum Color { | ||
Red = 1, | ||
Blue = 2, | ||
} | ||
|
||
#[open_enum] | ||
#[derive( | ||
core::fmt::Debug, | ||
std::clone::Clone, | ||
::core::marker::Copy, | ||
std::cmp::PartialEq, | ||
::core::cmp::Eq, | ||
zerocopy::AsBytes, | ||
::zerocopy::FromBytes, | ||
zerocopy::FromZeroes, | ||
)] | ||
#[repr(u32)] | ||
pub enum ColorWithNonPreludeDerives { | ||
Red = 1, | ||
Blue = 2, | ||
} | ||
|
||
// Ensure that `Color` actually implements the `derive`d traits. | ||
#[derive( | ||
Debug, Copy, Clone, PartialEq, Eq, zerocopy::AsBytes, zerocopy::FromBytes, zerocopy::FromZeroes, | ||
)] | ||
#[repr(C)] | ||
pub struct EmbedColor { | ||
pub color: Color, | ||
} | ||
|
||
#[derive( | ||
Debug, Copy, Clone, PartialEq, Eq, zerocopy::AsBytes, zerocopy::FromBytes, zerocopy::FromZeroes, | ||
)] | ||
#[repr(C)] | ||
pub struct EmbedColorWithNonPreludeDerives { | ||
pub color: ColorWithNonPreludeDerives, | ||
} | ||
|
||
#[test] | ||
fn embedded_enum_struct_partialeq() { | ||
assert_eq!( | ||
EmbedColor { color: Color::Red }, | ||
EmbedColor { color: Color::Red } | ||
); | ||
assert_ne!( | ||
EmbedColor { color: Color::Red }, | ||
EmbedColor { color: Color::Blue } | ||
); | ||
} | ||
|
||
#[test] | ||
fn embedded_enum_struct_debug() { | ||
let debug_str = format!("{:?}", EmbedColor { color: Color::Red }); | ||
assert!(debug_str.contains("Red"), "{debug_str}"); | ||
} | ||
|
||
#[test] | ||
fn extended_embedded_enum_struct_debug() { | ||
let debug_str = format!( | ||
"{:?}", | ||
EmbedColorWithNonPreludeDerives { | ||
color: ColorWithNonPreludeDerives::Red | ||
} | ||
); | ||
assert!(debug_str.contains("Red"), "{debug_str}"); | ||
} |