From 6dcc0e563118ef2d6d07b5d9d6d0f1042b497db0 Mon Sep 17 00:00:00 2001 From: Pyry Kontio Date: Mon, 2 Mar 2015 06:14:45 +0200 Subject: [PATCH 1/3] Adds an example for PhantomData. --- src/libcore/marker.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) 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 { } /// 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` will inform the compiler +/// # Examples +/// +/// When handling external resources over a foreign function interface, `PhantomData` 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 { +/// resource_handle: *mut (), +/// resource_type: PhantomData, +/// } +/// +/// impl ExternalResource { +/// fn new() -> ExternalResource { +/// let size_of_res = mem::size_of::(); +/// 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` 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 From 3ad1c8354051174921c04c6b376fc205790f90b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Niemier?= Date: Mon, 2 Mar 2015 22:31:01 +0100 Subject: [PATCH 2/3] Add description of fold function arguments. --- src/libcore/iter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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")] From 45c397d738ac9cbf3d0ea2233142023ca148c893 Mon Sep 17 00:00:00 2001 From: Amol Mundayoor Date: Mon, 23 Feb 2015 13:06:20 -0800 Subject: [PATCH 3/3] Fix array syntax in comment. Fixes #22721. --- src/libsyntax/ast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index b41eb05ded6..550ce3bb8c8 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1264,7 +1264,7 @@ pub struct BareFnTy { /// The different kinds of types recognized by the compiler pub enum Ty_ { TyVec(P), - /// A fixed length array (`[T, ..n]`) + /// A fixed length array (`[T; n]`) TyFixedLengthVec(P, P), /// A raw pointer (`*const T` or `*mut T`) TyPtr(MutTy),