Skip to content

Commit 5359879

Browse files
author
Jorge Aparicio
committed
libcore: convert unop traits to by value
1 parent 5caebb2 commit 5359879

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

src/libcore/ops.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,16 @@ rem_float_impl! { f64, fmod }
542542
/// -Foo;
543543
/// }
544544
/// ```
545+
// NOTE(stage0): Remove trait after a snapshot
546+
#[cfg(stage0)]
545547
#[lang="neg"]
546548
pub trait Neg<Result> for Sized? {
547549
/// The method for the unary `-` operator
548550
fn neg(&self) -> Result;
549551
}
550552

553+
// NOTE(stage0): Remove macro after a snapshot
554+
#[cfg(stage0)]
551555
macro_rules! neg_impl {
552556
($($t:ty)*) => ($(
553557
impl Neg<$t> for $t {
@@ -557,6 +561,8 @@ macro_rules! neg_impl {
557561
)*)
558562
}
559563

564+
// NOTE(stage0): Remove macro after a snapshot
565+
#[cfg(stage0)]
560566
macro_rules! neg_uint_impl {
561567
($t:ty, $t_signed:ty) => {
562568
impl Neg<$t> for $t {
@@ -566,6 +572,56 @@ macro_rules! neg_uint_impl {
566572
}
567573
}
568574

575+
/// The `Neg` trait is used to specify the functionality of unary `-`.
576+
///
577+
/// # Example
578+
///
579+
/// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
580+
/// `neg`, and therefore, `main` prints `Negating!`.
581+
///
582+
/// ```
583+
/// struct Foo;
584+
///
585+
/// impl Copy for Foo {}
586+
///
587+
/// impl Neg<Foo> for Foo {
588+
/// fn neg(self) -> Foo {
589+
/// println!("Negating!");
590+
/// self
591+
/// }
592+
/// }
593+
///
594+
/// fn main() {
595+
/// -Foo;
596+
/// }
597+
/// ```
598+
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
599+
#[lang="neg"]
600+
pub trait Neg<Result> {
601+
/// The method for the unary `-` operator
602+
fn neg(self) -> Result;
603+
}
604+
605+
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
606+
macro_rules! neg_impl {
607+
($($t:ty)*) => ($(
608+
impl Neg<$t> for $t {
609+
#[inline]
610+
fn neg(self) -> $t { -self }
611+
}
612+
)*)
613+
}
614+
615+
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
616+
macro_rules! neg_uint_impl {
617+
($t:ty, $t_signed:ty) => {
618+
impl Neg<$t> for $t {
619+
#[inline]
620+
fn neg(self) -> $t { -(self as $t_signed) as $t }
621+
}
622+
}
623+
}
624+
569625
neg_impl! { int i8 i16 i32 i64 f32 f64 }
570626

571627
neg_uint_impl! { uint, int }
@@ -598,13 +654,17 @@ neg_uint_impl! { u64, i64 }
598654
/// !Foo;
599655
/// }
600656
/// ```
657+
// NOTE(stage0): Remove macro after a snapshot
658+
#[cfg(stage0)]
601659
#[lang="not"]
602660
pub trait Not<Result> for Sized? {
603661
/// The method for the unary `!` operator
604662
fn not(&self) -> Result;
605663
}
606664

607665

666+
// NOTE(stage0): Remove macro after a snapshot
667+
#[cfg(stage0)]
608668
macro_rules! not_impl {
609669
($($t:ty)*) => ($(
610670
impl Not<$t> for $t {
@@ -614,6 +674,46 @@ macro_rules! not_impl {
614674
)*)
615675
}
616676

677+
/// The `Not` trait is used to specify the functionality of unary `!`.
678+
///
679+
/// # Example
680+
///
681+
/// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
682+
/// `not`, and therefore, `main` prints `Not-ing!`.
683+
///
684+
/// ```
685+
/// struct Foo;
686+
///
687+
/// impl Copy for Foo {}
688+
///
689+
/// impl Not<Foo> for Foo {
690+
/// fn not(self) -> Foo {
691+
/// println!("Not-ing!");
692+
/// self
693+
/// }
694+
/// }
695+
///
696+
/// fn main() {
697+
/// !Foo;
698+
/// }
699+
/// ```
700+
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
701+
#[lang="not"]
702+
pub trait Not<Result> {
703+
/// The method for the unary `!` operator
704+
fn not(self) -> Result;
705+
}
706+
707+
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
708+
macro_rules! not_impl {
709+
($($t:ty)*) => ($(
710+
impl Not<$t> for $t {
711+
#[inline]
712+
fn not(self) -> $t { !self }
713+
}
714+
)*)
715+
}
716+
617717
not_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 }
618718

619719
/// The `BitAnd` trait is used to specify the functionality of `&`.

0 commit comments

Comments
 (0)