pub struct Resp<P: Platform> { /* private fields */ }
Expand description
Resp
that uses Vec
as the backing collection type
use kwap::platform::Std;
use kwap::resp::Resp;
fn main() {
start_server(|req| {
let mut resp = Resp::<Std>::for_request(&req).unwrap();
resp.set_code(kwap::resp::code::CONTENT);
resp.set_option(12, Some(50)); // Content-Format: application/json
let payload = r#"""{
"foo": "bar",
"baz": "quux"
}"""#;
resp.set_payload(payload.bytes());
resp
});
}
fn start_server(f: impl FnOnce(kwap::req::Req<Std>) -> kwap::resp::Resp<Std>) {
// servery things
}
Implementations
sourceimpl<P: Platform> Resp<P>
impl<P: Platform> Resp<P>
sourcepub fn for_request(req: &Req<P>) -> Option<Self>
pub fn for_request(req: &Req<P>) -> Option<Self>
Create a new response for a given request.
If the request is CONfirmable, this will return Some(ACK).
If the request is NONconfirmable, this will return Some(NON).
If the request is EMPTY or RESET, this will return None.
use kwap::platform::{Message, Std};
use kwap::req::Req;
use kwap::resp::Resp;
// pretend this is an incoming request
let mut req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
req.set_msg_id(kwap_msg::Id(0));
req.set_msg_token(kwap_msg::Token(Default::default()));
let resp = Resp::<Std>::for_request(&req).unwrap();
let req_msg = Message::<Std>::from(req);
let resp_msg = Message::<Std>::from(resp);
// note that Req's default type is CON, so the response will be an ACK.
// this means that the token and id of the response will be the same
// as the incoming request.
assert_eq!(resp_msg.ty, kwap_msg::Type::Ack);
assert_eq!(req_msg.id, resp_msg.id);
assert_eq!(req_msg.token, resp_msg.token);
sourcepub fn ack(req: &Req<P>) -> Self
pub fn ack(req: &Req<P>) -> Self
Create a response ACKnowledging an incoming request.
An ack response must be used when you receive a CON request.
You may choose to include the response payload in an ACK, but keep in mind that you might receive duplicate If you do need to ensure they receive your response, you
sourcepub fn con(req: &Req<P>) -> Self
pub fn con(req: &Req<P>) -> Self
Create a CONfirmable response for an incoming request.
A confirmable response should be used when you receive a NON request and want to ensure the client receives your response
Note that it would be odd to respond to a CON request with an ACK followed by a CON response, because the client will keep resending the request until they receive the ACK.
The kwap
runtime will continually retry sending this until
an ACKnowledgement from the client is received.
sourcepub fn non(req: &Req<P>) -> Self
pub fn non(req: &Req<P>) -> Self
Create a NONconfirmable response for an incoming request.
A non-confirmable response should be used when:
- you receive a NON request and don’t need to ensure the client received the response
- you receive a CON request and don’t need to ensure the client received the response (you must ACK this type of request separately)
sourcepub fn payload(&self) -> impl Iterator<Item = &u8>
pub fn payload(&self) -> impl Iterator<Item = &u8>
Get the payload’s raw bytes
use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::Resp;
let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
// pretend this is an incoming response
let resp = Resp::<Std>::for_request(&req).unwrap();
let data: Vec<u8> = resp.payload().copied().collect();
sourcepub fn msg_type(&self) -> Type
pub fn msg_type(&self) -> Type
Get the message type
See kwap_msg::Type
for more info
sourcepub fn msg_id(&self) -> Id
pub fn msg_id(&self) -> Id
Get the message id
See kwap_msg::Id
for more info
sourcepub fn token(&self) -> Token
pub fn token(&self) -> Token
Get the message token
See kwap_msg::Token
for more info
sourcepub fn payload_string(&self) -> Result<String, FromUtf8Error>
pub fn payload_string(&self) -> Result<String, FromUtf8Error>
Get the payload and attempt to interpret it as an ASCII string
use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::Resp;
let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
// pretend this is an incoming response
let mut resp = Resp::<Std>::for_request(&req).unwrap();
resp.set_payload("hello!".bytes());
let data: String = resp.payload_string().unwrap();
sourcepub fn code(&self) -> Code
pub fn code(&self) -> Code
Get the response code
use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::{code, Resp};
// pretend this is an incoming request
let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
let resp = Resp::<Std>::for_request(&req).unwrap();
assert_eq!(resp.code(), code::CONTENT);
sourcepub fn set_code(&mut self, code: Code)
pub fn set_code(&mut self, code: Code)
Change the response code
use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::{code, Resp};
// pretend this is an incoming request
let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
let mut resp = Resp::<Std>::for_request(&req).unwrap();
resp.set_code(code::INTERNAL_SERVER_ERROR);
sourcepub fn set_option<V: IntoIterator<Item = u8>>(
&mut self,
number: u32,
value: V
) -> Option<(u32, V)>
pub fn set_option<V: IntoIterator<Item = u8>>(
&mut self,
number: u32,
value: V
) -> Option<(u32, V)>
Add a custom option to the response
If there was no room in the collection, returns the arguments back as Some(number, value)
.
Otherwise, returns None
.
use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::Resp;
// pretend this is an incoming request
let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
let mut resp = Resp::<Std>::for_request(&req).unwrap();
resp.set_option(17, Some(50)); // Accept: application/json
sourcepub fn set_payload<Bytes: IntoIterator<Item = u8>>(&mut self, payload: Bytes)
pub fn set_payload<Bytes: IntoIterator<Item = u8>>(&mut self, payload: Bytes)
Add a payload to this response
use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::Resp;
// pretend this is an incoming request
let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
let mut resp = Resp::<Std>::for_request(&req).unwrap();
// Maybe you have some bytes:
resp.set_payload(vec![1, 2, 3]);
// Or a string:
resp.set_payload("hello!".bytes());
Trait Implementations
sourceimpl<P: Platform> From<Message<<P as Platform>::MessagePayload, <P as Platform>::MessageOptionBytes, <P as Platform>::MessageOptions>> for Resp<P>
impl<P: Platform> From<Message<<P as Platform>::MessagePayload, <P as Platform>::MessageOptionBytes, <P as Platform>::MessageOptions>> for Resp<P>
sourceimpl<P: Platform> TryIntoBytes for Resp<P>
impl<P: Platform> TryIntoBytes for Resp<P>
type Error = <Message<<P as Platform>::MessagePayload, <P as Platform>::MessageOptionBytes, <P as Platform>::MessageOptions> as TryIntoBytes>::Error
Auto Trait Implementations
impl<P> RefUnwindSafe for Resp<P> where
<P as Platform>::MessageOptions: RefUnwindSafe,
<P as Platform>::MessagePayload: RefUnwindSafe,
<P as Platform>::NumberedOptions: RefUnwindSafe,
impl<P> Send for Resp<P> where
<P as Platform>::MessageOptions: Send,
<P as Platform>::MessagePayload: Send,
<P as Platform>::NumberedOptions: Send,
impl<P> Sync for Resp<P> where
<P as Platform>::MessageOptions: Sync,
<P as Platform>::MessagePayload: Sync,
<P as Platform>::NumberedOptions: Sync,
impl<P> Unpin for Resp<P> where
<P as Platform>::MessageOptions: Unpin,
<P as Platform>::MessagePayload: Unpin,
<P as Platform>::NumberedOptions: Unpin,
impl<P> UnwindSafe for Resp<P> where
<P as Platform>::MessageOptions: UnwindSafe,
<P as Platform>::MessagePayload: UnwindSafe,
<P as Platform>::NumberedOptions: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more