Skip to content

Commit

Permalink
Use #[derive] for Copy/Clone in s! and friends.
Browse files Browse the repository at this point in the history
Allows use of `#[cfg]` to skip items in an `s!` block without creating
invalid, orphaned `impl` blocks.

(backport <rust-lang#4038>)
(cherry picked from commit 44c548d)
  • Loading branch information
bossmc authored and tgross35 committed Nov 18, 2024
1 parent d098f04 commit d528e8a
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,11 @@ macro_rules! s {
__item! {
#[repr(C)]
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
#[derive(Copy, Clone)]
#[allow(deprecated)]
$(#[$attr])*
pub struct $i { $($field)* }
}
#[allow(deprecated)]
impl ::Copy for $i {}
#[allow(deprecated)]
impl ::Clone for $i {
fn clone(&self) -> $i { *self }
}
);
}

Expand All @@ -106,13 +101,10 @@ macro_rules! s_paren {
)* ) => ($(
__item! {
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
#[derive(Copy, Clone)]
$(#[$attr])*
pub struct $i ( $($field)* );
}
impl ::Copy for $i {}
impl ::Clone for $i {
fn clone(&self) -> $i { *self }
}
)*);
}

Expand All @@ -130,28 +122,19 @@ macro_rules! s_no_extra_traits {
(it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
__item! {
#[repr(C)]
#[derive(Copy, Clone)]
$(#[$attr])*
pub union $i { $($field)* }
}

impl ::Copy for $i {}
impl ::Clone for $i {
fn clone(&self) -> $i { *self }
}
);

(it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
__item! {
#[repr(C)]
#[derive(Copy, Clone)]
$(#[$attr])*
pub struct $i { $($field)* }
}
#[allow(deprecated)]
impl ::Copy for $i {}
#[allow(deprecated)]
impl ::Clone for $i {
fn clone(&self) -> $i { *self }
}
);
}

Expand All @@ -177,13 +160,10 @@ macro_rules! e {
)*) => ($(
__item! {
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
#[derive(Copy, Clone)]
$(#[$attr])*
pub enum $i { $($field)* }
}
impl ::Copy for $i {}
impl ::Clone for $i {
fn clone(&self) -> $i { *self }
}
)*);
}

Expand Down

0 comments on commit d528e8a

Please sign in to comment.