Expand description
Some tools to help with the return value
§Examples
ⓘ
use what_i_want::*;
fn login(username: String) -> bool {
require!(username == "admin", false);
...
}
fn login2(username: String) {
require!(username == "admin");
...
}
§Handling Result and Option
§Examples
ⓘ
// Before using `what_i_want`
pub async fn get_mutipart_data(mut mutipart_data: Multipart) -> MultipartData {
// Nested hell, and different Enum (Result, Option) handling
// Of course this code is just for demonstration
while let Some(Ok(mut field)) = mutipart_data.next().await {
if let Some(disposition) = field.headers().get(&header::CONTENT_DISPOSITION) {
if let Ok(disposition_str) = disposition.to_str() {
if let Some(dis) = ContentDisposition::parse(disposition_str) {
if let Some(key) = dis.name {
while let Some(Ok(chunk)) = field.next().await {
...
}
}
}
}
}
}
MultipartData { ... }
}
// After using `what_i_want`
use what_i_want::*;
async fn get_mutipart_data(mut mutipart_data: Multipart) -> MultipartData {
while let Some(Ok(mut field)) = mutipart_data.next().await {
let disposition = unwrap_or_continue!(field.headers().get(&header::CONTENT_DISPOSITION));
let disposition_str = unwrap_or_continue!(disposition.to_str());
let dis = unwrap_or_continue!(ContentDisposition::parse(disposition_str));
let key = unwrap_or_continue!(dis.name);
while let Some(Ok(chunk)) = field.next().await {
...
}
}
MultipartData { ... }
}
Macros§
- require
- Execute if the condition is true, otherwise return
- unwrap_
or_ continue - If it’s not what you want, then do
continue
- unwrap_
or_ do - If it’s not what you want, then do what you want
- unwrap_
or_ false - If it’s not what you want, then do
return false
- unwrap_
or_ return - If it’s not what you want, then do
return ()
- unwrap_
or_ true - If it’s not what you want, then do
return true
- unwrap_
or_ val - If it’s not what you want, then do
return <defined return value>
Traits§
- What
Iwant - Implement
WhatIwant
and let us know what you want