onlytypes 0.1.4

A library for creating types that can only be created by a certain function
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented3 out of 3 items with examples
  • Size
  • Source code size: 8.92 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 290.91 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 4s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • matveitolochko

Only Types, for Rust

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.

Example

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())
    }
}

Changelog