Skip to content

Commit

Permalink
chore: Use tokio in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Sep 17, 2023
1 parent 5af9db6 commit bc8eed8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ md5 = "0.7.0"
rand = { version = "0.8.5", features = ["small_rng"] }
structopt = "0.3.26"
tempfile = "3.3.0"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }

# deps for tftpd-targz.rs
async-compression = { version = "0.3.15", features = ["gzip", "futures-io"] }
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ Features:
use async_tftp::server::TftpServerBuilder;
use async_tftp::Result;

fn main() -> Result<()> {
smol::block_on(async { // or any other runtime/executor
let tftpd = TftpServerBuilder::with_dir_ro(".")?.build().await?;
tftpd.serve().await?;
Ok(())
})
#[tokio::main] // or any other runtime/executor
async fn main() -> Result<()> {
let tftpd = TftpServerBuilder::with_dir_ro(".")?.build().await?;
tftpd.serve().await?;
Ok(())
}
```

Add in `Cargo.toml`:

```toml
[dependencies]
smol = "1" # or any other runtime/executor
async-tftp = "0.3"
# or any other runtime/executor
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
```

## Running examples with cargo
Expand Down
22 changes: 10 additions & 12 deletions examples/tftpd-dir.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use async_tftp::server::TftpServerBuilder;
use futures_lite::future::block_on;

#[tokio::main]
fn main() -> Result<()> {
fern::Dispatch::new()
.level(log::LevelFilter::Info)
Expand All @@ -10,17 +10,15 @@ fn main() -> Result<()> {
.apply()
.expect("Failed to initialize logger");

block_on(async {
let tftpd = TftpServerBuilder::with_dir_ro(".")?
.bind("0.0.0.0:6969".parse().unwrap())
// Workaround to handle cases where client is behind VPN
.block_size_limit(1024)
.build()
.await?;
let tftpd = TftpServerBuilder::with_dir_ro(".")?
.bind("0.0.0.0:6969".parse().unwrap())
// Workaround to handle cases where client is behind VPN
.block_size_limit(1024)
.build()
.await?;

log::info!("Listening on: {}", tftpd.listen_addr()?);
tftpd.serve().await?;
log::info!("Listening on: {}", tftpd.listen_addr()?);
tftpd.serve().await?;

Ok(())
})
Ok(())
}
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
//! use async_tftp::server::TftpServerBuilder;
//! use async_tftp::Result;
//!
//! fn main() -> Result<()> {
//! smol::block_on(async { // or any other runtime/executor
//! let tftpd = TftpServerBuilder::with_dir_ro(".")?.build().await?;
//! tftpd.serve().await?;
//! Ok(())
//! })
//! #[tokio::main] // or any other runtime/executor
//! async fn main() -> Result<()> {
//! let tftpd = TftpServerBuilder::with_dir_ro(".")?.build().await?;
//! tftpd.serve().await?;
//! Ok(())
//! }
//! ```
//!
//! Add in `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! smol = "1" # or any other runtime/executor
//! async-tftp = "0.3"
//! # or any other runtime/executor
//! tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
//! ```
//!
//! [smol]: https://docs.rs/smol
Expand Down

0 comments on commit bc8eed8

Please sign in to comment.