@@ -361,11 +361,17 @@ pub trait From<T>: Sized {
361
361
/// An attempted conversion that consumes `self`, which may or may not be
362
362
/// expensive.
363
363
///
364
- /// Library authors should not directly implement this trait, but should prefer
365
- /// implementing the [`TryFrom`] trait, which offers greater flexibility and
366
- /// provides an equivalent `TryInto` implementation for free, thanks to a
367
- /// blanket implementation in the standard library. For more information on this,
368
- /// see the documentation for [`Into`].
364
+ /// Library authors should usually not directly implement this trait,
365
+ /// but should prefer implementing the [`TryFrom`] trait, which offers
366
+ /// greater flexibility and provides an equivalent `TryInto`
367
+ /// implementation for free, thanks to a blanket implementation in the
368
+ /// standard library. For more information on this, see the
369
+ /// documentation for [`Into`].
370
+ ///
371
+ /// # Implementing `TryInto`
372
+ ///
373
+ /// This suffers the same restrictions and reasoning as implementing
374
+ /// [`Into`], see there for details.
369
375
///
370
376
/// [`TryFrom`]: trait.TryFrom.html
371
377
/// [`Into`]: trait.Into.html
@@ -380,7 +386,56 @@ pub trait TryInto<T>: Sized {
380
386
fn try_into ( self ) -> Result < T , Self :: Error > ;
381
387
}
382
388
383
- /// Attempt to construct `Self` via a conversion.
389
+ /// Simple and safe type conversions that may fail in a controlled
390
+ /// way under some circumstances. It is the reciprocal of [`TryInto`].
391
+ ///
392
+ /// This is useful when you are doing a type conversion that may
393
+ /// trivially succeed but may also need special handling.
394
+ /// For example, there is no way to convert an `i64` into an `i32`
395
+ /// using the [`From`] trait, because an `i64` may contain a value
396
+ /// that an `i32` cannot represent and so the conversion would lose data.
397
+ /// This might be handled by truncating the `i64` to an `i32` (essentially
398
+ /// giving the `i64`'s value modulo `i32::MAX`) or by simply returning
399
+ /// `i32::MAX`, or by some other method. The `From` trait is intended
400
+ /// for perfect conversions, so the `TryFrom` trait informs the
401
+ /// programmer when a type conversion could go bad and lets them
402
+ /// decide how to handle it.
403
+ ///
404
+ /// # Generic Implementations
405
+ ///
406
+ /// - `TryFrom<T> for U` implies [`TryInto<U>`]` for T`
407
+ /// - [`try_from`] is reflexive, which means that `TryFrom<T> for T`
408
+ /// is implemented and cannot fail -- the associated `Error` type for
409
+ /// calling `T::try_from()` on a value of type `T` is `Infallible`.
410
+ /// When the `!` type is stablized `Infallible` and `!` will be
411
+ /// equivalent.
412
+ ///
413
+ /// # Examples
414
+ ///
415
+ /// As described, [`i32`] implements `TryFrom<i64>`:
416
+ ///
417
+ /// ```
418
+ /// #![feature(try_from)]
419
+ /// use std::convert::TryFrom;
420
+ ///
421
+ /// let big_number = 1_000_000_000_000i64;
422
+ /// // Silently truncates `big_number`, requires detecting
423
+ /// // and handling the truncation after the fact.
424
+ /// let smaller_number = big_number as i32;
425
+ /// assert_eq!(smaller_number, -727379968);
426
+ ///
427
+ /// // Returns an error because `big_number` is too big to
428
+ /// // fit in an `i32`.
429
+ /// let try_smaller_number = i32::try_from(big_number);
430
+ /// assert!(try_smaller_number.is_err());
431
+ ///
432
+ /// // Returns `Ok(3)`.
433
+ /// let try_successful_smaller_number = i32::try_from(3);
434
+ /// assert!(try_successful_smaller_number.is_ok());
435
+ /// ```
436
+ ///
437
+ /// [`try_from`]: trait.TryFrom.html#tymethod.try_from
438
+ /// [`TryInto`]: trait.TryInto.html
384
439
#[ stable( feature = "try_from" , since = "1.34.0" ) ]
385
440
pub trait TryFrom < T > : Sized {
386
441
/// The type returned in the event of a conversion error.
0 commit comments