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

Improve the experience of using syntax extensions with #![no_std] #17100

Closed
wants to merge 9 commits into from
3 changes: 2 additions & 1 deletion src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ fn oom() -> ! {
#[doc(hidden)]
pub fn fixme_14344_be_sure_to_link_to_collections() {}

#[cfg(not(test))]
// NOTE: Remove after next snapshot
#[cfg(stage0, not(test))]
#[doc(hidden)]
mod std {
pub use core::fmt;
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ use core::iter::{Chain, Enumerate, Repeat, Skip, Take};
use core::iter;
use core::slice;
use core::uint;
use std::hash;

use {Mutable, Set, MutableSet, MutableSeq};
use vec::Vec;
use hash;

type MatchWords<'a> = Chain<MaskWords<'a>, Skip<Take<Enumerate<Repeat<uint>>>>>;
// Take two BitV's, and return iterators of their words, where the shorter one
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ use core::fmt;
use core::iter;
use core::mem;
use core::ptr;
use std::hash::{Writer, Hash};

use {Mutable, Deque, MutableSeq};
use hash::{Writer, Hash};

/// A doubly-linked list.
pub struct DList<T> {
Expand Down Expand Up @@ -742,13 +742,13 @@ impl<S: Writer, A: Hash<S>> Hash<S> for DList<A> {
mod tests {
use std::prelude::*;
use std::rand;
use std::hash;
use test::Bencher;
use test;

use {Deque, MutableSeq};
use super::{DList, Node, ListInsertion};
use vec::Vec;
use hash;

pub fn check_links<T>(list: &DList<T>) {
let mut len = 0u;
Expand Down
12 changes: 10 additions & 2 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ extern crate alloc;
#[cfg(test)] extern crate native;
#[cfg(test)] extern crate test;
#[cfg(test)] extern crate debug;
#[cfg(test)] extern crate std;

#[cfg(test)] #[phase(plugin, link)] extern crate std;
#[cfg(test)] #[phase(plugin, link)] extern crate log;

use core::prelude::Option;
Expand Down Expand Up @@ -556,10 +556,12 @@ pub trait Deque<T> : MutableSeq<T> {
#[doc(hidden)]
pub fn fixme_14344_be_sure_to_link_to_collections() {}

// NOTE: Remove after next snapshot
#[cfg(stage0)]
#[cfg(not(test))]
mod std {
pub use core::fmt; // necessary for fail!()
pub use core::option; // necessary for fail!()
pub use core::fmt; // necessary for fail!()
pub use core::clone; // deriving(Clone)
pub use core::cmp; // deriving(Eq, Ord, etc.)
pub use hash; // deriving(Hash)
Expand All @@ -568,3 +570,9 @@ mod std {
pub use MutableSeq;
}
}

mod collections {
pub use hash; // deriving(Hsah)
pub use string; // format!()
pub use vec; // vec!()
}
20 changes: 19 additions & 1 deletion src/libcollections/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,30 @@
#![macro_escape]

/// Creates a `std::vec::Vec` containing the arguments.
#[macro_export]
macro_rules! vec(
($($e:expr),*) => ({
// leading _ to allow empty construction without a warning.
let mut _temp = ::vec::Vec::new();
let mut _temp = ::collections::vec::Vec::new();
$(_temp.push($e);)*
_temp
});
($($e:expr),+,) => (vec!($($e),+))
)

/// Use the syntax described in `core::fmt` to create a value of type `String`.
/// See `core::fmt` for more information.
///
/// # Example
///
/// ```
/// format!("test");
/// format!("hello {}", "world!");
/// format!("x = {}, y = {y}", 10i, y = 30i);
/// ```
#[macro_export]
macro_rules! format(
($($arg:tt)*) => (
format_args!(::collections::string::String::format, $($arg)*)
)
)
4 changes: 2 additions & 2 deletions src/libcollections/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use core::cmp;
use core::default::Default;
use core::fmt;
use core::iter;
use std::hash::{Writer, Hash};

use {Deque, Mutable, MutableSeq};
use vec::Vec;
use hash::{Writer, Hash};

static INITIAL_CAPACITY: uint = 8u; // 2^3
static MINIMUM_CAPACITY: uint = 2u;
Expand Down Expand Up @@ -527,13 +527,13 @@ mod tests {
use std::fmt::Show;
use std::prelude::*;
use std::gc::{GC, Gc};
use std::hash;
use test::Bencher;
use test;

use {Deque, Mutable, MutableSeq};
use super::RingBuf;
use vec::Vec;
use hash;

#[test]
fn test_simple() {
Expand Down
22 changes: 12 additions & 10 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,12 @@ pub mod raw {

#[cfg(test)]
mod tests {
use core::prelude::*;
use alloc::boxed::Box;
use collections::string::String;
use std::cell::Cell;
use std::default::Default;
use std::mem;
use std::prelude::*;
use std::rand::{Rng, task_rng};
use std::rc::Rc;
use std::rt;
Expand Down Expand Up @@ -1183,7 +1185,7 @@ mod tests {
assert_eq!(it.next(), None);
}
{
let v = ["Hello".to_string()];
let v = [String::from_str("Hello")];
let mut it = v.permutations();
let (min_size, max_opt) = it.size_hint();
assert_eq!(min_size, 1);
Expand Down Expand Up @@ -1925,20 +1927,20 @@ mod tests {
})
)
let empty: Vec<int> = vec![];
test_show_vec!(empty, "[]".to_string());
test_show_vec!(vec![1i], "[1]".to_string());
test_show_vec!(vec![1i, 2, 3], "[1, 2, 3]".to_string());
test_show_vec!(empty, String::from_str("[]"));
test_show_vec!(vec![1i], String::from_str("[1]"));
test_show_vec!(vec![1i, 2, 3], String::from_str("[1, 2, 3]"));
test_show_vec!(vec![vec![], vec![1u], vec![1u, 1u]],
"[[], [1], [1, 1]]".to_string());
String::from_str("[[], [1], [1, 1]]"));

let empty_mut: &mut [int] = &mut[];
test_show_vec!(empty_mut, "[]".to_string());
test_show_vec!(empty_mut, String::from_str("[]"));
let v: &mut[int] = &mut[1];
test_show_vec!(v, "[1]".to_string());
test_show_vec!(v, String::from_str("[1]"));
let v: &mut[int] = &mut[1, 2, 3];
test_show_vec!(v, "[1, 2, 3]".to_string());
test_show_vec!(v, String::from_str("[1, 2, 3]"));
let v: &mut [&mut[uint]] = &mut[&mut[], &mut[1u], &mut[1u, 1u]];
test_show_vec!(v, "[[], [1], [1, 1]]".to_string());
test_show_vec!(v, String::from_str("[[], [1], [1, 1]]"));
}

#[test]
Expand Down
7 changes: 5 additions & 2 deletions src/libcollections/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,13 @@ pub type Values<'a, T> =

#[cfg(test)]
mod test_map {
use std::prelude::*;
use core::prelude::*;
use vec::Vec;
use hash;

use {Map, MutableMap, Mutable, MutableSeq};
use super::SmallIntMap;
use string::String;

#[test]
fn test_find_mut() {
Expand Down Expand Up @@ -764,6 +765,8 @@ mod test_map {

#[test]
fn test_show() {
use std::to_string::ToString;
Copy link
Member

Choose a reason for hiding this comment

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

Import the trait but still remove the to_string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's needed for this call below:

let map_str = map.to_string();

Copy link
Member

Choose a reason for hiding this comment

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

Hm; why is there the to_string -> String::from_str change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When libcollections is built with cfg(test) it links the real libstd. This means that to_string returns an std::string::String, re-exported from a previous build of libcollections, rather than the string::String defined in the crate under test. We can still compare them with .as_slice(), but for most purposes it's better to use string::String.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, right. Different types with the same name!


let mut map = SmallIntMap::new();
let empty = SmallIntMap::<int>::new();

Expand All @@ -773,7 +776,7 @@ mod test_map {
let map_str = map.to_string();
let map_str = map_str.as_slice();
assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
assert_eq!(format!("{}", empty), "{}".to_string());
assert_eq!(format!("{}", empty), String::from_str("{}"));
}

#[test]
Expand Down
33 changes: 31 additions & 2 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use core::prelude::*;

use core::default::Default;
use core::fmt;
use core::fmt::FormatWriter;
use core::mem;
use core::ptr;
// FIXME: ICE's abound if you import the `Slice` type while importing `Slice` trait
Expand Down Expand Up @@ -726,6 +727,28 @@ impl String {
pub unsafe fn as_mut_vec<'a>(&'a mut self) -> &'a mut Vec<u8> {
&mut self.vec
}

/// The format function takes a precompiled format string and a list of
/// arguments, to return the resulting formatted string.
///
/// # Arguments
///
/// * args - a structure of arguments generated via the `format_args!` macro.
/// Because this structure can only be safely generated at
/// compile-time, this function is safe.
///
/// # Example
///
/// ```rust
/// let s = format_args!(String::format, "Hello, {}!", "world");
/// assert_eq!(s, "Hello, world!".to_string());
/// ```
pub fn format(args: &fmt::Arguments) -> String {
let mut output = vec!();
let _ = write!(&mut output, "{}", args);
String::from_utf8(output)
.ok().expect("non-utf8 result from write!()")
}
}

impl Collection for String {
Expand Down Expand Up @@ -867,7 +890,7 @@ pub mod raw {

#[cfg(test)]
mod tests {
use std::prelude::*;
use core::prelude::*;
use test::Bencher;

use {Mutable, MutableSeq};
Expand All @@ -878,7 +901,7 @@ mod tests {

#[test]
fn test_from_str() {
let owned: Option<::std::string::String> = from_str("string");
let owned: Option<::string::String> = Some(String::from_str("string"));
assert_eq!(owned.as_ref().map(|s| s.as_slice()), Some("string"));
}

Expand Down Expand Up @@ -1142,6 +1165,12 @@ mod tests {
assert_eq!(b.as_slice(), "1234522");
}

#[test]
fn test_format() {
let s = format_args!(String::format, "Hello, {}!", "world");
assert_eq!(s.as_slice(), "Hello, world!");
}

#[bench]
fn bench_with_capacity(b: &mut Bencher) {
b.iter(|| {
Expand Down
20 changes: 12 additions & 8 deletions src/libcollections/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ use core::iter::Peekable;
use core::iter;
use core::mem::{replace, swap};
use core::ptr;
use std::hash::{Writer, Hash};

use {Mutable, Set, MutableSet, MutableMap, Map, MutableSeq};
use vec::Vec;
use hash::{Writer, Hash};

/// This is implemented as an AA tree, which is a simplified variation of
/// a red-black tree where red (horizontal) nodes can only be added
Expand Down Expand Up @@ -1669,12 +1669,15 @@ impl<S: Writer, T: Ord + Hash<S>> Hash<S> for TreeSet<T> {

#[cfg(test)]
mod test_treemap {
use std::prelude::*;
use core::prelude::*;
use alloc::boxed::Box;
use std::rand::Rng;
use std::rand;

use {Map, MutableMap, Mutable, MutableSeq};
use super::{TreeMap, TreeNode};
use vec::Vec;
use string::String;

#[test]
fn find_empty() {
Expand Down Expand Up @@ -2099,8 +2102,8 @@ mod test_treemap {

let map_str = format!("{}", map);

assert!(map_str == "{1: 2, 3: 4}".to_string());
assert_eq!(format!("{}", empty), "{}".to_string());
assert!(map_str == String::from_str("{1: 2, 3: 4}"));
assert_eq!(format!("{}", empty), String::from_str("{}"));
}

#[test]
Expand Down Expand Up @@ -2247,11 +2250,12 @@ mod bench {

#[cfg(test)]
mod test_set {
use std::prelude::*;
use std::hash;
use core::prelude::*;

use {Set, MutableSet, Mutable, MutableMap, MutableSeq};
use super::{TreeMap, TreeSet};
use hash;
use string::String;

#[test]
fn test_clear() {
Expand Down Expand Up @@ -2550,7 +2554,7 @@ mod test_set {

let set_str = format!("{}", set);

assert!(set_str == "{1, 2}".to_string());
assert_eq!(format!("{}", empty), "{}".to_string());
assert!(set_str == String::from_str("{1, 2}"));
assert_eq!(format!("{}", empty), String::from_str("{}"));
}
}
Loading