Attribute macro that generates negated versions of`is_something` functions

Overview

negate

negate is a simple attribute macro that negates a given function.

Usage

#[negate]

Given a function of the form is_* that returns a boolean value, the macro will create a is_not_* function that negates the given function.

struct Word(&'static str);

impl Word {
    pub fn new(word: &'static str) -> Self {
        Self (word)
    }

    #[negate] // <- negate will implement a `is_not_uppercase` function!
    pub fn is_uppercase(&self) -> bool {
        self.0 == self.0.to_uppercase()
    }
}
let my_name = Word::new("My Name");

assert!(my_name.is_not_uppercase());

#[negate(name = "...")]

Using the name attribute allows you to set the name of the generated function. This also allows the usage of the [negate] macro with functions that do not start with is_.

use negate::negate;

pub enum TaskState {
    Ready,
    Finished,
}

pub struct Reactor {
    tasks: HashMap<usize, TaskState>,
}

impl Reactor {
    // Generates the `is_finished` function
    #[negate(name = "is_finished")]
    pub fn is_ready(&self, id: usize) -> bool {
        self.tasks.get(&id).map(|state| match state {
            TaskState::Ready => true,
            _ => false,
        }).unwrap_or(false)
    }
}

#[negate(docs = "...")]

Using the docs attribute allows you to customize the doc-string of the generated function.

use negate::negate;
#[negate(name = "is_odd", docs = "returns true if the given number is odd")]
fn is_even(x: i32) -> bool {
   x % 2 == 0
}
assert!(is_odd(5));

How does the generated code look like?

Non-associated functions

#[negate]
pub fn is_even(x: i32) -> bool {
    x % 2 == 0
}

Will expand to:

pub fn is_even(x: i32) -> bool {
    x % 2 == 0
}

/// This is an automatically generated function that denies [`is_even`].
/// Consult the original function for more information.
pub fn is_not_even(x: i32) -> bool {
    !is_even(x)
}

Using generics is likewise not a problem

#[negate]
fn is_equal<T>(x: T, y: T) -> bool
where
    T: Eq,
{
    x == y
}

The generated negated function:

/// This is an automatically generated function that denies [`is_equal`].
/// Consult the original function for more information.
fn is_not_equal<T>(x: T, y: T) -> bool
where
    T: Eq,
{
    !is_equal(x, y)
}

Associated functions

struct BigInt {
    ..
};

impl BigInt {
    #[negate(name = "is_negative", docs = "Returns true if the number is negative and false if the number is zero or positive.")]
    pub fn is_positive(&self) -> bool { .. }
}

Becomes:

impl BigInt {
    pub fn is_positive(&self) -> bool { .. }
    
    /// Returns true if the number is negative and false if the number is zero or positive.
    pub fn is_negative(&self) -> bool {
        !self.is_positive()
    }
}
You might also like...
A Rust proc-macro crate which derives functions to compile and parse back enums and structs to and from a bytecode representation

Bytecode A simple way to derive bytecode for you Enums and Structs. What is this This is a crate that provides a proc macro which will derive bytecode

proc-macro to help with using surrealdb's custom functions

SurrealDB Functions This is a proc-macro crate that given a path to a .surql file or a folder of .surql files, will parse DEFINE FUNCTION fn::s inside

The [cain!] macro is a macro that rewrites sequential Rust branch statements into nested branches

Note! This crate is experimental and under development. It may include bugs that alter the behavior of your code in unexpected ways. You should review

Simple rust asset handling derive macro for enums, and a proc-macro learning resource!

asset-derive Summary • Todos • Docs Summary Simple Rust asset loading derive macro for Enums, and a resource for learning proc-macros! Please feel fre

rabe is an Attribute Based Encryption library, written in Rust

Rabe rabe is a rust library implementing several Attribute Based Encryption (ABE) schemes using a modified version of the bn library of zcash (type-3

Extended attribute library for rust.

xattr A small library for setting, getting, and listing extended attributes. Supported Platforms: Linux, MacOS, FreeBSD, and NetBSD. API Documentation

Attribute for defining `macro_rules!` macros with proper visibility and scoping

macro-vis This crate provides an attribute for defining macro_rules! macros that have proper visibility and scoping. The default scoping and publicity

Attribute-Level Caching in Heterogeneous In-Memory DBMS

Alchemy Attribute-Level Caching in Heterogeneous In-Memory DBMS Alchemy is a DRAM-PM hybrid database engine built from scratch to achieve high perform

Rust Attribute-Based Encryption library rabe's C FFI binding , support CP-ABE and KP-ABE encrypt and decrypt, submodule of Rabe.Core c# library.

Rabe-ffi Rust Attribute-Based Encryption library rabe's C FFI binding , support CP-ABE and KP-ABE encrypt and decrypt, submodule of Rabe.Core c# libra

Solving context limits when working with AI LLM models by implementing a "chunkable" attribute on your prompt structs.

Promptize Promptize attempts to solve the issues with context limits when working with AI systems. It allows a user to add an attribute to their struc

Docker images for compiling static Rust binaries using musl-libc and musl-gcc, with static versions of useful C libraries. Supports openssl and diesel crates.

rust-musl-builder: Docker container for easily building static Rust binaries Source on GitHub Changelog UPDATED: Major updates in this release which m

Cassandra DB native client written in Rust language. Find 1.x versions on https://github.com/AlexPikalov/cdrs/tree/v.1.x Looking for an async version? - Check WIP https://github.com/AlexPikalov/cdrs-async
Cassandra DB native client written in Rust language. Find 1.x versions on https://github.com/AlexPikalov/cdrs/tree/v.1.x Looking for an async version? - Check WIP https://github.com/AlexPikalov/cdrs-async

CDRS CDRS is looking for maintainers CDRS is Apache Cassandra driver written in pure Rust. 💡 Looking for an async version? async-std https://github.c

sublingual: toy versions of existing programming languages

sublingual: toy versions of existing programming languages This is a collection of toy languages created by taking much "larger" languages (e.g. Rust)

Redirect Deno dependencies from semantic versions to the newest fitting version on deno.land/x

Deno Semver Redirect Redirect Deno dependencies from semantic versions to the newest fitting version on deno.land/x. See also this Deno Issue. How to

Nvm - Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

Node Version Manager Table of Contents Intro About Installing and Updating Install & Update Script Additional Notes Troubleshooting on Linux Troublesh

A compatibility layer to smooth the transition between different versions of embedded-hal

Embedded HAL Compatibility Layer A compatibility layer to smooth the transition between different versions of embedded-hal (specifically 0.2.x and 1.0

Library that implements different versions of PPMD algorithm (compress and decompress)

ppmd-rs Library that implements different versions of PPMD algorithm (compress and decompress) Dependencies Rust 1.58 or newer Cargo How to build Clon

Quinine is a Rust library that implements atomic, lock-free, but write-once versions of containers like `Box` or `Arc`

Quinine is a Rust library that implements atomic, lock-free, but write-once versions of containers like `Box` or `Arc`

httm prints the size, date and corresponding locations of available unique versions of files residing on ZFS snapshots

httm prints the size, date and corresponding locations of available unique versions of files residing on ZFS snapshots, as well as allowing their interactive viewing and restoration.

Owner
Vinícius Miguel
barely passed the Turing test
Vinícius Miguel
Download .crate files of all versions of all crates from crates.io

get-all-crates Download .crate files of all versions of all crates from crates.io. Useful for things like noisy-clippy which need to analyze the sourc

David Tolnay 18 Jan 3, 2023
Rust Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.

Rust Embed Rust Custom Derive Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev. Y

Peter 1k Jan 5, 2023
Rust I18n is use Rust codegen for load YAML file storage translations on compile time, and give you a t! macro for simply get translation texts.

Rust I18n Rust I18n is use Rust codegen for load YAML file storage translations on compile time, and give you a t! macro for simply get translation te

Longbridge 73 Dec 27, 2022
lispr is a Rust macro that tries to implement a small subset of LISPs syntax in Rust

lispr lispr is a Rust macro that tries to implement a small subset of LISPs syntax in Rust. It is neither especially beautiful or efficient since it i

Jan Vaorin 0 Feb 4, 2022
A Rust attribute macro to limit a function's number of runs over a specified period of time

throttle_my_fn: A Rust attribute macro to throttle the execution of functions throttle_my_fn is a Rust attribute macro to limit a function's number of

Fred Morcos 8 Dec 3, 2022
A Rust attribute macro that adds memoization to a function (rhymes with Mickey)

michie (sounds like Mickey) — an attribute macro that adds memoization to a function. Table of contents Features Non-features key_expr key_type store_

Mobus Operandi 16 Dec 20, 2022
An attribute macro to simplify writing simple command line applications.

fncli An attribute macro to simplify writing simple command line applications. Example #[fncli::cli] fn main(a: i32, b: i32) { println!("{}", a +

Vidhan Bhatt 29 Dec 15, 2022
Attribute macro for implementing methods on both Foo and ArchivedFoo.

rkyv_impl Implement methods for Foo and ArchivedFoo in a single impl block. use rkyv::Archive; use rkyv_impl::*; use std::iter::Sum; #[derive(Archive

Duncan 5 Aug 6, 2023
ChatGPT powered Rust proc macro that generates code at compile-time.

gpt-macro ChatGPT powered Rust proc macro that generates code at compile-time. Implemented Macros auto_impl!{} #[auto_test(...)] Usage Get ChatGPT API

Akira Moroo 429 Apr 15, 2023
Const equivalents of many [`bytemuck`] functions, and a few additional const functions.

Const equivalents of many bytemuck functions, and a few additional const functions. constmuck uses bytemuck's traits, so any type that implements thos

null 6 Nov 4, 2021