Skip to content

Commit 31e9ed5

Browse files
authored
Auto merge of #34778 - GuillaumeGomez:rollup, r=GuillaumeGomez
Rollup of 7 pull requests - Successful merges: #34736, #34737, #34740, #34742, #34749, #34750, #34770 - Failed merges: #33951
2 parents 5c69a4f + 23d5f56 commit 31e9ed5

File tree

7 files changed

+357
-19
lines changed

7 files changed

+357
-19
lines changed

src/doc/book/closures.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ could annotate it on the function declaration:
336336

337337
```rust,ignore
338338
fn call_with_ref<'a, F>(some_closure:F) -> i32
339-
where F: Fn(&'a 32) -> i32 {
339+
where F: Fn(&'a i32) -> i32 {
340340
```
341341

342342
However this presents a problem with in our case. When you specify the explicit
@@ -350,7 +350,7 @@ of the closure we can use Higher-Ranked Trait Bounds with the `for<...>` syntax:
350350

351351
```ignore
352352
fn call_with_ref<F>(some_closure:F) -> i32
353-
where F: for<'a> Fn(&'a 32) -> i32 {
353+
where F: for<'a> Fn(&'a i32) -> i32 {
354354
```
355355

356356
This lets the Rust compiler find the minimum lifetime to invoke our closure and

src/doc/reference.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ comma:
26332633

