Trait `Send` required but single threaded

Friends
I have a simple example using the num crate that will not compile because:

13 |                 Err(err) => panic!(err),
   |                             ^^^^^^^^^^^ `<T as num_traits::Num>::FromStrRadixErr` cannot be sent between threads safely
   |
   = help: the trait `std::marker::Send` is not implemented for `<T as num_traits::Num>::FromStrRadixErr`

Is this because of requirements of the num crate that I cannot see?

The sample code is:

use num::Num;

struct Foo<T> {
    v:T,
}

impl<T> Foo<T> {
    fn new(v:&'static str) -> Foo<T>
        where T: Num {
        Foo{
            v:match <T as Num>::from_str_radix(v, 10){
                Ok(v) => v,
                Err(err) => panic!(err),
            },
        }
    }
}

fn main() {
    let f:Foo<f64> = Foo::new("-1");
    println!("Foo{{{}}}", f.v);
}

Changing the where clause to:

        where <T as Num>::FromStrRadixErr: 'static,
              T: Num,
    <T as Num>::FromStrRadixErr: std::marker::Send {

fixes it, but I do not understand why. I am unhappy with "magic incantations" so I would like to know! I need to kno the role of the life time 'static in this context. The struct this is part of in the bigger code has a life time associated with it, surely that is the life time I should use. But why?

Also in the bigger case, but not here, I have to put std::fmt::Debug into the where clause for <T as Num>::FromStrRadixErr. Why?

This is because you are using panic incorrectly. A panic can be caught and then sent across thread boundaries, so it first argument of a panic should be Send. Normally, a panic takes a string/format string,

panic!("an error has occured");
panic!("an error has occured: {}", err);

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.