diff options
| author | bors <bors@rust-lang.org> | 2015-03-08 21:51:59 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-03-08 21:51:59 +0000 |
| commit | d71239f628f0338d042a7f2cd6f5909025cb0509 (patch) | |
| tree | 14f614e6b52dc7ad80824fa1ec691b007b0bd439 /src/libcore | |
| parent | b775541aafb5dcb12f33c6ad4e275d79e54b6987 (diff) | |
| parent | 45c397d738ac9cbf3d0ea2233142023ca148c893 (diff) | |
| download | rust-d71239f628f0338d042a7f2cd6f5909025cb0509.tar.gz rust-d71239f628f0338d042a7f2cd6f5909025cb0509.zip | |
Auto merge of #23179 - steveklabnik:mini_rollup, r=steveklabnik
I had to fix up some PRs: * https://github.com/rust-lang/rust/pull/22976 * https://github.com/rust-lang/rust/pull/22945 * https://github.com/rust-lang/rust/pull/22845
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/iter.rs | 2 | ||||
| -rw-r--r-- | src/libcore/marker.rs | 40 |
2 files changed, 40 insertions, 2 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 32225b90f6b..d5e891a156e 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -611,7 +611,7 @@ pub trait IteratorExt: Iterator + Sized { /// /// ``` /// let a = [1, 2, 3, 4, 5]; - /// assert!(a.iter().fold(0, |a, &b| a + b) == 15); + /// assert!(a.iter().fold(0, |acc, &item| acc + item) == 15); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index 868a671b956..a05bc04a530 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -351,7 +351,45 @@ pub trait PhantomFn<A:?Sized,R:?Sized=()> { } /// instance, it will behave *as if* an instance of the type `T` were /// present for the purpose of various automatic analyses. /// -/// For example, embedding a `PhantomData<T>` will inform the compiler +/// # Examples +/// +/// When handling external resources over a foreign function interface, `PhantomData<T>` can +/// prevent mismatches by enforcing types in the method implementations, although the struct +/// doesn't actually contain values of the resource type. +/// +/// ``` +/// # trait ResType { fn foo(&self); }; +/// # struct ParamType; +/// # mod foreign_lib { +/// # pub fn new(_: usize) -> *mut () { 42 as *mut () } +/// # pub fn do_stuff(_: *mut (), _: usize) {} +/// # } +/// # fn convert_params(_: ParamType) -> usize { 42 } +/// use std::marker::PhantomData; +/// use std::mem; +/// +/// struct ExternalResource<R> { +/// resource_handle: *mut (), +/// resource_type: PhantomData<R>, +/// } +/// +/// impl<R: ResType> ExternalResource<R> { +/// fn new() -> ExternalResource<R> { +/// let size_of_res = mem::size_of::<R>(); +/// ExternalResource { +/// resource_handle: foreign_lib::new(size_of_res), +/// resource_type: PhantomData, +/// } +/// } +/// +/// fn do_stuff(&self, param: ParamType) { +/// let foreign_params = convert_params(param); +/// foreign_lib::do_stuff(self.resource_handle, foreign_params); +/// } +/// } +/// ``` +/// +/// Another example: embedding a `PhantomData<T>` will inform the compiler /// that one or more instances of the type `T` could be dropped when /// instances of the type itself is dropped, though that may not be /// apparent from the other structure of the type itself. This is |
