Trait Gan

Source
pub trait Gan: Sized {
    // Provided methods
    fn ignore(self) { ... }
    fn ok<E>(self) -> Result<(), E> { ... }
    fn okay<E>(self) -> Result<Self, E> { ... }
    fn some(self) -> Option<Self> { ... }
    fn none<E>(self) -> Option<E> { ... }
}
Expand description

核心拓展方法集
Core extension trait

Provided Methods§

Source

fn ignore(self)

直接忽略当前值
Ignores the value completely (alternative to let _ = ...)

§示例 / Example
//使用前 / Before:  
let _ = format!("{}", 42);
//使用后 / After:
use gan::Gan;
format!("{}", 42).ignore();
Source

fn ok<E>(self) -> Result<(), E>

消耗当前值并返回 Ok
Consumes the value and returns Ok
故意命名为ok,由于 Result 的 ok 方法已经被占用,所以避免被误用,Result 必须通过 .ignore().ok()来显式忽略并返回ok
Named as ok to avoid conflict with Result’s ok method. Result must be explicitly ignored and returned as ok using .ignore().ok()

§示例 / Example
//使用前 / Before:
use std::io::{Write,Error};
use std::fs::OpenOptions;
fn write_(file: &str, data:&str) -> Result<(), Error> {
    let mut file = OpenOptions::new().append(true).open(file)?;
    write!(file, "{}", data)?;
    Ok(())
}
//使用后 / After:
use gan::Gan;
fn write(file: &str, data:&str) -> Result<(), Error> {
    let mut file = OpenOptions::new().append(true).open(file)?;
    write!(file, "{}", data)?.ok()
    // write!(file, "{}", data).ok()// 无法通过编译,防止错误的省略?
}
Source

fn okay<E>(self) -> Result<Self, E>

返回原值的 Ok 包装
Wraps self into Ok variant

§示例 / Example
//使用前 / Before:
fn get_data_() -> Result<i32, String> {
   Ok(42)
}
//使用后 / After:
use gan::Gan;
fn get_data() -> Result<i32, String> {
  42.okay()
}
Source

fn some(self) -> Option<Self>

返回 Some 包装
Wraps into Some

§示例 / Example
//使用前 / Before:
let opt: Option<i32> = Some(42);
//使用后 / After:
use gan::Gan;
let opt: Option<i32> = 42.some();
Source

fn none<E>(self) -> Option<E>

构造空 None 值
Creates None of a type while ignoring self

§示例 / Example
//使用前 / Before:  
let opt: Option<i32> = None;
//使用后 / After:
use gan::Gan;
let opt: Option<i32> = 42.none();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Sized> Gan for T