|
48 | 48 |
|
49 | 49 | #![stable(feature = "rust1", since = "1.0.0")]
|
50 | 50 |
|
| 51 | +use str::FromStr; |
| 52 | + |
51 | 53 | /// A cheap, reference-to-reference conversion.
|
52 | 54 | ///
|
53 | 55 | /// `AsRef` is very similar to, but different than, [`Borrow`]. See
|
@@ -212,20 +214,20 @@ pub trait From<T>: Sized {
|
212 | 214 | #[unstable(feature = "try_from", issue = "33417")]
|
213 | 215 | pub trait TryInto<T>: Sized {
|
214 | 216 | /// The type returned in the event of a conversion error.
|
215 |
| - type Err; |
| 217 | + type Error; |
216 | 218 |
|
217 | 219 | /// Performs the conversion.
|
218 |
| - fn try_into(self) -> Result<T, Self::Err>; |
| 220 | + fn try_into(self) -> Result<T, Self::Error>; |
219 | 221 | }
|
220 | 222 |
|
221 | 223 | /// Attempt to construct `Self` via a conversion.
|
222 | 224 | #[unstable(feature = "try_from", issue = "33417")]
|
223 | 225 | pub trait TryFrom<T>: Sized {
|
224 | 226 | /// The type returned in the event of a conversion error.
|
225 |
| - type Err; |
| 227 | + type Error; |
226 | 228 |
|
227 | 229 | /// Performs the conversion.
|
228 |
| - fn try_from(value: T) -> Result<Self, Self::Err>; |
| 230 | + fn try_from(value: T) -> Result<Self, Self::Error>; |
229 | 231 | }
|
230 | 232 |
|
231 | 233 | ////////////////////////////////////////////////////////////////////////////////
|
@@ -290,9 +292,9 @@ impl<T> From<T> for T {
|
290 | 292 | // TryFrom implies TryInto
|
291 | 293 | #[unstable(feature = "try_from", issue = "33417")]
|
292 | 294 | impl<T, U> TryInto<U> for T where U: TryFrom<T> {
|
293 |
| - type Err = U::Err; |
| 295 | + type Error = U::Error; |
294 | 296 |
|
295 |
| - fn try_into(self) -> Result<U, U::Err> { |
| 297 | + fn try_into(self) -> Result<U, U::Error> { |
296 | 298 | U::try_from(self)
|
297 | 299 | }
|
298 | 300 | }
|
@@ -322,3 +324,13 @@ impl AsRef<str> for str {
|
322 | 324 | self
|
323 | 325 | }
|
324 | 326 | }
|
| 327 | + |
| 328 | +// FromStr implies TryFrom<&str> |
| 329 | +#[unstable(feature = "try_from", issue = "33417")] |
| 330 | +impl<'a, T> TryFrom<&'a str> for T where T: FromStr { |
| 331 | + type Error = <T as FromStr>::Err; |
| 332 | + |
| 333 | + fn try_from(s: &'a str) -> Result<T, Self::Error> { |
| 334 | + FromStr::from_str(s) |
| 335 | + } |
| 336 | +} |
0 commit comments