Skip to content

Commit 8900290

Browse files
michaelsproulbrson
authored andcommitted
Add an impl for Box<Error> from String.
Closes #30156.
1 parent 2f7d6a5 commit 8900290

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/libstd/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ impl From<String> for Box<Error + Send + Sync> {
119119
}
120120
}
121121

122+
#[stable(feature = "string_box_error", since = "1.7.0")]
123+
impl From<String> for Box<Error> {
124+
fn from(str_err: String) -> Box<Error> {
125+
let err1: Box<Error + Send + Sync> = From::from(str_err);
126+
let err2: Box<Error> = err1;
127+
err2
128+
}
129+
}
130+
122131
#[stable(feature = "rust1", since = "1.0.0")]
123132
impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a> {
124133
fn from(err: &'b str) -> Box<Error + Send + Sync + 'a> {

src/test/run-pass/string-box-error.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Ensure that both `Box<Error + Send + Sync>` and `Box<Error>` can be obtained from `String`.
12+
13+
use std::error::Error;
14+
15+
fn main() {
16+
let _err1: Box<Error + Send + Sync> = From::from("test".to_string());
17+
let _err2: Box<Error> = From::from("test".to_string());
18+
}

0 commit comments

Comments
 (0)