summaryrefslogtreecommitdiff
path: root/examples/async_http_client.rs
diff options
context:
space:
mode:
authorAlex Orlenko <zxteam@protonmail.com>2020-05-06 02:32:05 +0100
committerAlex Orlenko <zxteam@protonmail.com>2020-05-11 02:43:34 +0100
commit7b0e4b4280b283271429c4f7315d6241d07418d1 (patch)
tree66c564dd1597a0d0397f03d3bb845d399c5f121c /examples/async_http_client.rs
parent6e2bb73cff65729118841f338577bfec14d593ec (diff)
downloadmlua-7b0e4b4280b283271429c4f7315d6241d07418d1.zip
Add Send capability to Lua
Diffstat (limited to 'examples/async_http_client.rs')
-rw-r--r--examples/async_http_client.rs11
1 files changed, 5 insertions, 6 deletions
diff --git a/examples/async_http_client.rs b/examples/async_http_client.rs
index 390dada..063c8b5 100644
--- a/examples/async_http_client.rs
+++ b/examples/async_http_client.rs
@@ -1,26 +1,25 @@
-use std::cell::RefCell;
use std::collections::HashMap;
-use std::rc::Rc;
+use std::sync::Arc;
use bstr::BString;
use hyper::{body::Body as HyperBody, Client as HyperClient};
-use tokio::stream::StreamExt;
+use tokio::{stream::StreamExt, sync::Mutex};
use mlua::{Error, Lua, Result, UserData, UserDataMethods};
#[derive(Clone)]
-struct BodyReader(Rc<RefCell<HyperBody>>);
+struct BodyReader(Arc<Mutex<HyperBody>>);
impl BodyReader {
fn new(body: HyperBody) -> Self {
- BodyReader(Rc::new(RefCell::new(body)))
+ BodyReader(Arc::new(Mutex::new(body)))
}
}
impl UserData for BodyReader {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_method("read", |_, reader, ()| async move {
- let mut reader = reader.0.borrow_mut();
+ let mut reader = reader.0.lock().await;
let bytes = reader.try_next().await.map_err(Error::external)?;
if let Some(bytes) = bytes {
return Ok(Some(BString::from(bytes.as_ref())));