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

Rollup of 8 pull requests #44186

Merged
merged 20 commits into from
Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ce322ee
rustc: Fix proc_macro expansions on trait methods
alexcrichton Aug 25, 2017
0c3c43c
Remove Splice struct return value from String::splice
mattico Aug 22, 2017
8be132e
Initial diagnostic API for proc-macros.
SergioBenitez Aug 28, 2017
e5b123c
Update the libc submodule
alexcrichton Jul 17, 2017
27c4ff6
rustc: Remove `specailization_cache` in favor of a query
alexcrichton Aug 29, 2017
b9b6549
API docs: macros. Part of #29329 Standard Library Documentation Chec…
AndyGauge Aug 29, 2017
84c5441
fix test failures in documentation change
AndyGauge Aug 29, 2017
32d35e6
rustc: Make the `trait_map` of TyCtxt private
alexcrichton Aug 29, 2017
80d513a
broken links resolved
AndyGauge Aug 29, 2017
6f7594d
Fix link in unstable book entry for Generators
mattico Aug 30, 2017
942c8dc
rustc: Make the `export_map` of TyCtxt private
alexcrichton Aug 29, 2017
581dc93
Rollup merge of #44044 - mattico:string-splice-return, r=dtolnay
alexcrichton Aug 30, 2017
63cc2dd
Rollup merge of #44089 - alexcrichton:trait-proc-macro, r=nrc
alexcrichton Aug 30, 2017
bc6981b
Rollup merge of #44116 - alexcrichton:update-libc, r=Mark-Simulacrum
alexcrichton Aug 30, 2017
fd4f362
Rollup merge of #44125 - SergioBenitez:master, r=nrc
alexcrichton Aug 30, 2017
8962393
Rollup merge of #44157 - alexcrichton:no-specializes-cache, r=eddyb
alexcrichton Aug 30, 2017
c0812c8
Rollup merge of #44160 - AndyGauge:api-docs-macros, r=steveklabnik
alexcrichton Aug 30, 2017
16bbff0
Rollup merge of #44172 - mattico:patch-2, r=frewsxcv
alexcrichton Aug 30, 2017
5c279a4
Merge branch 'hide-trait-map' into rollup
alexcrichton Aug 30, 2017
b9fea42
Update libc to fix sparc compiles
alexcrichton Aug 31, 2017
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
1 change: 1 addition & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/doc/unstable-book/src/language-features/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The tracking issue for this feature is: [#43122]

[#34511]: https://github.com/rust-lang/rust/issues/43122
[#43122]: https://github.com/rust-lang/rust/issues/43122

------------------------

Expand Down
3 changes: 1 addition & 2 deletions src/doc/unstable-book/src/library-features/splice.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ let mut s = String::from("α is alpha, β is beta");
let beta_offset = s.find('β').unwrap_or(s.len());

// Replace the range up until the β from the string
let t: String = s.splice(..beta_offset, "Α is capital alpha; ").collect();
assert_eq!(t, "α is alpha, ");
s.splice(..beta_offset, "Α is capital alpha; ");
assert_eq!(s, "Α is capital alpha; β is beta");
```
29 changes: 22 additions & 7 deletions src/liballoc/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

/// Creates a `Vec` containing the arguments.
/// Creates a [`Vec`] containing the arguments.
///
/// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
/// There are two forms of this macro:
///
/// - Create a `Vec` containing a given list of elements:
/// - Create a [`Vec`] containing a given list of elements:
///
/// ```
/// let v = vec![1, 2, 3];
Expand All @@ -22,22 +22,25 @@
/// assert_eq!(v[2], 3);
/// ```
///
/// - Create a `Vec` from a given element and size:
/// - Create a [`Vec`] from a given element and size:
///
/// ```
/// let v = vec![1; 3];
/// assert_eq!(v, [1, 1, 1]);
/// ```
///
/// Note that unlike array expressions this syntax supports all elements
/// which implement `Clone` and the number of elements doesn't have to be
/// which implement [`Clone`] and the number of elements doesn't have to be
/// a constant.
///
/// This will use `clone()` to duplicate an expression, so one should be careful
/// This will use `clone` to duplicate an expression, so one should be careful
/// using this with types having a nonstandard `Clone` implementation. For
/// example, `vec![Rc::new(1); 5]` will create a vector of five references
/// to the same boxed integer value, not five references pointing to independently
/// boxed integers.
///
/// [`Vec`]: ../std/vec/struct.Vec.html
/// [`Clone`]: ../std/clone/trait.Clone.html
#[cfg(not(test))]
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -67,10 +70,22 @@ macro_rules! vec {
($($x:expr,)*) => (vec![$($x),*])
}

/// Use the syntax described in `std::fmt` to create a value of type `String`.
/// See [`std::fmt`][fmt] for more information.
/// Creates a `String` using interpolation of runtime expressions.
///
/// The first argument `format!` recieves is a format string. This must be a string
/// literal. The power of the formatting string is in the `{}`s contained.
///
/// Additional parameters passed to `format!` replace the `{}`s within the
/// formatting string in the order given unless named or positional parameters
/// are used, see [`std::fmt`][fmt] for more information.
///
/// A common use for `format!` is concatenation and interpolation of strings.
/// The same convention is used with [`print!`] and [`write!`] macros,
/// depending on the intended destination of the string.
///
/// [fmt]: ../std/fmt/index.html
/// [`print!`]: ../std/macro.print.html
/// [`write!`]: ../std/macro.write.html
///
/// # Panics
///
Expand Down
109 changes: 19 additions & 90 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,19 +1392,19 @@ impl String {
}

/// Creates a splicing iterator that removes the specified range in the string,
/// replaces with the given string, and yields the removed chars.
/// The given string doesnt need to be the same length as the range.
/// and replaces it with the given string.
/// The given string doesn't need to be the same length as the range.
///
/// Note: The element range is removed when the [`Splice`] is dropped,
/// even if the iterator is not consumed until the end.
/// Note: Unlike [`Vec::splice`], the replacement happens eagerly, and this
/// method does not return the removed chars.
///
/// # Panics
///
/// Panics if the starting point or end point do not lie on a [`char`]
/// boundary, or if they're out of bounds.
///
/// [`char`]: ../../std/primitive.char.html
/// [`Splice`]: ../../std/string/struct.Splice.html
/// [`Vec::splice`]: ../../std/vec/struct.Vec.html#method.splice
///
/// # Examples
///
Expand All @@ -1416,45 +1416,32 @@ impl String {
/// let beta_offset = s.find('β').unwrap_or(s.len());
///
/// // Replace the range up until the β from the string
/// let t: String = s.splice(..beta_offset, "Α is capital alpha; ").collect();
/// assert_eq!(t, "α is alpha, ");
/// s.splice(..beta_offset, "Α is capital alpha; ");
/// assert_eq!(s, "Α is capital alpha; β is beta");
/// ```
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
pub fn splice<'a, 'b, R>(&'a mut self, range: R, replace_with: &'b str) -> Splice<'a, 'b>
pub fn splice<R>(&mut self, range: R, replace_with: &str)
where R: RangeArgument<usize>
{
// Memory safety
//
// The String version of Splice does not have the memory safety issues
// of the vector version. The data is just plain bytes.
// Because the range removal happens in Drop, if the Splice iterator is leaked,
// the removal will not happen.
let len = self.len();
let start = match range.start() {
Included(&n) => n,
Excluded(&n) => n + 1,
Unbounded => 0,

match range.start() {
Included(&n) => assert!(self.is_char_boundary(n)),
Excluded(&n) => assert!(self.is_char_boundary(n + 1)),
Unbounded => {},
};
let end = match range.end() {
Included(&n) => n + 1,
Excluded(&n) => n,
Unbounded => len,
match range.end() {
Included(&n) => assert!(self.is_char_boundary(n + 1)),
Excluded(&n) => assert!(self.is_char_boundary(n)),
Unbounded => {},
};

// Take out two simultaneous borrows. The &mut String won't be accessed
// until iteration is over, in Drop.
let self_ptr = self as *mut _;
// slicing does the appropriate bounds checks
let chars_iter = self[start..end].chars();

Splice {
start,
end,
iter: chars_iter,
string: self_ptr,
replace_with,
}
unsafe {
self.as_mut_vec()
}.splice(range, replace_with.bytes());
}

/// Converts this `String` into a [`Box`]`<`[`str`]`>`.
Expand Down Expand Up @@ -2241,61 +2228,3 @@ impl<'a> DoubleEndedIterator for Drain<'a> {

#[unstable(feature = "fused", issue = "35602")]
impl<'a> FusedIterator for Drain<'a> {}

/// A splicing iterator for `String`.
///
/// This struct is created by the [`splice()`] method on [`String`]. See its
/// documentation for more.
///
/// [`splice()`]: struct.String.html#method.splice
/// [`String`]: struct.String.html
#[derive(Debug)]
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
pub struct Splice<'a, 'b> {
/// Will be used as &'a mut String in the destructor
string: *mut String,
/// Start of part to remove
start: usize,
/// End of part to remove
end: usize,
/// Current remaining range to remove
iter: Chars<'a>,
replace_with: &'b str,
}

#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
unsafe impl<'a, 'b> Sync for Splice<'a, 'b> {}
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
unsafe impl<'a, 'b> Send for Splice<'a, 'b> {}

#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
impl<'a, 'b> Drop for Splice<'a, 'b> {
fn drop(&mut self) {
unsafe {
let vec = (*self.string).as_mut_vec();
vec.splice(self.start..self.end, self.replace_with.bytes());
}
}
}

#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
impl<'a, 'b> Iterator for Splice<'a, 'b> {
type Item = char;

#[inline]
fn next(&mut self) -> Option<char> {
self.iter.next()
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}

#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
impl<'a, 'b> DoubleEndedIterator for Splice<'a, 'b> {
#[inline]
fn next_back(&mut self) -> Option<char> {
self.iter.next_back()
}
}
22 changes: 5 additions & 17 deletions src/liballoc/tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,8 @@ fn test_drain() {
#[test]
fn test_splice() {
let mut s = "Hello, world!".to_owned();
let t: String = s.splice(7..12, "世界").collect();
s.splice(7..12, "世界");
assert_eq!(s, "Hello, 世界!");
assert_eq!(t, "world");
}

#[test]
Expand All @@ -457,12 +456,10 @@ fn test_splice_char_boundary() {
#[test]
fn test_splice_inclusive_range() {
let mut v = String::from("12345");
let t: String = v.splice(2...3, "789").collect();
v.splice(2...3, "789");
assert_eq!(v, "127895");
assert_eq!(t, "34");
let t2: String = v.splice(1...2, "A").collect();
v.splice(1...2, "A");
assert_eq!(v, "1A895");
assert_eq!(t2, "27");
}

#[test]
Expand All @@ -482,24 +479,15 @@ fn test_splice_inclusive_out_of_bounds() {
#[test]
fn test_splice_empty() {
let mut s = String::from("12345");
let t: String = s.splice(1..2, "").collect();
s.splice(1..2, "");
assert_eq!(s, "1345");
assert_eq!(t, "2");
}

#[test]
fn test_splice_unbounded() {
let mut s = String::from("12345");
let t: String = s.splice(.., "").collect();
s.splice(.., "");
assert_eq!(s, "");
assert_eq!(t, "12345");
}

#[test]
fn test_splice_forget() {
let mut s = String::from("12345");
::std::mem::forget(s.splice(2..4, "789"));
assert_eq!(s, "12345");
}

#[test]
Expand Down
Loading