| Crates.io | sealed-generic |
| lib.rs | sealed-generic |
| version | 0.1.1 |
| created_at | 2025-09-12 22:34:20.508308+00 |
| updated_at | 2025-09-13 02:47:27.030076+00 |
| description | Sealed generic type discriminants |
| homepage | |
| repository | https://github.com/joshua-auchincloss/sealed-generic.git |
| max_upload_size | |
| id | 1836991 |
| size | 28,707 |
Sealed generic is a crate which offers sealed discriminants of a generic type.
The primary use case for this library are use cases where you may not be able to provide generics, or require a concrete type due to separate constraints.
use sealed_generic::SealedGeneric;
// derive "SealedGeneric"
#[derive(SealedGeneric)]
#[define(
// #[define(types(ty = i32))] generates "BasicI32"
types(ty = i32),
// #[define(types(with(ty = i64)))] generates "BasicI32" with no additional options
types(with(
ty = i64,
)),
// define "BasicU64", and derive #[derive(PartialEq, Eq, Debug)]
types(with(
ty = u64,
derives = PartialEq,
derives = Eq,
derives = Debug
)),
// define "BasicI16", and derive #[derive(serde::Deserialize, serde::Serialize)]
// with #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
types(
with(
ty = i16,
attr = "serde(rename_all = \"SCREAMING_SNAKE_CASE\")",
derives = serde::Deserialize,
derives = serde::Serialize,
)
)
)]
struct Basic<T> {
ty: T,
}
While turned off by default, you may seal the generic types of the struct to the types generated from the derive macro.
Note: you must add Sealed<name of struct> as a type constraint.
use sealed_generic::SealedGeneric;
#[derive(SealedGeneric)]
#[define(
sealed,
types(ty = i32),
)]
pub struct SomeGeneric<T: SealedSomeGeneric> {
value: T
}
fn main() {
SomeGeneric<i32>{ ty: 0_i32 };
// OK
SomeGeneric<String>{ ty: "".into() };
// Compiler Error >> `String` does not implement `SealedSomeGeneric`
}
[T; 2] doesnt work yet)