26342634
There are several forms of struct expressions. A _struct expression_
26352635
consists of the [path](#paths) of a [struct item](#structs), followed by
2636-
a brace-enclosed list of one or more comma-separated name-value pairs,
2636+
a brace-enclosed list of zero or more comma-separated name-value pairs,
26372637
providing the field values of a new instance of the struct. A field name
26382638
can be any identifier, and is separated from its value expression by a colon.
26392639
The location denoted by a struct field is mutable if and only if the
@@ -2652,10 +2652,12 @@ The following are examples of struct expressions:
26522652

26532653
```
26542654
# struct Point { x: f64, y: f64 }
2655+
# struct NothingInMe { }
26552656
# struct TuplePoint(f64, f64);
26562657
# mod game { pub struct User<'a> { pub name: &'a str, pub age: u32, pub score: usize } }
26572658
# struct Cookie; fn some_fn<T>(t: T) {}
26582659
Point {x: 10.0, y: 20.0};
2660+
NothingInMe {};
26592661
TuplePoint(10.0, 20.0);
26602662
let u = game::User {name: "Joe", age: 35, score: 100_000};
26612663
some_fn::<Cookie>(Cookie);

src/liballoc/boxed.rs

+46-3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ impl<T: ?Sized> Box<T> {
249249
/// This function is unsafe because improper use may lead to
250250
/// memory problems. For example, a double-free may occur if the
251251
/// function is called twice on the same raw pointer.
252+
///
253+
/// # Examples
254+
///
255+
/// ```
256+
/// let x = Box::new(5);
257+
/// let ptr = Box::into_raw(x);
258+
/// let x = unsafe { Box::from_raw(ptr) };
259+
/// ```
252260
#[stable(feature = "box_raw", since = "1.4.0")]
253261
#[inline]
254262
pub unsafe fn from_raw(raw: *mut T) -> Self {
@@ -266,9 +274,8 @@ impl<T: ?Sized> Box<T> {
266274
/// # Examples
267275
///
268276
/// ```
269-
/// let seventeen = Box::new(17);
270-
/// let raw = Box::into_raw(seventeen);
271-
/// let boxed_again = unsafe { Box::from_raw(raw) };
277+
/// let x = Box::new(5);
278+
/// let ptr = Box::into_raw(x);
272279
/// ```
273280
#[stable(feature = "box_raw", since = "1.4.0")]
274281
#[inline]
@@ -399,6 +406,24 @@ impl Box<Any> {
399406
#[inline]
400407
#[stable(feature = "rust1", since = "1.0.0")]
401408
/// Attempt to downcast the box to a concrete type.
409+
///
410+
/// # Examples
411+
///
412+
/// ```
413+
/// use std::any::Any;
414+
///
415+
/// fn print_if_string(value: Box<Any>) {
416+
/// if let Ok(string) = value.downcast::<String>() {
417+
/// println!("String ({}): {}", string.len(), string);
418+
/// }
419+
/// }
420+
///
421+
/// fn main() {
422+
/// let my_string = "Hello World".to_string();
423+
/// print_if_string(Box::new(my_string));
424+
/// print_if_string(Box::new(0i8));
425+
/// }
426+
/// ```
402427
pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> {
403428
if self.is::<T>() {
404429
unsafe {
@@ -419,6 +444,24 @@ impl Box<Any + Send> {
419444
#[inline]
420445
#[stable(feature = "rust1", since = "1.0.0")]
421446
/// Attempt to downcast the box to a concrete type.
447+
///
448+
/// # Examples
449+
///
450+
/// ```
451+
/// use std::any::Any;
452+
///
453+
/// fn print_if_string(value: Box<Any + Send>) {
454+
/// if let Ok(string) = value.downcast::<String>() {
455+
/// println!("String ({}): {}", string.len(), string);
456+
/// }
457+
/// }
458+
///
459+
/// fn main() {
460+
/// let my_string = "Hello World".to_string();
461+
/// print_if_string(Box::new(my_string));
462+
/// print_if_string(Box::new(0i8));
463+
/// }
464+
/// ```
422465
pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any + Send>> {
423466
<Box<Any>>::downcast(self).map_err(|s| unsafe {
424467
// reapply the Send marker

src/libcore/any.rs

+158-2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ use marker::{Reflect, Sized};
9292
#[stable(feature = "rust1", since = "1.0.0")]
9393
pub trait Any: Reflect + 'static {
9494
/// Gets the `TypeId` of `self`.
95+
///
96+
/// # Examples
97+
///
98+
/// ```
99+
/// #![feature(get_type_id)]
100+
///
101+
/// use std::any::{Any, TypeId};
102+
///
103+
/// fn is_string(s: &Any) -> bool {
104+
/// TypeId::of::<String>() == s.get_type_id()
105+
/// }
106+
///
107+
/// fn main() {
108+
/// assert_eq!(is_string(&0), false);
109+
/// assert_eq!(is_string(&"cookie monster".to_owned()), true);
110+
/// }
111+
/// ```
95112
#[unstable(feature = "get_type_id",
96113
reason = "this method will likely be replaced by an associated static",
97114
issue = "27745")]
@@ -125,7 +142,26 @@ impl fmt::Debug for Any + Send {
125142
}
126143

127144
impl Any {
128-
/// Returns true if the boxed type is the same as `T`
145+
/// Returns true if the boxed type is the same as `T`.
146+
///
147+
/// # Examples
148+
///
149+
/// ```
150+
/// use std::any::Any;
151+
///
152+
/// fn is_string(s: &Any) {
153+
/// if s.is::<String>() {
154+
/// println!("It's a string!");
155+
/// } else {
156+
/// println!("Not a string...");
157+
/// }
158+
/// }
159+
///
160+
/// fn main() {
161+
/// is_string(&0);
162+
/// is_string(&"cookie monster".to_owned());
163+
/// }
164+
/// ```
129165
#[stable(feature = "rust1", since = "1.0.0")]
130166
#[inline]
131167
pub fn is<T: Any>(&self) -> bool {
@@ -141,6 +177,25 @@ impl Any {
141177

142178
/// Returns some reference to the boxed value if it is of type `T`, or
143179
/// `None` if it isn't.
180+
///
181+
/// # Examples
182+
///
183+
/// ```
184+
/// use std::any::Any;
185+
///
186+
/// fn print_if_string(s: &Any) {
187+
/// if let Some(string) = s.downcast_ref::<String>() {
188+
/// println!("It's a string({}): '{}'", string.len(), string);
189+
/// } else {
190+
/// println!("Not a string...");
191+
/// }
192+
/// }
193+
///
194+
/// fn main() {
195+
/// print_if_string(&0);
196+
/// print_if_string(&"cookie monster".to_owned());
197+
/// }
198+
/// ```
144199
#[stable(feature = "rust1", since = "1.0.0")]
145200
#[inline]
146201
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
@@ -159,6 +214,29 @@ impl Any {
159214

160215
/// Returns some mutable reference to the boxed value if it is of type `T`, or
161216
/// `None` if it isn't.
217+
///
218+
/// # Examples
219+
///
220+
/// ```
221+
/// use std::any::Any;
222+
///
223+
/// fn modify_if_u32(s: &mut Any) {
224+
/// if let Some(num) = s.downcast_mut::<u32>() {
225+
/// *num = 42;
226+
/// }
227+
/// }
228+
///
229+
/// fn main() {
230+
/// let mut x = 10u32;
231+
/// let mut s = "starlord".to_owned();
232+
///
233+
/// modify_if_u32(&mut x);
234+
/// modify_if_u32(&mut s);
235+
///
236+
/// assert_eq!(x, 42);
237+
/// assert_eq!(&s, "starlord");
238+
/// }
239+
/// ```
162240
#[stable(feature = "rust1", since = "1.0.0")]
163241
#[inline]
164242
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
@@ -178,20 +256,81 @@ impl Any {
178256

179257
impl Any+Send {
180258
/// Forwards to the method defined on the type `Any`.
259+
///
260+
/// # Examples
261+
///
262+
/// ```
263+
/// use std::any::Any;
264+
///
265+
/// fn is_string(s: &(Any + Send)) {
266+
/// if s.is::<String>() {
267+
/// println!("It's a string!");
268+
/// } else {
269+
/// println!("Not a string...");
270+
/// }
271+
/// }
272+
///
273+
/// fn main() {
274+
/// is_string(&0);
275+
/// is_string(&"cookie monster".to_owned());
276+
/// }
277+
/// ```
181278
#[stable(feature = "rust1", since = "1.0.0")]
182279
#[inline]
183280
pub fn is<T: Any>(&self) -> bool {
184281
Any::is::<T>(self)
185282
}
186283

187284
/// Forwards to the method defined on the type `Any`.
285+
///
286+
/// # Examples
287+
///
288+
/// ```
289+
/// use std::any::Any;
290+
///
291+
/// fn print_if_string(s: &(Any + Send)) {
292+
/// if let Some(string) = s.downcast_ref::<String>() {
293+
/// println!("It's a string({}): '{}'", string.len(), string);
294+
/// } else {
295+
/// println!("Not a string...");
296+
/// }
297+
/// }
298+
///
299+
/// fn main() {
300+
/// print_if_string(&0);
301+
/// print_if_string(&"cookie monster".to_owned());
302+
/// }
303+
/// ```
188304
#[stable(feature = "rust1", since = "1.0.0")]
189305
#[inline]
190306
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
191307
Any::downcast_ref::<T>(self)
192308
}
193309

194310
/// Forwards to the method defined on the type `Any`.
311+
///
312+
/// # Examples
313+
///
314+
/// ```
315+
/// use std::any::Any;
316+
///
317+
/// fn modify_if_u32(s: &mut (Any+ Send)) {
318+
/// if let Some(num) = s.downcast_mut::<u32>() {
319+
/// *num = 42;
320+
/// }
321+
/// }
322+
///
323+
/// fn main() {
324+
/// let mut x = 10u32;
325+
/// let mut s = "starlord".to_owned();
326+
///
327+
/// modify_if_u32(&mut x);
328+
/// modify_if_u32(&mut s);
329+
///
330+
/// assert_eq!(x, 42);
331+
/// assert_eq!(&s, "starlord");
332+
/// }
333+
/// ```
195334
#[stable(feature = "rust1", since = "1.0.0")]
196335
#[inline]
197336
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
@@ -220,7 +359,24 @@ pub struct TypeId {
220359

221360
impl TypeId {
222361
/// Returns the `TypeId` of the type this generic function has been
223-
/// instantiated with
362+
/// instantiated with.
363+
///
364+
/// # Examples
365+
///
366+
/// ```
367+
/// #![feature(get_type_id)]
368+
///
369+
/// use std::any::{Any, TypeId};
370+
///
371+
/// fn is_string(s: &Any) -> bool {
372+
/// TypeId::of::<String>() == s.get_type_id()
373+
/// }
374+
///
375+
/// fn main() {
376+
/// assert_eq!(is_string(&0), false);
377+
/// assert_eq!(is_string(&"cookie monster".to_owned()), true);
378+
/// }
379+
/// ```
224380
#[stable(feature = "rust1", since = "1.0.0")]
225381
pub fn of<T: ?Sized + Reflect + 'static>() -> TypeId {
226382
TypeId {

0 commit comments

Comments
 (0)