Skip to content

Instantly share code, notes, and snippets.

@hvariant
Created October 12, 2019 17:02
Show Gist options
  • Select an option

  • Save hvariant/d9f7c0ae5cca6503fe38858647faf033 to your computer and use it in GitHub Desktop.

Select an option

Save hvariant/d9f7c0ae5cca6503fe38858647faf033 to your computer and use it in GitHub Desktop.
use async_std::sync::{Arc, Mutex};
use serde::{Deserialize, Serialize};
use tide::{response, Context, EndpointResult};
#[derive(Serialize, Deserialize, Clone)]
struct Message {
author: Option<String>,
contents: String,
}
async fn return_json(ctx: Context<Arc<Mutex<Message>>>) -> EndpointResult {
let msg = &*ctx.state().lock().await;
Ok(response::json(msg))
}
fn main() {
let msg = Message {
author: Some("body".to_string()),
contents: "once told me".to_string(),
};
let mut app = tide::App::with_state(Arc::new(Mutex::new(msg)));
app.at("/json").get(return_json);
app.run("127.0.0.1:8000").unwrap();
}
@hvariant
Copy link
Author

error[E0277]: `(dyn futures_core::stream::Stream<Item = std::result::Result<bytes::bytes::Bytes, std::io::Error>> + std::marker::Send + 'static)` cannot be shared between threads safely
  --> presenter/src/main.rs:23:21
   |
23 |     app.at("/json").get(return_json);
   |                     ^^^ `(dyn futures_core::stream::Stream<Item = std::result::Result<bytes::bytes::Bytes, std::io::Error>> + std::marker::Send + 'static)` cannot be shared between threads safely
   |
   = help: the trait `std::marker::Sync` is not implemented for `(dyn futures_core::stream::Stream<Item = std::result::Result<bytes::bytes::Bytes, std::io::Error>> + std::marker::Send + 'static)`
   = note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<(dyn futures_core::stream::Stream<Item = std::result::Result<bytes::bytes::Bytes, std::io::Error>> + std::marker::Send + 'static)>`
   = note: required because it appears within the type `std::boxed::Box<(dyn futures_core::stream::Stream<Item = std::result::Result<bytes::bytes::Bytes, std::io::Error>> + std::marker::Send + 'static)>`
   = note: required because it appears within the type `std::pin::Pin<std::boxed::Box<(dyn futures_core::stream::Stream<Item = std::result::Result<bytes::bytes::Bytes, std::io::Error>> + std::marker::Send + 'static)>>`
   = note: required because it appears within the type `http_service::Body`
   = note: required because it appears within the type `http::request::Request<http_service::Body>`
   = note: required because it appears within the type `tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>`
   = note: required because of the requirements on the impl of `std::marker::Send` for `&tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>`
   = note: required because it appears within the type `for<'r, 's, 't0, 't1, 't2, 't3, 't4> {tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>, &'r tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>, tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>, &'s async_std::sync::mutex::Mutex<Message>, &'t0 std::sync::Arc<async_std::sync::mutex::Mutex<Message>>, impl std::future::Future, impl std::future::Future, ()}`
   = note: required because it appears within the type `[static generator@presenter/src/main.rs:11:75: 14:2 ctx:tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>> for<'r, 's, 't0, 't1, 't2, 't3, 't4> {tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>, &'r tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>, tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>, &'s async_std::sync::mutex::Mutex<Message>, &'t0 std::sync::Arc<async_std::sync::mutex::Mutex<Message>>, impl std::future::Future, impl std::future::Future, ()}]`
   = note: required because it appears within the type `std::future::GenFuture<[static generator@presenter/src/main.rs:11:75: 14:2 ctx:tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>> for<'r, 's, 't0, 't1, 't2, 't3, 't4> {tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>, &'r tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>, tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>, &'s async_std::sync::mutex::Mutex<Message>, &'t0 std::sync::Arc<async_std::sync::mutex::Mutex<Message>>, impl std::future::Future, impl std::future::Future, ()}]>`
   = note: required because it appears within the type `impl std::future::Future`
   = note: required because it appears within the type `impl std::future::Future`
   = note: required because of the requirements on the impl of `tide_core::endpoint::Endpoint<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>` for `fn(tide_core::context::Context<std::sync::Arc<async_std::sync::mutex::Mutex<Message>>>) -> impl std::future::Future {return_json}`

error: aborting due to previous error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment