Incompatible types with correct function signatures [duplicate]











up vote
2
down vote

favorite













This question already has an answer here:




  • How do I conditionally return different types of futures?

    1 answer



  • Why can impl trait not be used to return multiple / conditional types?

    1 answer




I have 2 functions which return the same type impl Future<Item = (), Error = ()>



extern crate tokio;
extern crate futures;

use tokio::timer::Interval;
use std::time::{Duration, Instant};
use tokio::prelude::*;
use futures::future;

fn run2() -> impl Future<Item = (), Error = ()> {
future::ok(())
}

fn run() -> impl Future<Item = (), Error = ()> {
Interval::new(Instant::now(), Duration::from_millis(1000))
.for_each(move |instant| {
println!("fire; instant={:?}", instant);
Ok(())
})
.map_err(|e| panic!("interval errored; err={:?}", e))
}

fn main_run() -> impl Future<Item = (), Error = ()> {
if 1 == 1 {
run()
} else {
run2()
}
}

fn main() {
tokio::run(main_run());
}


playground



I'm trying to execute functions conditionally in main_run, but I get a weird error:



error[E0308]: if and else have incompatible types
--> src/main.rs:23:5
|
23 | / if 1 == 1 {
24 | | run()
25 | | } else {
26 | | run2()
27 | | }
| |_____^ expected opaque type, found a different opaque type
|
= note: expected type `impl futures::Future` (opaque type)
found type `impl futures::Future` (opaque type)


Both functions return the same type: impl Future<Item = (), Error = ()>

Why is compiler not happy?



EDIT:

Since it was a duplicate I found a solution in the response to the original question, however here is the solution to this particular question if someone stumbles upon this question in the future:



extern crate tokio;
extern crate futures;

use tokio::timer::Interval;
use std::time::{Duration, Instant};
use tokio::prelude::*;
use futures::future;

fn run2() -> Box<Future<Item = (), Error = ()> + Send> {
Box::new(future::ok(()))
}

fn run() -> Box<Future<Item = (), Error = ()> + Send> {
Box::new(Interval::new(Instant::now(), Duration::from_millis(1000))
.for_each(move |instant| {
println!("fire; instant={:?}", instant);
Ok(())
})
.map_err(|e| panic!("interval errored; err={:?}", e)))
}

fn main_run() -> impl Future<Item = (), Error = ()> {
if 1 == 1 {
run()
} else {
run2()
}
}

fn main() {
tokio::run(main_run());
}


playgound










share|improve this question















