@@ -351,7 +351,35 @@ pub trait PhantomFn<A:?Sized,R:?Sized=()> { }
351
351
/// instance, it will behave *as if* an instance of the type `T` were
352
352
/// present for the purpose of various automatic analyses.
353
353
///
354
- /// For example, embedding a `PhantomData<T>` will inform the compiler
354
+ /// # Example
355
+ /// When handling external resources over a foreign function interface,
356
+ /// PhantomData can prevent mismatches by enforcing types in
357
+ /// the method implementations, although the struct doesn't actually
358
+ /// contain values of the resource type.
359
+ ///
360
+ ///```
361
+ ///pub struct ExternalResource<R> {
362
+ /// resource_handle: *mut (),
363
+ /// resource_type: PhantomData<R>,
364
+ ///}
365
+ ///
366
+ ///impl<R: ResType> ExternalResource<R> {
367
+ /// pub fn new() -> ExternalResource<R> {
368
+ /// let size_of_res = mem::size_of::<R>();
369
+ /// ExternalResource {
370
+ /// resource_handle: foreign_lib::new(size_of_res),
371
+ /// resource_type: PhantomData,
372
+ /// }
373
+ /// }
374
+ ///
375
+ /// pub fn do_stuff(&self, param: ParamType) {
376
+ /// let foreign_params = convert_params::<R>(param);
377
+ /// foreign_lib::do_stuff(self.resource_handle, foreign_params);
378
+ /// }
379
+ ///}
380
+ ///```
381
+ ///
382
+ /// Another example: embedding a `PhantomData<T>` will inform the compiler
355
383
/// that one or more instances of the type `T` could be dropped when
356
384
/// instances of the type itself is dropped, though that may not be
357
385
/// apparent from the other structure of the type itself. This is
0 commit comments