Crates.io | onlytypes |
lib.rs | onlytypes |
version | 0.1.4 |
source | src |
created_at | 2023-10-20 20:10:39.80802 |
updated_at | 2023-10-21 20:08:29.340681 |
description | A library for creating types that can only be created by a certain function |
homepage | |
repository | https://github.com/AtomicGamer9523/onlytypes-rs |
max_upload_size | |
id | 1009349 |
size | 8,925 |
Allows you to ignore impl blocks, or stub functions. If all you need is compile-time type checking, this is for you. Or if you don't want to ship a lot of bloat to your users, this is also for you.
use std::fmt;
pub trait MyTrait {
type Output;
fn my_trait_function(&self) -> Self::Output;
fn some_other_trait_function(&self) -> Self::Output;
}
pub struct MyStruct {
pub key: String,
pub value: String
}
#[onlytypes::ignore] // Completely ignores this impl block
impl MyStruct {
pub fn new(key: String, value: String) -> Self {
Self { key, value }
}
pub fn key(&self) -> &str {
self.key.as_str()
}
pub fn value(&self) -> &str {
self.value.as_str()
}
pub fn to_json(&self) -> String {
format!("{{\"key\": \"{}\", \"value\": \"{}\"}}", self.key(), self.value())
}
}
impl fmt::Debug for MyStruct {
#[onlytypes::stub]// Will stub this function (but still compile)
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PublicStruct")
.field("key", &self.key())
.field("value", &self.value())
.finish()
}
}
#[onlytypes::stub]// Will stub the entire impl block (but still compile)
impl MyTrait for MyStruct {
type Output = String;
fn my_trait_function(&self) -> Self::Output {
format!("{}: {}", self.key(), self.value())
}
fn some_other_trait_function(&self) -> Self::Output {
format!("{}: {}", self.key(), self.value())
}
}