Skip to content

Commit 3cc3ad1

Browse files
authored
Auto merge of #34819 - GuillaumeGomez:rollup, r=GuillaumeGomez
Rollup of 7 pull requests - Successful merges: #34456, #34733, #34777, #34794, #34799, #34804, #34818 - Failed merges: #33951
2 parents b6c1ef3 + 8959374 commit 3cc3ad1

File tree

19 files changed

+200
-80
lines changed

19 files changed

+200
-80
lines changed

src/doc/book/ffi.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,62 @@ pub fn uncompress(src: &[u8]) -> Option<Vec<u8>> {
183183
}
184184
```
185185

186-
For reference, the examples used here are also available as a [library on
187-
GitHub](https://github.com/thestinger/rust-snappy).
186+
Then, we can add some tests to show how to use them.
187+
188+
```rust
189+
# #![feature(libc)]
190+
# extern crate libc;
191+
# use libc::{c_int, size_t};
192+
# unsafe fn snappy_compress(input: *const u8,
193+
# input_length: size_t,
194+
# compressed: *mut u8,
195+
# compressed_length: *mut size_t)
196+
# -> c_int { 0 }
197+
# unsafe fn snappy_uncompress(compressed: *const u8,
198+
# compressed_length: size_t,
199+
# uncompressed: *mut u8,
200+
# uncompressed_length: *mut size_t)
201+
# -> c_int { 0 }
202+
# unsafe fn snappy_max_compressed_length(source_length: size_t) -> size_t { 0 }
203+
# unsafe fn snappy_uncompressed_length(compressed: *const u8,
204+
# compressed_length: size_t,
205+
# result: *mut size_t)
206+
# -> c_int { 0 }
207+
# unsafe fn snappy_validate_compressed_buffer(compressed: *const u8,
208+
# compressed_length: size_t)
209+
# -> c_int { 0 }
210+
# fn main() { }
211+
212+
#[cfg(test)]
213+
mod tests {
214+
use super::*;
215+
216+
#[test]
217+
fn valid() {
218+
let d = vec![0xde, 0xad, 0xd0, 0x0d];
219+
let c: &[u8] = &compress(&d);
220+
assert!(validate_compressed_buffer(c));
221+
assert!(uncompress(c) == Some(d));
222+
}
223+
224+
#[test]
225+
fn invalid() {
226+
let d = vec![0, 0, 0, 0];
227+
assert!(!validate_compressed_buffer(&d));
228+
assert!(uncompress(&d).is_none());
229+
}
230+
231+
#[test]
232+
fn empty() {
233+
let d = vec![];
234+
assert!(!validate_compressed_buffer(&d));
235+
assert!(uncompress(&d).is_none());
236+
let c = compress(&d);
237+
assert!(validate_compressed_buffer(&c));
238+
assert!(uncompress(&c) == Some(d));
239+
}
240+
}
241+
```
188242

189243
# Destructors
190244

src/liballoc/arc.rs

+46-43
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,11 @@
1212

1313
//! Threadsafe reference-counted boxes (the `Arc<T>` type).
1414
//!
15-
//! The `Arc<T>` type provides shared ownership of an immutable value.
16-
//! Destruction is deterministic, and will occur as soon as the last owner is
17-
//! gone. It is marked as `Send` because it uses atomic reference counting.
18-
//!
19-
//! If you do not need thread-safety, and just need shared ownership, consider
20-
//! the [`Rc<T>` type](../rc/struct.Rc.html). It is the same as `Arc<T>`, but
21-
//! does not use atomics, making it both thread-unsafe as well as significantly
22-
//! faster when updating the reference count.
23-
//!
24-
//! The `downgrade` method can be used to create a non-owning `Weak<T>` pointer
25-
//! to the box. A `Weak<T>` pointer can be upgraded to an `Arc<T>` pointer, but
26-
//! will return `None` if the value has already been dropped.
27-
//!
28-
//! For example, a tree with parent pointers can be represented by putting the
29-
//! nodes behind strong `Arc<T>` pointers, and then storing the parent pointers
30-
//! as `Weak<T>` pointers.
15+
//! The `Arc<T>` type provides shared ownership of an immutable value through
16+
//! atomic reference counting.
3117
//!
18+
//! `Weak<T>` is a weak reference to the `Arc<T>` box, and it is created by
19+
//! the `downgrade` method.
3220
//! # Examples
3321
//!
3422
//! Sharing some immutable data between threads:
@@ -47,27 +35,6 @@
4735
//! });
4836
//! }
4937
//! ```
50-
//!
51-
//! Sharing mutable data safely between threads with a `Mutex`:
52-
//!
53-
//! ```no_run
54-
//! use std::sync::{Arc, Mutex};
55-
//! use std::thread;
56-
//!
57-
//! let five = Arc::new(Mutex::new(5));
58-
//!
59-
//! for _ in 0..10 {
60-
//! let five = five.clone();
61-
//!
62-
//! thread::spawn(move || {
63-
//! let mut number = five.lock().unwrap();
64-
//!
65-
//! *number += 1;
66-
//!
67-
//! println!("{}", *number); // prints 6
68-
//! });
69-
//! }
70-
//! ```
7138
7239
use boxed::Box;
7340