marked as duplicate by Stargateur, Community Nov 5 at 2:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    2
    down vote

    favorite













    This question already has an answer here:




    • How do I conditionally return different types of futures?

      1 answer



    • Why can impl trait not be used to return multiple / conditional types?

      1 answer




    I have 2 functions which return the same type impl Future<Item = (), Error = ()>



    extern crate tokio;
    extern crate futures;

    use tokio::timer::Interval;
    use std::time::{Duration, Instant};
    use tokio::prelude::*;
    use futures::future;

    fn run2() -> impl Future<Item = (), Error = ()> {
    future::ok(())
    }

    fn run() -> impl Future<Item = (), Error = ()> {
    Interval::new(Instant::now(), Duration::from_millis(1000))
    .for_each(move |instant| {
    println!("fire; instant={:?}", instant);
    Ok(())
    })
    .map_err(|e| panic!("interval errored; err={:?}", e))
    }

    fn main_run() -> impl Future<Item = (), Error = ()> {
    if 1 == 1 {
    run()
    } else {
    run2()
    }
    }

    fn main() {
    tokio::run(main_run());
    }


    playground



    I'm trying to execute functions conditionally in main_run, but I get a weird error:



    error[E0308]: if and else have incompatible types
    --> src/main.rs:23:5
    |
    23 | / if 1 == 1 {
    24 | | run()
    25 | | } else {
    26 | | run2()
    27 | | }
    | |_____^ expected opaque type, found a different opaque type
    |
    = note: expected type `impl futures::Future` (opaque type)
    found type `impl futures::Future` (opaque type)


    Both functions return the same type: impl Future<Item = (), Error = ()>

    Why is compiler not happy?



    EDIT:

    Since it was a duplicate I found a solution in the response to the original question, however here is the solution to this particular question if someone stumbles upon this question in the future:



    extern crate tokio;
    extern crate futures;

    use tokio::timer::Interval;
    use std::time::{Duration, Instant};
    use tokio::prelude::*;
    use futures::future;

    fn run2() -> Box<Future<Item = (), Error = ()> + Send> {
    Box::new(future::ok(()))
    }

    fn run() -> Box<Future<Item = (), Error = ()> + Send> {
    Box::new(Interval::new(Instant::now(), Duration::from_millis(1000))
    .for_each(move |instant| {
    println!("fire; instant={:?}", instant);
    Ok(())
    })
    .map_err(|e| panic!("interval errored; err={:?}", e)))
    }

    fn main_run() -> impl Future<Item = (), Error = ()> {
    if 1 == 1 {
    run()
    } else {
    run2()
    }
    }

    fn main() {
    tokio::run(main_run());
    }


    playgound










    share|improve this question















    marked as duplicate by Stargateur, Community Nov 5 at 2:53


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite












      This question already has an answer here:




      • How do I conditionally return different types of futures?

        1 answer



      • Why can impl trait not be used to return multiple / conditional types?

        1 answer




      I have 2 functions which return the same type impl Future<Item = (), Error = ()>



      extern crate tokio;
      extern crate futures;

      use tokio::timer::Interval;
      use std::time::{Duration, Instant};
      use tokio::prelude::*;
      use futures::future;

      fn run2() -> impl Future<Item = (), Error = ()> {
      future::ok(())
      }

      fn run() -> impl Future<Item = (), Error = ()> {
      Interval::new(Instant::now(), Duration::from_millis(1000))
      .for_each(move |instant| {
      println!("fire; instant={:?}", instant);
      Ok(())
      })
      .map_err(|e| panic!("interval errored; err={:?}", e))
      }

      fn main_run() -> impl Future<Item = (), Error = ()> {
      if 1 == 1 {
      run()
      } else {
      run2()
      }
      }

      fn main() {
      tokio::run(main_run());
      }


      playground



      I'm trying to execute functions conditionally in main_run, but I get a weird error:



      error[E0308]: if and else have incompatible types
      --> src/main.rs:23:5
      |
      23 | / if 1 == 1 {
      24 | | run()
      25 | | } else {
      26 | | run2()
      27 | | }
      | |_____^ expected opaque type, found a different opaque type
      |
      = note: expected type `impl futures::Future` (opaque type)
      found type `impl futures::Future` (opaque type)


      Both functions return the same type: impl Future<Item = (), Error = ()>

      Why is compiler not happy?



      EDIT:

      Since it was a duplicate I found a solution in the response to the original question, however here is the solution to this particular question if someone stumbles upon this question in the future:



      extern crate tokio;
      extern crate futures;

      use tokio::timer::Interval;
      use std::time::{Duration, Instant};
      use tokio::prelude::*;
      use futures::future;

      fn run2() -> Box<Future<Item = (), Error = ()> + Send> {
      Box::new(future::ok(()))
      }

      fn run() -> Box<Future<Item = (), Error = ()> + Send> {
      Box::new(Interval::new(Instant::now(), Duration::from_millis(1000))
      .for_each(move |instant| {
      println!("fire; instant={:?}", instant);
      Ok(())
      })
      .map_err(|e| panic!("interval errored; err={:?}", e)))
      }

      fn main_run() -> impl Future<Item = (), Error = ()> {
      if 1 == 1 {
      run()
      } else {
      run2()
      }
      }

      fn main() {
      tokio::run(main_run());
      }


      playgound










      share|improve this question
















      This question already has an answer here:




      • How do I conditionally return different types of futures?

        1 answer



      • Why can impl trait not be used to return multiple / conditional types?

        1 answer




      I have 2 functions which return the same type impl Future<Item = (), Error = ()>



      extern crate tokio;
      extern crate futures;

      use tokio::timer::Interval;
      use std::time::{Duration, Instant};
      use tokio::prelude::*;
      use futures::future;

      fn run2() -> impl Future<Item = (), Error = ()> {
      future::ok(())
      }

      fn run() -> impl Future<Item = (), Error = ()> {
      Interval::new(Instant::now(), Duration::from_millis(1000))
      .for_each(move |instant| {
      println!("fire; instant={:?}", instant);
      Ok(())
      })
      .map_err(|e| panic!("interval errored; err={:?}", e))
      }

      fn main_run() -> impl Future<Item = (), Error = ()> {
      if 1 == 1 {
      run()
      } else {
      run2()
      }
      }

      fn main() {
      tokio::run(main_run());
      }


      playground



      I'm trying to execute functions conditionally in main_run, but I get a weird error:



      error[E0308]: if and else have incompatible types
      --> src/main.rs:23:5
      |
      23 | / if 1 == 1 {
      24 | | run()
      25 | | } else {
      26 | | run2()
      27 | | }
      | |_____^ expected opaque type, found a different opaque type
      |
      = note: expected type `impl futures::Future` (opaque type)
      found type `impl futures::Future` (opaque type)


      Both functions return the same type: impl Future<Item = (), Error = ()>

      Why is compiler not happy?



      EDIT:

      Since it was a duplicate I found a solution in the response to the original question, however here is the solution to this particular question if someone stumbles upon this question in the future:



      extern crate tokio;
      extern crate futures;

      use tokio::timer::Interval;
      use std::time::{Duration, Instant};
      use tokio::prelude::*;
      use futures::future;

      fn run2() -> Box<Future<Item = (), Error = ()> + Send> {
      Box::new(future::ok(()))
      }

      fn run() -> Box<Future<Item = (), Error = ()> + Send> {
      Box::new(Interval::new(Instant::now(), Duration::from_millis(1000))
      .for_each(move |instant| {
      println!("fire; instant={:?}", instant);
      Ok(())
      })
      .map_err(|e| panic!("interval errored; err={:?}", e)))
      }

      fn main_run() -> impl Future<Item = (), Error = ()> {
      if 1 == 1 {
      run()
      } else {
      run2()
      }
      }

      fn main() {
      tokio::run(main_run());
      }


      playgound





      This question already has an answer here:




      • How do I conditionally return different types of futures?

        1 answer



      • Why can impl trait not be used to return multiple / conditional types?

        1 answer








      rust






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 5 at 5:50

























      asked Nov 5 at 2:03









      Leonti

      4,50173057




      4,50173057




      marked as duplicate by Stargateur, Community Nov 5 at 2:53


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Stargateur, Community Nov 5 at 2:53


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.































          active

          oldest

          votes






















          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()