https://github.com/taiki-e/negative-impl
Negative trait implementations on stable Rust.
https://github.com/taiki-e/negative-impl
no-std proc-macro rust
Last synced: 4 months ago
JSON representation
Negative trait implementations on stable Rust.
- Host: GitHub
- URL: https://github.com/taiki-e/negative-impl
- Owner: taiki-e
- License: apache-2.0
- Created: 2020-10-16T14:46:02.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-04-24T16:09:02.000Z (almost 2 years ago)
- Last Synced: 2024-05-01T23:58:17.248Z (almost 2 years ago)
- Topics: no-std, proc-macro, rust
- Language: Shell
- Homepage: https://docs.rs/negative-impl
- Size: 174 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# negative-impl
[](https://crates.io/crates/negative-impl)
[](https://docs.rs/negative-impl)
[](#license)
[](https://www.rust-lang.org)
[](https://github.com/taiki-e/negative-impl/actions)
Negative trait implementations on stable Rust.
This crate emulates the [unstable `negative_impls` feature](https://doc.rust-lang.org/nightly/unstable-book/language-features/negative-impls.html)
by [generating a trait implementation with a condition that will never be true](https://github.com/taiki-e/negative-impl/issues/6#issuecomment-1669714453).
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
negative-impl = "0.1"
```
## Examples
```rust
use negative_impl::negative_impl;
pub struct Type {}
#[negative_impl]
impl !Send for Type {}
#[negative_impl]
impl !Sync for Type {}
```
## Supported traits
Currently this crate only supports [auto traits](https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits).
- [`Send`](https://doc.rust-lang.org/std/marker/trait.Send.html)
- [`Sync`](https://doc.rust-lang.org/std/marker/trait.Sync.html)
- [`Unpin`](https://doc.rust-lang.org/std/marker/trait.Unpin.html)
- [`UnwindSafe`](https://doc.rust-lang.org/std/panic/trait.UnwindSafe.html)
- [`RefUnwindSafe`](https://doc.rust-lang.org/std/panic/trait.RefUnwindSafe.html)
## Limitations
### Conflicting implementations
The following code cannot compile due to `impl Trait for T` and
`impl Trait for Type` conflict.
```rust
use negative_impl::negative_impl;
pub struct Type {}
#[negative_impl]
impl !Send for Type {}
trait Trait {}
impl Trait for T {}
impl Trait for Type {}
```
```text
error[E0119]: conflicting implementations of trait `Trait` for type `Type`:
--> src/lib.rs:60:1
|
14 | impl Trait for T {}
| ------------------------- first implementation here
15 | impl Trait for Type {}
| ^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Type`
```
The above code can be compiled using the unstable `negative_impls` feature.
```rust
#![feature(negative_impls)]
pub struct Type {}
impl !Send for Type {}
trait Trait {}
impl Trait for T {}
impl Trait for Type {}
```
## License
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
[MIT license](LICENSE-MIT) at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.