fMeow / maybe-async-rs

A procedure macro to unify SYNC and ASYNC implementation for downstream application/crates
https://docs.rs/maybe-async
MIT License
146 stars 17 forks source link

#[sync_impl] not switching off compilation for block #9

Closed GeoffClements closed 3 years ago

GeoffClements commented 3 years ago

I have created a minimal example to show this problem:

main.rs

use maybe_async::{async_impl, sync_impl};

#[cfg(not(feature = "is_sync"))]
use async_std::{io, task::{Context, Poll}};
#[cfg(feature = "is_sync")]
use std::io;

struct ReadInner<R> {
    inner: R,
}

impl<R: io::Read> io::Read for ReadInner<R> {
    #[sync_impl]
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.read(buf)
    }

    #[async_impl]
    fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize>> {
        self.inner.poll_read(cx, buf)
    }
}

fn main() {
    println!("Hello, world!");
}

Cargo.toml

[dependencies]
async-std = "1.9"
maybe-async = {version = "0.2", features = ["is_sync"]}

[features]
is_sync = []
$ cargo check --features is_sync
    Checking asyncer v0.1.0 (/home/geoff/temp/asyncer)
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
$ cargo check 
    Checking asyncer v0.1.0 (/home/geoff/temp/asyncer)
error[E0407]: method `read` is not a member of trait `io::Read`
  --> src/main.rs:14:5
   |
14 | /     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
15 | |         self.inner.read(buf)
16 | |     }
   | |_____^ not a member of trait `io::Read`

warning: unused imports: `Context`, `Poll`
 --> src/main.rs:4:28
  |
4 | use async_std::{io, task::{Context, Poll}};
  |                            ^^^^^^^  ^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0046]: not all trait items implemented, missing: `poll_read`
  --> src/main.rs:12:1
   |
12 | impl<R: io::Read> io::Read for ReadInner<R> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `poll_read` in implementation
   |
   = help: implement the missing item: `fn poll_read(self: Pin<&mut Self>, _: &mut Context<'_>, _: &mut [u8]) -> Poll<Result<usize, std::io::Error>> { todo!() }`

error[E0599]: no method named `read` found for type parameter `R` in the current scope
  --> src/main.rs:15:20
   |
15 |         self.inner.read(buf)
   |                    ^^^^ method not found in `R`
   |
   = help: items from traits can only be used if the trait is in scope
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
   |
1  | use std::io::Read;
   |
1  | use async_std::io::ReadExt;
   |
1  | use futures_lite::io::AsyncReadExt;
   |

error: aborting due to 3 previous errors; 1 warning emitted

Some errors have detailed explanations: E0046, E0407, E0599.
For more information about an error, try `rustc --explain E0046`.
error: could not compile `asyncer`

To learn more, run the command again with --verbose.
fMeow commented 3 years ago

I think it needs importing std::io::Read trait in is_sync implementation to enable self.inner.read(buf) call.

Try adding Read in import:

#[cfg(feature = "is_sync")]
use std::io::{self, Read};
GeoffClements commented 3 years ago

It was my configuration Cargo.toml that was causing the problem.

I should have had:

[dependencies]
async-std = "1.9"
maybe-async = "0.2"

[features]
is_sync = ["maybe-async/is_sync"]

I think it was the example in the README that threw me. Anyway, closing.

Thanks!