Struct MemoryAdapter

Source
pub struct MemoryAdapter { /* private fields */ }

Implementations§

Source§

impl MemoryAdapter

Source

pub fn new() -> MemoryAdapter

Examples found in repository?
examples/3.rs (line 10)
8fn main() {
9  let mut server = Server::new();
10  let database = memory::MemoryAdapter::new();
11  server.resource("/cats", move |req: Request| {
12    database.handle(req)
13  });
14  server.listen("127.0.0.1:3000");
15}
More examples
Hide additional examples
examples/5.rs (line 11)
8fn main() {
9  let mut server = Server::new();
10  use std::sync::Arc;
11  let database = Arc::new(memory::MemoryAdapter::new());
12  server.resource("/cats", move |req: Request| {
13    let database1 = database.clone();
14    req
15      .and_then(move |req| {
16        database1.handle(req)
17      })
18  });
19  server.listen("127.0.0.1:3000");
20}
examples/4.rs (line 10)
8fn main() {
9  let mut server = Server::new();
10  let database = memory::MemoryAdapter::new();
11  server.resource("/cats", move |req: Request| {
12    database.handle(req).and_then(|mut reply| {
13      {
14          let mut data = reply.data_mut().unwrap();
15          data.insert("example".to_string(), json!("data"));
16      }
17      reply
18    })
19  });
20  server.listen("127.0.0.1:3000");
21}
examples/7.rs (line 12)
8fn main() {
9  let mut server = Server::new();
10  use std::sync::Arc;
11  use std::ops::Deref;
12  let database = Arc::new(memory::MemoryAdapter::new());
13  let chan = Arc::new(memory::MemoryChannel::new());
14  server.resource("/cats", move |req: Request| {
15    let database1 = database.clone();
16    let chan1 = chan.clone();
17    let chan2 = chan.clone();
18    req
19      .and_then(move |req| {
20        match req.method() {
21          Method::Listen => chan1.handle(req),
22          _ => database1.handle(req),
23        }
24      })
25      .and_then(move |reply| {
26        util::send_from_reply(reply, chan2.deref())
27      })
28  });
29  server.listen("127.0.0.1:3000");
30}
examples/6.rs (line 11)
8fn main() {
9  let mut server = Server::new();
10  use std::sync::Arc;
11  let database = Arc::new(memory::MemoryAdapter::new());
12  server.resource("/cats", move |req: Request| {
13    let database1 = database.clone();
14    req
15      .and_then(move |req| {
16        if req.method() == Method::Delete {
17          if let &JsonValue::String(ref password) = req.param("password") {
18            if password != "meow" {
19              return Error::forbidden("incorrect password");
20            }
21          } else {
22            return Error::unauthorized("please provide a password");
23          }
24        }
25        req.boxed()
26      })
27      .and_then(move |req| {
28        database1.handle(req)
29      })
30  });
31  server.listen("127.0.0.1:3000");
32}
examples/hello.rs (line 12)
7fn main() {
8  let mut s = Server::new();
9  s.resource("/meow", |_req: Request| {
10    Error::forbidden("not allowed! sorry.")
11  });
12  let adapter = Arc::new(memory::MemoryAdapter::new());
13  let channel = Arc::new(memory::MemoryChannel::new());
14  s.resource("/hello2", move |req: Request| {
15    req
16      .and_then(|req| {
17        req.into_reply(JsonObject::new())
18      })
19  });
20  s.resource("/hello", move |req: Request| {
21    let adapter = adapter.clone();
22    let channel1 = channel.clone();
23    // let channel2 = channel.clone();
24    req
25      .and_then(move |req| {
26        match req.method().clone() {
27          Method::Action(_) => Error::forbidden("not allowed! sorry."),
28          Method::Listen => channel1.handle(req),
29          _ => adapter.handle(req),
30        }
31      })
32      // .and_then(move |reply| { util::send_from_reply(reply, channel2.deref()) })
33  });
34  s.listen("127.0.0.1:3000");
35}

Trait Implementations§

Source§

impl Adapter for MemoryAdapter

Source§

fn list( &self, params: &JsonObject, ) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>

currently this function only supports equality matching — we’d probably want to add more kinds of matching and querying in the future, maybe by building into query object

Source§

fn get( &self, id: &str, _params: &JsonObject, ) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>

Source§

fn post( &self, data: &JsonObject, _params: &JsonObject, ) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>

Source§

fn patch( &self, id: &str, data: &JsonObject, _params: &JsonObject, ) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>

Source§

fn delete( &self, id: &str, _params: &JsonObject, ) -> BoxFuture<JsonObject, (ErrorKind, JsonValue)>

Source§

fn handle(&self, req: Request) -> BoxFuture<Reply, Error>

Takes a Request, passes it to the appropriate function, and turns the response into a proper Reply future. If you’re using an Adapter in your webapp, this is the function you want to call.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.