Announcing specializer 1.0.0

⚓ rust    📅 2025-06-13    👤 surdeus    👁️ 2      

surdeus

https://crates.io/crates/specializer

This crate isn't really solving anything that hasn't been solved, but more an alternate API design exploration from some existing crates for specialization on specific types (where the nightly feature of specialization for types implementing specific traits isn't part of this). Some alternatives are listed here:

Rather than using macros, this uses a builder-like pattern (not quite the builder pattern, since it executes at the final step instead of building an instance of a type). Here's an example of it in action:

use specializer::Specializer;

fn specialized<T>(ty: T) -> String
where
    T: 'static
{
    let fallback = |_| "unknown".to_owned();

    Specializer::new(ty, fallback)
        .specialize_param(|int: i32| (int * 2).to_string())
        .specialize_param(|string: String| string)
        .run()
}

assert_eq!(specialized(3), "6");
assert_eq!(specialized("Hello world".to_string()), "Hello world");
assert_eq!(specialized(()), "unknown");

There are also different variants of the Specializer for async (which gets to use the newish AsyncFnOnce) and borrowed return/parameters, which should cover most use cases.

1 post - 1 participant

Read full topic

🏷️ rust_feed