# Pre RFC: Remove \`FromError\` trait, add \`From\` trait

**URL:** <https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783>\
**Category:** Uncategorized\
**Created:** [November 10, 2014, 9:36am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783 "2014-11-10T09:36:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TyOverby](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tyoverby/32/363_2.png) [@TyOverby](https://internals.rust-lang.org/u/TyOverby)\
**Post date:** [November 10, 2014, 9:36am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/1 "2014-11-10T09:36:31Z")

</div>

The ability to define how external types can be transformed into internal types is very powerful. With a few impls, the readability of error handling has been improved drastically. However, there are other areas that would benefit from this flexibility (event handling comes to mind). My hope is to remove the `FromError` trait and add a `From` trait. The only change between the two would be the name.

Let me know what you think! If there’s good feedback, I’ll write up an RFC!

Edit: Looking into the standard library, we also have FromPrimitive and FromString traits. Could these be replaced by a general From trait?

---

<div class="post-metadata">

**Author:** ![seanmonstar](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/seanmonstar/32/36_2.png) [@seanmonstar](https://internals.rust-lang.org/u/seanmonstar)\
**Post date:** [November 10, 2014, 5:28pm UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/2 "2014-11-10T17:28:31Z")

</div>

I was thinking the same thing this weekend, that there’s no particular reason this trait needs to be confined to Errors.

---

<div class="post-metadata">

**Author:** ![nstoddard](https://avatars.discourse-cdn.com/v4/letter/n/f07891/32.png) [@nstoddard](https://internals.rust-lang.org/u/nstoddard)\
**Post date:** [November 10, 2014, 8:25pm UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/3 "2014-11-10T20:25:18Z")

</div>

It would be nice if this could be used instead of FromPrimitive, ToPrimitive, FromStr, etc. However, those methods return the result wrapped in an Option, but FromError doesn’t. Should the generic From trait wrap the return value in an Option, or not? If not, it would mean that this couldn’t be used as a replacement for FromPrimitive etc.

I have some code in a linear algebra library that would be greatly simplified by something like this. I have a `Vector3<T>` struct and I sometimes need to convert between different `T`. AFAIK, it’s not currently easy to write a generic method to do the conversion, and it sounds like this proposal could help.

---

<div class="post-metadata">

**Author:** ![TyOverby](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tyoverby/32/363_2.png) [@TyOverby](https://internals.rust-lang.org/u/TyOverby)\
**Post date:** [November 10, 2014, 9:03pm UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/4 "2014-11-10T21:03:14Z")

</div>

Good point @nstoddard. Maybe two traits then? From and MaybeFrom?

---

<div class="post-metadata">

**Author:** ![tomjakubowski](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tomjakubowski/32/4876_2.png) [@tomjakubowski](https://internals.rust-lang.org/u/tomjakubowski)\
**Post date:** [November 11, 2014, 3:57am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/5 "2014-11-11T03:57:47Z")

</div>

Would you need MaybeFrom? What’s wrong with just implementing the trait on `Option<T>`, for example:

```rust
trait From<T> {
    fn from(x: T) -> Self;
}

impl<'a> From<&'a str> for Option<i64> {
    fn from(x: &str) -> Option<i64> {
        Some(1234)
    }
}

```

---

<div class="post-metadata">

**Author:** ![TyOverby](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tyoverby/32/363_2.png) [@TyOverby](https://internals.rust-lang.org/u/TyOverby)\
**Post date:** [November 11, 2014, 5:25am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/6 "2014-11-11T05:25:35Z")

</div>

@tomjakubowski: yep, that’s way better.

---

<div class="post-metadata">

**Author:** ![tomjakubowski](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tomjakubowski/32/4876_2.png) [@tomjakubowski](https://internals.rust-lang.org/u/tomjakubowski)\
**Post date:** [November 13, 2014, 4:34am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/7 "2014-11-13T04:34:00Z")

</div>

Replacing `FromPrimitive` with a generic `From` trait could be problematic because `FromPrimitive` has two required methods (for converting from `u64` and `i64` respectively) and a whole bunch of default methods for converting from the other primitive types. We could create blanket impls:

```rust
impl<T> From<u32> for T where T: From<u64> {
    fn from(x: u32) -> T { From::from(x as u64) }
}

```

But then you can’t do the equivalent of overriding `FromPrimitive`'s `from_u32` default method because of conflicting impls.

---

<div class="post-metadata">

**Author:** ![Gankra](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gankra/32/6002_2.png) [@Gankra](https://internals.rust-lang.org/u/Gankra)\
**Post date:** [November 14, 2014, 4:10am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/8 "2014-11-14T04:10:08Z")

</div>

Another great usecase for From: Genericizing over smart pointers!

[http://is.gd/ZSgrLG](http://is.gd/ZSgrLG)

---

<div class="post-metadata">

**Author:** ![P1start](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/p1start/32/23_2.png) [@P1start](https://internals.rust-lang.org/u/P1start)\
**Post date:** [November 14, 2014, 5:19am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/9 "2014-11-14T05:19:50Z")

</div>

I’m not 100% sure that this is a good idea. The purpose of a trait is not to require that a type has a method with a certain name with a certain set of argument types with a certain return type. The purpose of a trait is to require that a type has a method which does a certain thing. Generalising all `From` traits (`FromError`, `FromPrimitive`, `FromStr`, `FromIterator`, `BorrowFrom`) into a single trait will lose the extra requirements and invariants that must be obeyed that come with the trait, making a trait whose signature is all that matters, and the actual purpose of the function left undefined.

But to be honest I can’t think of a concrete example of any trait’s connotations that would be lost by a generalisation to a single `From` trait. So I’m going to say that this is a neat idea, and could be made even cooler if it were a special lang item trait that actually controlled the `as` operator. That would mean that you could convert a `String` to a `&str` with `as` (`"foo".to_string() as &str`), and maybe even convert them the other way round (perhaps with `to` and `into` operators instead).

---

<div class="post-metadata">

**Author:** ![TyOverby](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tyoverby/32/363_2.png) [@TyOverby](https://internals.rust-lang.org/u/TyOverby)\
**Post date:** [November 14, 2014, 5:34am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/10 "2014-11-14T05:34:29Z")

</div>

@Gankra: you are the guy that has been doing all the collections reform right? Would you be willing to write this RFC or help me write it?

---

<div class="post-metadata">

**Author:** ![Gankra](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gankra/32/6002_2.png) [@Gankra](https://internals.rust-lang.org/u/Gankra)\
**Post date:** [November 14, 2014, 2:54pm UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/11 "2014-11-14T14:54:25Z")

</div>

I don’t have a ton of time to be writing up RFCs right now, but I’m willing to provide help.

Also I pseudo-retract the SmartPtr point. If you know you need some concrete contents like int, then you can use Smaht, but if you just want to take a Smaht and then use it with multiple types (or just have your interface hide which types are needed), then you probably needed HKT or something.

---

<div class="post-metadata">

**Author:** ![arthurprs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arthurprs/32/233_2.png) [@arthurprs](https://internals.rust-lang.org/u/arthurprs)\
**Post date:** [November 14, 2014, 5:02pm UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/12 "2014-11-14T17:02:19Z")

</div>

That sounds very reasonable to do and will standardize an important concept (generic conversions).

---

<div class="post-metadata">

**Author:** ![mitsuhiko](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mitsuhiko/32/379_2.png) [@mitsuhiko](https://internals.rust-lang.org/u/mitsuhiko)\
**Post date:** [November 14, 2014, 10:07pm UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/13 "2014-11-14T22:07:01Z")

</div>

Strong -1 on this. I wrote a bunch of `FromSomething` traits already and they have vastly diverging requirements to the signature. For instance `FromError` in rust-welder changed the situation to include error location information. `FromRedisValue` in redis-rs has more than one method, `FromSQL` in Postgres has more than one parameter (postgres type + option value).

I can’t see how this would help anything.

---

<div class="post-metadata">

**Author:** ![mitsuhiko](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mitsuhiko/32/379_2.png) [@mitsuhiko](https://internals.rust-lang.org/u/mitsuhiko)\
**Post date:** [November 14, 2014, 10:08pm UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/14 "2014-11-14T22:08:24Z")

</div>

As an example for rust-welder: This is how the `FromError` trait looks in there:

```rust
pub trait FromError<E> {
    fn from_error(err: E, loc: Option<ErrorLocation>) -> Self;
}

```

---

<div class="post-metadata">

**Author:** ![TyOverby](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tyoverby/32/363_2.png) [@TyOverby](https://internals.rust-lang.org/u/TyOverby)\
**Post date:** [November 14, 2014, 10:57pm UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/15 "2014-11-14T22:57:48Z")

</div>

@mitsuhiko: I don’t see why that example couldn’t be rewritten as

```rust
impl From<(PostgresError, Option<ErrorLocation>)> for MyOtherErrorType {
    fn from(o: (PostgresError, Option<ErrorLocation>)) -> MyOtherErrorType {
        ...
    }
}

```

You could very easily have “arguments” as tuples of values.

---

<div class="post-metadata">

**Author:** ![arthurprs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arthurprs/32/233_2.png) [@arthurprs](https://internals.rust-lang.org/u/arthurprs)\
**Post date:** [November 14, 2014, 11:26pm UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/16 "2014-11-14T23:26:18Z")

</div>

I don’t think this is good enough. Although I don’t see why we shouldn’t have the 2 most common (From and MaybeFrom) in the stdlib.

---

<div class="post-metadata">

**Author:** ![TyOverby](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tyoverby/32/363_2.png) [@TyOverby](https://internals.rust-lang.org/u/TyOverby)\
**Post date:** [November 15, 2014, 1:19am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/17 "2014-11-15T01:19:31Z")

</div>

@arthurprs: I don’t see what would be so bad with

```rust
impl From<&str> for Option<uint> {
    ...
}

```

---

<div class="post-metadata">

**Author:** ![arthurprs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arthurprs/32/233_2.png) [@arthurprs](https://internals.rust-lang.org/u/arthurprs)\
**Post date:** [November 15, 2014, 1:38am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/18 "2014-11-15T01:38:55Z")

</div>

I was actually referring to the tuple parameter version.

---

<div class="post-metadata">

**Author:** ![mitsuhiko](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mitsuhiko/32/379_2.png) [@mitsuhiko](https://internals.rust-lang.org/u/mitsuhiko)\
**Post date:** [November 15, 2014, 10:00am UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/19 "2014-11-15T10:00:51Z")

</div>

The tuple solution is both looking ugly and not solving the problem where you need more than one method on the trait. So you will still see other `FromX` traits showing up. What’s the point of a generic `From` trait then?

---

<div class="post-metadata">

**Author:** ![TyOverby](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tyoverby/32/363_2.png) [@TyOverby](https://internals.rust-lang.org/u/TyOverby)\
**Post date:** [November 15, 2014, 8:33pm UTC](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783/20 "2014-11-15T20:33:43Z")

</div>

The point would be that in 90% of cases, converting between one type to another is good enough. Other languages have language features (overridable casting) to get exactly this.

[Next page](https://internals.rust-lang.org/t/pre-rfc-remove-fromerror-trait-add-from-trait/783.md?page=2)
