Determine if a type implements a logical trait expression?.
This library defines the [impls!
], a macro?
that returns a [bool
] indicating whether a type implements a boolean-like
expression over a set of traits?.
rust
assert!(impls!(String: Clone & !Copy & Send & Sync));
See examples for detailed use cases.
This crate is available on crates.io and can be used by adding the
following to your project's [Cargo.toml
]:
toml
[dependencies]
impls = "1"
and this to your crate root (main.rs
or lib.rs
):
```rust
extern crate impls; ```
When using Rust 2018 edition, the following import can help if
having #[macro_use]
is undesirable.
rust
use impls::impls;
This documentation uses jargon that may be new to inexperienced Rust users. This section exists to make these terms easier to understand. Feel free to skip this section if these are already familiar to you.
In Rust, macros are functions over the abstract syntax tree (AST). They map input tokens to output tokens by performing some operation over them through a set of rules. Because of this, only their outputs are ever type-checked.
If you wish to learn about implementing macros, I recommend: - The Little Book of Rust Macros - "Macros" - The Rust Programming Language - "Macros" - The Rust Reference - "Macros By Example" - The Rust Reference
To use this crate, you do not need to know how macros are defined.
In Rust, traits are a way of defining a generalized property. They should be
thought of expressing what a type is capable of doing. For example: if a
type implements [Into
] for some type T
, then we know it can be converted
into T
by just calling the .into()
method on it.
If you wish to learn about traits in detail, I recommend: - "Traits: Defining Shared Behavior" - The Rust Programming Language - "Traits" - The Rust Reference
In this crate, traits should be thought of as [bool
]s where the condition
is whether the given type implements the trait or not.
An expression can be formed from these trait operations:
And (&
): also known as [logical disjunction], this returns true
if
both operands are true
. This is usually defined in Rust via the
[BitAnd
] trait.
Or (|
): also known as [logical conjunction], this returns true
if
either of two operands is true
. This is usually defined in Rust via
the [BitOr
] trait.
Exclusive-or (^
): also known as [exclusive disjunction], this returns
true
if only one of two operands is true
. This is usually defined
in Rust via the [BitXor
] trait.
Not (!
): a negation that returns false
if the operand is true
, or
true
if the operand is false
. This is usually defined in Rust via the
[Not
] trait.
See "Precedence and Nesting" for information about the order in which these operations are performed.
This macro works in every type context. See below for use cases.
Because types are [compile-time] constructs, the result of this macro can be
used as a const
value:
rust
const IMPLS: bool = impls!(u8: From<u32>);
Using [static_assertions
], we can fail to compile if the trait expression
evaluates to false
:
rust
const_assert!(impls!(*const u8: Send | Sync));
Trait operations abide by Rust's expression precedence. To define a custom order of operations (e.g. left-to-right), simply nest the expressions with parentheses.
```rust
let pre = impls!(u64: From
asserteq!(pre, true | true ^ true & true); assertne!(pre, ltr); ```
Because exclusive-or (^
) is a trait operation, we can check that a type
implements one of two traits, but not both:
```rust struct T;
trait Foo {} trait Bar {}
impl Foo for T {}
assert!(impls!(T: Foo ^ Bar)); ```
Something that's surprising to many Rust users is that [&mut T
] does not
implement [Copy
] nor [Clone
]:
rust
assert!(impls!(&mut u32: !Copy & !Clone));
Surely you're thinking now that this macro must be broken, because you've
been able to reuse &mut T
throughout your lifetime with Rust. This works
because, in certain contexts, the compiler silently adds "re-borrows"
(&mut *ref
) with a shorter lifetime and shadows the original. In reality,
&mut T
is a move-only type.
There's a variety of types in Rust that don't implement [Sized
]:
```rust // Slices store their size with their pointer. assert!(impls!(str: !Sized)); assert!(impls!([u8]: !Sized));
// Trait objects store their size in a vtable. trait Foo {} assert!(impls!(dyn Foo: !Sized));
// Wrappers around unsized types are also unsized themselves. struct Bar([u8]); assert!(impls!(Bar: !Sized)); ```
When called from a generic function, the returned value is based on the constraints of the generic type:
```rust use std::cell::Cell;
struct Value
impl
Keep in mind that this can result in false negatives:
```rust
const fn is_copy
assertne!(iscopy::
Traits with lifetimes are also supported:
```rust trait Ref<'a> {} impl<'a, T: ?Sized> Ref<'a> for &'a T {} impl<'a, T: ?Sized> Ref<'a> for &'a mut T {}
assert!(impls!(&'static str: Ref<'static>)); assert!(impls!(&'static mut [u8]: Ref<'static>)); assert!(impls!(String: !Ref<'static>)); ```
Nikolai Vazquez (GitHub: @nvzqz, Twitter: @NikolaiVazquez)
Implemented the impls!
macro with support for all logical operators and
without the limitations of the initial does_impl!
macro by Nadrieril.
Nadrieril Feneanar (GitHub: @Nadrieril)
Implemented the initial does_impl!
macro in
nvzqz/static-assertions-rs#28
upon which this crate was originally based.
This project is released under either:
at your choosing.