@@ -92,15 +59,19 @@ use heap::deallocate;
9259
const MAX_REFCOUNT: usize = (isize::MAX) as usize;
9360

9461
/// An atomically reference counted wrapper for shared state.
62+
/// Destruction is deterministic, and will occur as soon as the last owner is
63+
/// gone. It is marked as `Send` because it uses atomic reference counting.
9564
///
96-
/// # Examples
65+
/// If you do not need thread-safety, and just need shared ownership, consider
66+
/// the [`Rc<T>` type](../rc/struct.Rc.html). It is the same as `Arc<T>`, but
67+
/// does not use atomics, making it both thread-unsafe as well as significantly
68+
/// faster when updating the reference count.
9769
///
98-
/// In this example, a large vector is shared between several threads.
99-
/// With simple pipes, without `Arc`, a copy would have to be made for each
100-
/// thread.
70+
/// # Examples
10171
///
102-
/// When you clone an `Arc<T>`, it will create another pointer to the data and
103-
/// increase the reference counter.
72+
/// In this example, a large vector of data will be shared by several threads. First we
73+
/// wrap it with a `Arc::new` and then clone the `Arc<T>` reference for every thread (which will
74+
/// increase the reference count atomically).
10475
///
10576
/// ```
10677
/// use std::sync::Arc;
@@ -111,6 +82,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
11182
/// let shared_numbers = Arc::new(numbers);
11283
///
11384
/// for _ in 0..10 {
85+
/// // prepare a copy of reference here and it will be moved to the thread
11486
/// let child_numbers = shared_numbers.clone();
11587
///
11688
/// thread::spawn(move || {
@@ -121,6 +93,29 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
12193
/// }
12294
/// }
12395
/// ```
96+
/// You can also share mutable data between threads safely
97+
/// by putting it inside `Mutex` and then share `Mutex` immutably
98+
/// with `Arc<T>` as shown below.
99+
///
100+
/// ```
101+
/// use std::sync::{Arc, Mutex};
102+
/// use std::thread;
103+
///
104+
/// let five = Arc::new(Mutex::new(5));
105+
///
106+
/// for _ in 0..10 {
107+
/// let five = five.clone();
108+
///
109+
/// thread::spawn(move || {
110+
/// let mut number = five.lock().unwrap();
111+
///
112+
/// *number += 1;
113+
///
114+
/// println!("{}", *number); // prints 6
115+
/// });
116+
/// }
117+
/// ```
118+
124119
#[unsafe_no_drop_flag]
125120
#[stable(feature = "rust1", since = "1.0.0")]
126121
pub struct Arc<T: ?Sized> {
@@ -139,6 +134,14 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T> {}
139134
///
140135
/// Weak pointers will not keep the data inside of the `Arc` alive, and can be
141136
/// used to break cycles between `Arc` pointers.
137+
///
138+
/// A `Weak<T>` pointer can be upgraded to an `Arc<T>` pointer, but
139+
/// will return `None` if the value has already been dropped.
140+
///
141+
/// For example, a tree with parent pointers can be represented by putting the
142+
/// nodes behind strong `Arc<T>` pointers, and then storing the parent pointers
143+
/// as `Weak<T>` pointers.
144+
142145
#[unsafe_no_drop_flag]
143146
#[stable(feature = "arc_weak", since = "1.4.0")]
144147
pub struct Weak<T: ?Sized> {

src/libcollections/vec.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,12 @@ impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Clone {
16031603
////////////////////////////////////////////////////////////////////////////////
16041604

16051605
/// An iterator that moves out of a vector.
1606+
///
1607+
/// This `struct` is created by the `into_iter` method on [`Vec`][`Vec`] (provided
1608+
/// by the [`IntoIterator`] trait).
1609+
///
1610+
/// [`Vec`]: struct.Vec.html
1611+
/// [`IntoIterator`]: ../../std/iter/trait.IntoIterator.html
16061612
#[stable(feature = "rust1", since = "1.0.0")]
16071613
pub struct IntoIter<T> {
16081614
_buf: RawVec<T>,
@@ -1710,6 +1716,11 @@ impl<T> Drop for IntoIter<T> {
17101716
}
17111717

17121718
/// A draining iterator for `Vec<T>`.
1719+
///
1720+
/// This `struct` is created by the [`drain`] method on [`Vec`].
1721+
///
1722+
/// [`drain`]: struct.Vec.html#method.drain
1723+
/// [`Vec`]: struct.Vec.html
17131724
#[stable(feature = "drain", since = "1.6.0")]
17141725
pub struct Drain<'a, T: 'a> {
17151726
/// Index of tail to preserve

src/libcore/macros.rs

+2
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ macro_rules! write {
238238
}
239239

240240
/// Use the `format!` syntax to write data into a buffer, appending a newline.
241+
/// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`)
242+
/// alone (no additional CARRIAGE RETURN (`\r`/`U+000D`).
241243
///
242244
/// This macro is typically used with a buffer of `&mut `[`Write`][write].
243245
///

src/libcore/num/mod.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -2308,26 +2308,45 @@ impl usize {
23082308
///
23092309
/// [`f32::classify()`]: ../../std/primitive.f32.html#method.classify
23102310
/// [`f64::classify()`]: ../../std/primitive.f64.html#method.classify
2311+
///
2312+
/// # Examples
2313+
///
2314+
/// ```
2315+
/// use std::num::FpCategory;
2316+
/// use std::f32;
2317+
///
2318+
/// let num = 12.4_f32;
2319+
/// let inf = f32::INFINITY;
2320+
/// let zero = 0f32;
2321+
/// let sub: f32 = 0.000000000000000000000000000000000000011754942;
2322+
/// let nan = f32::NAN;
2323+
///
2324+
/// assert_eq!(num.classify(), FpCategory::Normal);
2325+
/// assert_eq!(inf.classify(), FpCategory::Infinite);
2326+
/// assert_eq!(zero.classify(), FpCategory::Zero);
2327+
/// assert_eq!(nan.classify(), FpCategory::Nan);
2328+
/// assert_eq!(sub.classify(), FpCategory::Subnormal);
2329+
/// ```
23112330
#[derive(Copy, Clone, PartialEq, Debug)]
23122331
#[stable(feature = "rust1", since = "1.0.0")]
23132332
pub enum FpCategory {
2314-
/// "Not a Number", often obtained by dividing by zero
2333+
/// "Not a Number", often obtained by dividing by zero.
23152334
#[stable(feature = "rust1", since = "1.0.0")]
23162335
Nan,
23172336

2318-
/// Positive or negative infinity
2337+
/// Positive or negative infinity.
23192338
#[stable(feature = "rust1", since = "1.0.0")]
23202339
Infinite ,
23212340

2322-
/// Positive or negative zero
2341+
/// Positive or negative zero.
23232342
#[stable(feature = "rust1", since = "1.0.0")]
23242343
Zero,
23252344

2326-
/// De-normalized floating point representation (less precise than `Normal`)
2345+
/// De-normalized floating point representation (less precise than `Normal`).
23272346
#[stable(feature = "rust1", since = "1.0.0")]
23282347
Subnormal,
23292348

2330-
/// A regular floating point number
2349+
/// A regular floating point number.
23312350
#[stable(feature = "rust1", since = "1.0.0")]
23322351
Normal,
23332352
}

src/libpanic_unwind/gcc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#![allow(private_no_mangle_fns)]
5858

5959
use core::any::Any;
60+
use core::ptr;
6061
use alloc::boxed::Box;
6162

6263
use unwind as uw;
@@ -88,7 +89,7 @@ pub unsafe fn panic(data: Box<Any + Send>) -> u32 {
8889
}
8990

9091
pub fn payload() -> *mut u8 {
91-
0 as *mut u8
92+
ptr::null_mut()
9293
}
9394

9495
pub unsafe fn cleanup(ptr: *mut u8) -> Box<Any + Send> {

src/libpanic_unwind/seh64_gnu.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use alloc::boxed::Box;
1818

1919
use core::any::Any;
2020
use core::intrinsics;
21+
use core::ptr;
2122
use dwarf::eh;
2223
use windows as c;
2324

@@ -50,7 +51,7 @@ pub unsafe fn panic(data: Box<Any + Send>) -> u32 {
5051
}
5152

5253
pub fn payload() -> *mut u8 {
53-
0 as *mut u8
54+
ptr::null_mut()
5455
}
5556

5657
pub unsafe fn cleanup(ptr: *mut u8) -> Box<Any + Send> {

src/librustc_trans/base.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ use libc::c_uint;
9696
use std::ffi::{CStr, CString};
9797
use std::cell::{Cell, RefCell};
9898
use std::collections::{HashMap, HashSet};
99+
use std::ptr;
99100
use std::rc::Rc;
100101
use std::str;
101102
use std::{i8, i16, i32, i64};
@@ -2201,7 +2202,7 @@ pub fn maybe_create_entry_wrapper(ccx: &CrateContext) {
22012202
start_fn,
22022203
args.as_ptr(),
22032204
args.len() as c_uint,
2204-
0 as *mut _,
2205+
ptr::null_mut(),
22052206
noname());
22062207

22072208
llvm::LLVMBuildRet(bld, result);

src/librustc_trans/builder.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
177177

178178
check_call("invoke", llfn, args);
179179

180-
let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(0 as *mut _);
180+
let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(ptr::null_mut());
181181

182182
unsafe {
183183
llvm::LLVMRustBuildInvoke(self.llbuilder,
@@ -859,7 +859,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
859859

860860
check_call("call", llfn, args);
861861

862-
let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(0 as *mut _);
862+
let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(ptr::null_mut());
863863

864864
unsafe {
865865
llvm::LLVMRustBuildCall(self.llbuilder, llfn, args.as_ptr(),
@@ -961,7 +961,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
961961
self.count_insn("trap");
962962
llvm::LLVMRustBuildCall(self.llbuilder, t,
963963
args.as_ptr(), args.len() as c_uint,
964-
0 as *mut _,
964+
ptr::null_mut(),
965965
noname());
966966
}
967967
}
@@ -1000,7 +1000,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10001000
parent: Option<ValueRef>,
10011001
args: &[ValueRef]) -> ValueRef {
10021002
self.count_insn("cleanuppad");
1003-
let parent = parent.unwrap_or(0 as *mut _);
1003+
let parent = parent.unwrap_or(ptr::null_mut());
10041004
let name = CString::new("cleanuppad").unwrap();
10051005
let ret = unsafe {
10061006
llvm::LLVMRustBuildCleanupPad(self.llbuilder,
@@ -1016,7 +1016,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10161016
pub fn cleanup_ret(&self, cleanup: ValueRef,
10171017
unwind: Option<BasicBlockRef>) -> ValueRef {
10181018
self.count_insn("cleanupret");
1019-
let unwind = unwind.unwrap_or(0 as *mut _);
1019+
let unwind = unwind.unwrap_or(ptr::null_mut());
10201020
let ret = unsafe {
10211021
llvm::LLVMRustBuildCleanupRet(self.llbuilder, cleanup, unwind)
10221022
};
@@ -1052,8 +1052,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10521052
unwind: Option<BasicBlockRef>,
10531053
num_handlers: usize) -> ValueRef {
10541054
self.count_insn("catchswitch");
1055-
let parent = parent.unwrap_or(0 as *mut _);
1056-
let unwind = unwind.unwrap_or(0 as *mut _);
1055+
let parent = parent.unwrap_or(ptr::null_mut());
1056+
let unwind = unwind.unwrap_or(ptr::null_mut());
10571057
let name = CString::new("catchswitch").unwrap();
10581058
let ret = unsafe {
10591059
llvm::LLVMRustBuildCatchSwitch(self.llbuilder, parent, unwind,

0 commit comments

Comments
 (0)