Skip to main content

Crate wfu

Crate wfu 

Source
Expand description

§Worksoup’s Formatting Utilities

Crates.io Documentation License

[dependencies]
wfu = "0.0.3"
use wfu::*;
use std::ops::Deref;


#[derive(Debug, Clone, Copy, Default)]
struct UppercaseProxy;
impl FmtHandler<&str> for UppercaseProxy {
    #[inline]
    fn fmt(&self, data: &&str, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
        for c in data.chars() {
            write!(f, "{}", c.to_uppercase())?;
        }
        Ok(())
    }
}
impl FmtHandler<str> for UppercaseProxy {
    #[inline]
    fn fmt(&self, data: &str, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
        for c in data.chars() {
            write!(f, "{}", c.to_uppercase())?;
        }
        Ok(())
    }
}
impl FmtHandler<String> for UppercaseProxy {
    #[inline(always)]
    fn fmt(&self, data: &String, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
        <Self as FmtHandler<str>>::fmt(self, data, f)
    }
}

fn main() {
    let s = "hello world";

    let result = format!("{}", s.fmt_as::<UppercaseProxy>());
    assert_eq!(result, "HELLO WORLD");
}

§Provided

§内置

use wfu::*;
 
let s = "hello";
let display_result = format!("{}", s.fmt_as::<DisplayProxy>());
let debug_result = format!("{}", s.fmt_as::<DebugProxy>());

let vec = vec!["a", "b", "c"];
// Joined
let joined = format!("{}", vec.fmt_by(Joined(", ")));
assert_eq!(joined, "a, b, c");

// Repeat
let stars = format!("{}", "*".fmt_by(Repeat(5)));
assert_eq!(stars, "*****");

§闭包

use wfu::*;

let value = 42;
let holder = value.fmt_with(&|v, f| write!(f, "Value: {} ({:#x})", v, v));
let result = format!("{}", holder);
assert_eq!(result, "Value: 42 (0x2a)");

Structs§

BinaryProxy
CloneIterDebugMap
用于克隆后迭代并格式化为调试映射的格式化处理器
CloneIterJoined
用于连接迭代器元素的格式化处理器,适用于需克隆才能迭代的类型(如 Range
DebugMap
用于将迭代器格式化为调试映射的格式化处理器
DebugProxy
DerefHolder
DisplayProxy
FmtFn
通用格式化函数包装器
FmtFnProxy
FmtWithWrapper
Joined
用于连接迭代器元素的格式化处理器
LowerExpProxy
LowerHexProxy
OctalProxy
PointerProxy
RefItemDebugMap
用于将元素为引用的迭代器格式化为调试映射的格式化处理器
Repeat
TargetHolder
UpperExpProxy
UpperHexProxy

Traits§

FmtAs
FmtBy
FmtByHolder
FmtHandler
FmtWith
WithInner