Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various cleanups of macro-expanding-to-module workarounds #10719

Merged
merged 3 commits into from
Nov 29, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions src/libstd/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,31 +1036,26 @@ pub fn upperhex(buf: &[u8], f: &mut Formatter) {
f.pad_integral(local.slice_to(buf.len()), "0x", true);
}

// FIXME(#4375) shouldn't need an inner module
macro_rules! integer(($signed:ident, $unsigned:ident) => {
mod $signed {
use super::*;

// Signed is special because it actuall emits the negative sign,
// nothing else should do that, however.
impl Signed for $signed {
fn fmt(c: &$signed, f: &mut Formatter) {
::$unsigned::to_str_bytes(c.abs() as $unsigned, 10, |buf| {
f.pad_integral(buf, "", *c >= 0);
})
}
// Signed is special because it actuall emits the negative sign,
// nothing else should do that, however.
impl Signed for $signed {
fn fmt(c: &$signed, f: &mut Formatter) {
::$unsigned::to_str_bytes(c.abs() as $unsigned, 10, |buf| {
f.pad_integral(buf, "", *c >= 0);
})
}
int_base!($signed, $unsigned, 2, Binary, "0b")
int_base!($signed, $unsigned, 8, Octal, "0o")
int_base!($signed, $unsigned, 16, LowerHex, "0x")
upper_hex!($signed, $unsigned)

int_base!($unsigned, $unsigned, 2, Binary, "0b")
int_base!($unsigned, $unsigned, 8, Octal, "0o")
int_base!($unsigned, $unsigned, 10, Unsigned, "")
int_base!($unsigned, $unsigned, 16, LowerHex, "0x")
upper_hex!($unsigned, $unsigned)
}
int_base!($signed, $unsigned, 2, Binary, "0b")
int_base!($signed, $unsigned, 8, Octal, "0o")
int_base!($signed, $unsigned, 16, LowerHex, "0x")
upper_hex!($signed, $unsigned)

int_base!($unsigned, $unsigned, 2, Binary, "0b")
int_base!($unsigned, $unsigned, 8, Octal, "0o")
int_base!($unsigned, $unsigned, 10, Unsigned, "")
int_base!($unsigned, $unsigned, 16, LowerHex, "0x")
upper_hex!($unsigned, $unsigned)
})

integer!(int, uint)
Expand Down
33 changes: 13 additions & 20 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
//! Operations and constants for `f32`
#[allow(missing_doc)];

use prelude::*;

use cmath::c_float_utils;
use default::Default;
use libc::c_int;
use num::{Zero, One, strconv};
use libc::{c_float, c_int};
use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
use num::{Zero, One, strconv};
use num;
use prelude::*;
use to_str;
use unstable::intrinsics;

pub use cmath::c_float_targ_consts::*;

use self::delegated::*;

macro_rules! delegate(
(
$(
Expand All @@ -33,22 +34,14 @@ macro_rules! delegate(
) -> $rv:ty = $bound_name:path
),*
) => (
// An inner module is required to get the #[inline] attribute on the
// functions.
mod delegated {
use cmath::c_float_utils;
use libc::{c_float, c_int};
use unstable::intrinsics;

$(
#[inline]
pub fn $name($( $arg : $arg_ty ),*) -> $rv {
unsafe {
$bound_name($( $arg ),*)
}
$(
#[inline]
pub fn $name($( $arg : $arg_ty ),*) -> $rv {
unsafe {
$bound_name($( $arg ),*)
}
)*
}
}
)*
)
)

Expand Down
33 changes: 13 additions & 20 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@

#[allow(missing_doc)];

use prelude::*;

use cmath::c_double_utils;
use default::Default;
use libc::c_int;
use num::{Zero, One, strconv};
use libc::{c_double, c_int};
use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
use num::{Zero, One, strconv};
use num;
use prelude::*;
use to_str;
use unstable::intrinsics;

pub use cmath::c_double_targ_consts::*;
pub use cmp::{min, max};

use self::delegated::*;

macro_rules! delegate(
(
$(
Expand All @@ -35,22 +36,14 @@ macro_rules! delegate(
) -> $rv:ty = $bound_name:path
),*
) => (
// An inner module is required to get the #[inline] attribute on the
// functions.
mod delegated {
use cmath::c_double_utils;
use libc::{c_double, c_int};
use unstable::intrinsics;

$(
#[inline]
pub fn $name($( $arg : $arg_ty ),*) -> $rv {
unsafe {
$bound_name($( $arg ),*)
}
$(
#[inline]
pub fn $name($( $arg : $arg_ty ),*) -> $rv {
unsafe {
$bound_name($( $arg ),*)
}
)*
}
}
)*
)
)

Expand Down
4 changes: 3 additions & 1 deletion src/libstd/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// FIXME(#4375): this shouldn't have to be a nested module named 'generated'
// FIXME(#4375): This shouldn't have to be a nested module named 'generated'...
// FIXME(#10716): ... but now that we could solve that, the import lines and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should remove the previous FIXME because it's no longer an open issue. I would personally be ok with just copy-pasting all of the attributes/include statements in each of the files using this macro (it's a big step forward in having reasonable documentation for these modules)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, will do that then

// attributes still prevent a removal of that module.

#[macro_escape];
#[doc(hidden)];
Expand Down
4 changes: 3 additions & 1 deletion src/libstd/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// FIXME(#4375): this shouldn't have to be a nested module named 'generated'
// FIXME(#4375): This shouldn't have to be a nested module named 'generated'...
// FIXME(#10716): ... but now that we could solve that, the import lines and
// attributes still prevent a removal of that module.

#[macro_escape];
#[doc(hidden)];
Expand Down
184 changes: 89 additions & 95 deletions src/libstd/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
#[allow(missing_doc)];

use clone::Clone;

pub use self::inner::*;
#[cfg(not(test))] use cmp::*;
#[cfg(not(test))] use default::Default;
#[cfg(not(test))] use num::Zero;

/// Method extensions to pairs where both types satisfy the `Clone` bound
pub trait CopyableTuple<T, U> {
Expand Down Expand Up @@ -86,116 +87,109 @@ macro_rules! tuple_impls {
})+
}
)+) => {
pub mod inner {
use clone::Clone;
#[cfg(not(test))] use cmp::*;
#[cfg(not(test))] use default::Default;
#[cfg(not(test))] use num::Zero;

$(
pub trait $move_trait<$($T),+> {
$(fn $get_fn(self) -> $T;)+
}
$(
pub trait $move_trait<$($T),+> {
$(fn $get_fn(self) -> $T;)+
}

impl<$($T),+> $move_trait<$($T),+> for ($($T,)+) {
$(
#[inline]
fn $get_fn(self) -> $T {
let $move_pattern = self;
$ret
}
)+
}

pub trait $immutable_trait<$($T),+> {
$(fn $get_ref_fn<'a>(&'a self) -> &'a $T;)+
}
impl<$($T),+> $move_trait<$($T),+> for ($($T,)+) {
$(
#[inline]
fn $get_fn(self) -> $T {
let $move_pattern = self;
$ret
}
)+
}

impl<$($T),+> $immutable_trait<$($T),+> for ($($T,)+) {
$(
#[inline]
fn $get_ref_fn<'a>(&'a self) -> &'a $T {
let $ref_pattern = *self;
$ret
}
)+
}
pub trait $immutable_trait<$($T),+> {
$(fn $get_ref_fn<'a>(&'a self) -> &'a $T;)+
}

impl<$($T:Clone),+> Clone for ($($T,)+) {
fn clone(&self) -> ($($T,)+) {
($(self.$get_ref_fn().clone(),)+)
impl<$($T),+> $immutable_trait<$($T),+> for ($($T,)+) {
$(
#[inline]
fn $get_ref_fn<'a>(&'a self) -> &'a $T {
let $ref_pattern = *self;
$ret
}
)+
}

impl<$($T:Clone),+> Clone for ($($T,)+) {
fn clone(&self) -> ($($T,)+) {
($(self.$get_ref_fn().clone(),)+)
}
}

#[cfg(not(test))]
impl<$($T:Eq),+> Eq for ($($T,)+) {
#[inline]
fn eq(&self, other: &($($T,)+)) -> bool {
$(*self.$get_ref_fn() == *other.$get_ref_fn())&&+
}
#[inline]
fn ne(&self, other: &($($T,)+)) -> bool {
$(*self.$get_ref_fn() != *other.$get_ref_fn())||+
}
#[cfg(not(test))]
impl<$($T:Eq),+> Eq for ($($T,)+) {
#[inline]
fn eq(&self, other: &($($T,)+)) -> bool {
$(*self.$get_ref_fn() == *other.$get_ref_fn())&&+
}
#[inline]
fn ne(&self, other: &($($T,)+)) -> bool {
$(*self.$get_ref_fn() != *other.$get_ref_fn())||+
}
}

#[cfg(not(test))]
impl<$($T:TotalEq),+> TotalEq for ($($T,)+) {
#[inline]
fn equals(&self, other: &($($T,)+)) -> bool {
$(self.$get_ref_fn().equals(other.$get_ref_fn()))&&+
}
#[cfg(not(test))]
impl<$($T:TotalEq),+> TotalEq for ($($T,)+) {
#[inline]
fn equals(&self, other: &($($T,)+)) -> bool {
$(self.$get_ref_fn().equals(other.$get_ref_fn()))&&+
}
}

#[cfg(not(test))]
impl<$($T:Ord + Eq),+> Ord for ($($T,)+) {
#[inline]
fn lt(&self, other: &($($T,)+)) -> bool {
lexical_ord!(lt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
}
#[inline]
fn le(&self, other: &($($T,)+)) -> bool {
lexical_ord!(le, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
}
#[inline]
fn ge(&self, other: &($($T,)+)) -> bool {
lexical_ord!(ge, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
}
#[inline]
fn gt(&self, other: &($($T,)+)) -> bool {
lexical_ord!(gt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
}
#[cfg(not(test))]
impl<$($T:Ord + Eq),+> Ord for ($($T,)+) {
#[inline]
fn lt(&self, other: &($($T,)+)) -> bool {
lexical_ord!(lt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
}
#[inline]
fn le(&self, other: &($($T,)+)) -> bool {
lexical_ord!(le, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
}
#[inline]
fn ge(&self, other: &($($T,)+)) -> bool {
lexical_ord!(ge, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
}
#[inline]
fn gt(&self, other: &($($T,)+)) -> bool {
lexical_ord!(gt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
}
}

#[cfg(not(test))]
impl<$($T:TotalOrd),+> TotalOrd for ($($T,)+) {
#[inline]
fn cmp(&self, other: &($($T,)+)) -> Ordering {
lexical_cmp!($(self.$get_ref_fn(), other.$get_ref_fn()),+)
}
#[cfg(not(test))]
impl<$($T:TotalOrd),+> TotalOrd for ($($T,)+) {
#[inline]
fn cmp(&self, other: &($($T,)+)) -> Ordering {
lexical_cmp!($(self.$get_ref_fn(), other.$get_ref_fn()),+)
}
}

#[cfg(not(test))]
impl<$($T:Default),+> Default for ($($T,)+) {
#[inline]
fn default() -> ($($T,)+) {
($({ let x: $T = Default::default(); x},)+)
}
#[cfg(not(test))]
impl<$($T:Default),+> Default for ($($T,)+) {
#[inline]
fn default() -> ($($T,)+) {
($({ let x: $T = Default::default(); x},)+)
}
}

#[cfg(not(test))]
impl<$($T:Zero),+> Zero for ($($T,)+) {
#[inline]
fn zero() -> ($($T,)+) {
($({ let x: $T = Zero::zero(); x},)+)
}
#[inline]
fn is_zero(&self) -> bool {
$(self.$get_ref_fn().is_zero())&&+
}
#[cfg(not(test))]
impl<$($T:Zero),+> Zero for ($($T,)+) {
#[inline]
fn zero() -> ($($T,)+) {
($({ let x: $T = Zero::zero(); x},)+)
}
)+
}
#[inline]
fn is_zero(&self) -> bool {
$(self.$get_ref_fn().is_zero())&&+
}
}
)+
}
}

Expand Down
Loading