static_assertions-0.3.3/CHANGELOG.md010064400007650000024000000101661350023702600153100ustar0000000000000000# Changelog [![Crates.io][crate-badge]][crate] All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog] and this project adheres to [Semantic Versioning]. ## [Unreleased] ## [0.3.3] - 2019-06-12 ### Added - `const_assert_ne!` macro counterpart to `const_assert_eq!` ### Fixed - `assert_eq_type!` would pass when types can coerce via `Deref`, such as with `str` and `String` ## [0.3.2] - 2019-05-15 ### Added - A `assert_eq_type!` macro that allows for checking whether inputs are the same concrete type - A `assert_ne_type!` macro for checking whether inputs all refer to different types ### Fixed - `const_assert!` now only takes `bool` values whereas integer (or other type) values could previously be passed ## [0.3.1] - 2018-11-15 ### Fixed - Macros that refer to other internal macros can now be imported when compiling for Rust 2018 ([issue #10](https://github.com/nvzqz/static-assertions-rs/issues/10)) ## [0.3.0] - 2018-11-14 ### Changed - Bumped minimum supported (automatically tested) Rust version to 1.24.0 - Moved message parameter for `assert_cfg!()` to last argument position, making it consistent with other macros ### Removed - No need to use `macro!(label; ...)` syntax when compiling on nightly Rust and enabling the `nightly` feature flag ## [0.2.5] - 2017-12-12 ### Changed - `assert_eq_size_ptr` wraps its code inside of a closure, ensuring that the unsafe code inside never runs - Clippy no longer warns about `unneeded_field_pattern` within `assert_fields` ### Added - Much better documentation with test examples that are guaranteed to fail at compile-time ### Removed - Removed testing features; compile failure tests are now done via doc tests ## [0.2.4] - 2017-12-11 ### Removed - Removed the actual call to `mem::transmute` while still utilizing it for size verification ([Simon Sapin], [#5]) ### Added - `assert_cfg` macro that asserts that the given configuration is set - `assert_fields` macro to assert that a struct type or enum variant has a given field ### Fixed - Allow more generics flexibility in `assert_impl` ## [0.2.3] - 2017-08-24 ### Fixed - Trailing commas are now allowed ### Removed - Removed clippy warnings ## [0.2.2] - 2017-08-13 ### Added - Added `assert_impl` macro to ensure a type implements a given set of traits ## [0.2.1] - 2017-08-13 ### Added - Added `assert_obj_safe` macro for ensuring that a trait is object-safe ## [0.2.0] - 2017-08-12 ### Added - Added `assert_eq_size_ptr` macro ### Fixed - Allow `assert_eq_size`, `const_assert`, and `const_assert_eq` in non-function contexts via providing a unique label [#1] ### Removed - **[Breaking]** Semicolon-separated `assert_eq_size` is no longer allowed ## [0.1.1] - 2017-08-12 ### Added - Added `const_assert_eq` macro ## 0.1.0 - 2017-08-12 Initial release [Simon Sapin]: https://github.com/SimonSapin [#1]: https://github.com/nvzqz/static-assertions-rs/issues/1 [#5]: https://github.com/nvzqz/static-assertions-rs/pull/5 [crate]: https://crates.io/crates/static_assertions [crate-badge]: https://img.shields.io/crates/v/static_assertions.svg [Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ [Semantic Versioning]: http://semver.org/spec/v2.0.0.html [Unreleased]: https://github.com/nvzqz/static-assertions-rs/compare/v0.3.3...HEAD [0.3.3]: https://github.com/nvzqz/static-assertions-rs/compare/v0.3.2...v0.3.3 [0.3.2]: https://github.com/nvzqz/static-assertions-rs/compare/v0.3.1...v0.3.2 [0.3.1]: https://github.com/nvzqz/static-assertions-rs/compare/v0.3.0...v0.3.1 [0.3.0]: https://github.com/nvzqz/static-assertions-rs/compare/v0.2.5...v0.3.0 [0.2.5]: https://github.com/nvzqz/static-assertions-rs/compare/v0.2.4...v0.2.5 [0.2.4]: https://github.com/nvzqz/static-assertions-rs/compare/v0.2.3...v0.2.4 [0.2.3]: https://github.com/nvzqz/static-assertions-rs/compare/v0.2.2...v0.2.3 [0.2.2]: https://github.com/nvzqz/static-assertions-rs/compare/v0.2.1...v0.2.2 [0.2.1]: https://github.com/nvzqz/static-assertions-rs/compare/v0.2.0...v0.2.1 [0.2.0]: https://github.com/nvzqz/static-assertions-rs/compare/v0.1.1...v0.2.0 [0.1.1]: https://github.com/nvzqz/static-assertions-rs/compare/v0.1.0...v0.1.1 static_assertions-0.3.3/Cargo.toml.orig010064400007650000024000000015561350023676500164020ustar0000000000000000[package] name = "static_assertions" version = "0.3.3" authors = ["Nikolai Vazquez"] license = "MIT/Apache-2.0" readme = "README.md" homepage = "https://github.com/nvzqz/static-assertions-rs" repository = "https://github.com/nvzqz/static-assertions-rs" documentation = "https://docs.rs/static_assertions/" categories = ["no-std", "rust-patterns", "development-tools::testing"] keywords = ["assert", "static", "testing"] description = "Compile-time assertions to ensure that invariants are met." include = ["Cargo.toml", "src/**/*.rs", "README.md", "CHANGELOG.md", "LICENSE*"] [badges] travis-ci = { repository = "nvzqz/static-assertions-rs" } is-it-maintained-open-issues = { repository = "nvzqz/static-assertions-rs" } is-it-maintained-issue-resolution = { repository = "nvzqz/static-assertions-rs" } maintenance = { status = "passively-maintained" } [features] nightly = [] static_assertions-0.3.3/Cargo.toml0000644000000026000000000000000126330ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "static_assertions" version = "0.3.3" authors = ["Nikolai Vazquez"] include = ["Cargo.toml", "src/**/*.rs", "README.md", "CHANGELOG.md", "LICENSE*"] description = "Compile-time assertions to ensure that invariants are met." homepage = "https://github.com/nvzqz/static-assertions-rs" documentation = "https://docs.rs/static_assertions/" readme = "README.md" keywords = ["assert", "static", "testing"] categories = ["no-std", "rust-patterns", "development-tools::testing"] license = "MIT/Apache-2.0" repository = "https://github.com/nvzqz/static-assertions-rs" [features] nightly = [] [badges.is-it-maintained-issue-resolution] repository = "nvzqz/static-assertions-rs" [badges.is-it-maintained-open-issues] repository = "nvzqz/static-assertions-rs" [badges.maintenance] status = "passively-maintained" [badges.travis-ci] repository = "nvzqz/static-assertions-rs" static_assertions-0.3.3/LICENSE-APACHE010064400007650000024000000261361314364153400154350ustar0000000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. static_assertions-0.3.3/LICENSE-MIT010064400007650000024000000020601314364153400151330ustar0000000000000000MIT License Copyright (c) 2017 Nikolai Vazquez Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. static_assertions-0.3.3/README.md010064400007650000024000000064751350023676500147770ustar0000000000000000[![Banner](https://raw.githubusercontent.com/nvzqz/static-assertions-rs/assets/Banner.png)](https://github.com/nvzqz/static-assertions-rs)
Crates.io Downloads Build Status rustc ^1.24.0
Become a Patron! Buy me a coffee
Rust compile-time assertions to ensure that invariants are met. [Documentation](https://docs.rs/static_assertions/) ## Installation This crate is available [on crates.io](https://crates.io/crates/static_assertions) and can be used by adding the following to your project's [`Cargo.toml`](https://doc.rust-lang.org/cargo/reference/manifest.html): ```toml [dependencies] static_assertions = "0.3.3" ``` and this to your crate root (`main.rs` or `lib.rs`): ```rust #[macro_use] extern crate static_assertions; ``` ## Usage This crate exposes the following macros: - [`assert_cfg!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.assert_cfg.html) - [`assert_eq_size!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.assert_eq_size.html) - [`assert_eq_size_ptr!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.assert_eq_size_ptr.html) - [`assert_eq_size_val!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.assert_eq_size_val.html) - [`assert_eq_type!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.assert_eq_type.html) - [`assert_ne_type!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.assert_ne_type.html) - [`assert_fields!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.assert_fields.html) - [`assert_impl!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.assert_impl.html) - [`assert_obj_safe!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.assert_obj_safe.html) - [`const_assert!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.const_assert.html) - [`const_assert_eq!`](https://docs.rs/static_assertions/0.3.3/static_assertions/macro.const_assert_eq.html) ### Limitations See [issue #1](https://github.com/nvzqz/static-assertions-rs/issues/1) to read up on current limitations of this crate and how to currently overcome them. ## Changes See [`CHANGELOG.md`](https://github.com/nvzqz/static-assertions-rs/blob/master/CHANGELOG.md) for a complete list of what has changed from one version to another. ## License This project is released under either: - [MIT License](https://github.com/nvzqz/static-assertions-rs/blob/master/LICENSE-MIT) - [Apache License (Version 2.0)](https://github.com/nvzqz/static-assertions-rs/blob/master/LICENSE-APACHE) at your choosing. static_assertions-0.3.3/src/assert_cfg.rs010064400007650000024000000032211337305076600167620ustar0000000000000000/// Asserts that a given configuration is set. /// /// # Examples /// /// A project will simply fail to compile if the given configuration is not set. /// /// ``` /// # #[macro_use] extern crate static_assertions; /// # fn main() {} /// // We're not masochists /// # #[cfg(not(target_pointer_width = "16"))] // Just in case /// assert_cfg!(not(target_pointer_width = "16")); /// ``` /// /// If a project does not support a set of configurations, you may want to /// report why. There is the option of providing a compile error message string: /// /// ``` /// # #[macro_use] extern crate static_assertions; /// # fn main() {} /// # #[cfg(any(unix, linux))] /// assert_cfg!(any(unix, linux), "There is only support for Unix or Linux"); /// /// // User needs to specify a database back-end /// # #[cfg(target_pointer_width = "0")] // Impossible /// assert_cfg!(all(not(all(feature = "mysql", feature = "mongodb")), /// any( feature = "mysql", feature = "mongodb")), /// "Must exclusively use MySQL or MongoDB as database back-end"); /// ``` /// /// Some configurations are impossible. For example, we can't be compiling for /// both Unix _and_ Windows simultaneously: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// assert_cfg!(all(unix, windows), "No, that's not how it works! ಠ_ಠ"); /// # } /// ``` #[macro_export] macro_rules! assert_cfg { () => {}; ($($cfg:meta)+, $msg:expr) => { #[cfg(not($($cfg)*))] compile_error!($msg); }; ($($cfg:tt)*) => { #[cfg(not($($cfg)*))] compile_error!(concat!("Cfg does not pass: ", stringify!($($cfg)*))); }; } static_assertions-0.3.3/src/assert_eq_size.rs010064400007650000024000000110361350023334100176450ustar0000000000000000/// Asserts that types are equal in size. /// /// When performing operations such as pointer casts or dealing with [`usize`] /// versus [`u64`] versus [`u32`], the size of your types matter. That is where /// this macro comes into play. /// /// # Alternatives /// /// There also exists [`assert_eq_size_val`](macro.assert_eq_size_val.html) and /// [`assert_eq_size_ptr`](macro.assert_eq_size_ptr.html). Instead of specifying /// types to compare, values' sizes can be directly compared against each other. /// /// # Examples /// /// On stable Rust, using the macro requires a unique “label” when used in a /// module scope: /// #[cfg_attr(feature = "nightly", doc = "```ignore")] #[cfg_attr(not(feature = "nightly"), doc = "```")] /// # #[macro_use] extern crate static_assertions; /// # fn main() {} /// assert_eq_size!(bytes; (u8, u8), u16); /// ``` /// /// The [labeling limitation](index.html#limitations) is not necessary if /// compiling on nightly Rust with the `nightly` feature enabled: /// #[cfg_attr(feature = "nightly", doc = "```")] #[cfg_attr(not(feature = "nightly"), doc = "```ignore")] /// #![feature(underscore_const_names)] /// # #[macro_use] extern crate static_assertions; /// /// assert_eq_size!(u32, [u16; 2]); /// /// fn main() { /// // Supports unlimited arguments without hitting recursion limit /// assert_eq_size!([u8; 4], (u16, u16), u32); /// } /// ``` /// /// The following produces a compilation failure because `u32` has 4 times the /// size of `u8`: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// assert_eq_size!(u32, u8); /// # } /// ``` /// /// [`usize`]: https://doc.rust-lang.org/std/primitive.usize.html /// [`u64`]: https://doc.rust-lang.org/std/primitive.u64.html /// [`u32`]: https://doc.rust-lang.org/std/primitive.u32.html #[macro_export(local_inner_macros)] macro_rules! assert_eq_size { ($($xs:tt)+) => { _assert_eq_size!($($xs)+); }; } #[doc(hidden)] #[cfg(feature = "nightly")] #[macro_export(local_inner_macros)] macro_rules! _assert_eq_size { ($x:ty, $($xs:ty),+ $(,)*) => { const _: fn() -> () = || { $(let _ = $crate::_core::mem::transmute::<$x, $xs>;)+ }; }; } #[doc(hidden)] #[cfg(not(feature = "nightly"))] #[macro_export(local_inner_macros)] macro_rules! _assert_eq_size { ($x:ty, $($xs:ty),+ $(,)*) => { $(let _ = $crate::_core::mem::transmute::<$x, $xs>;)+ }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_eq_size!($($xs)+); } }; } /// Asserts that values pointed to are equal in size. /// /// This especially is useful for when coercing pointers between different types /// and ensuring the underlying values are the same size. /// /// # Examples /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// fn operation(x: &(u32, u32), y: &[u16; 4]) { /// assert_eq_size_ptr!(x, y); /// } /// # fn main() {} /// ``` /// /// Byte arrays of different lengths have different sizes: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// static BYTES: &[u8; 4] = &[ /// /* ... */ /// # 0; 4 /// ]; /// /// static TABLE: &[u8; 16] = &[ /// /* ... */ /// # 0; 16 /// ]; /// /// assert_eq_size_ptr!(BYTES, TABLE); /// # } /// ``` #[macro_export] macro_rules! assert_eq_size_ptr { ($x:expr, $($xs:expr),+ $(,)*) => { #[allow(unknown_lints, unsafe_code, forget_copy, useless_transmute)] let _ = || unsafe { use $crate::_core::{mem, ptr}; let mut copy = ptr::read($x); $(ptr::write(&mut copy, mem::transmute(ptr::read($xs)));)+ mem::forget(copy); }; } } /// Asserts that values are equal in size. /// /// This macro doesn't consume its arguments and thus works for /// non-[`Clone`]able values. /// /// # Examples /// /// ``` /// # #[macro_use] /// # extern crate static_assertions; /// # fn main() { /// struct Byte(u8); /// /// let x = 10u8; /// let y = Byte(42); // Works for non-cloneable types /// /// assert_eq_size_val!(x, y); /// assert_eq_size_val!(x, y, 0u8); /// # } /// ``` /// /// Even though both values are 0, they are of types with different sizes: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// assert_eq_size_val!(0u8, 0u32); /// # } /// ``` /// /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[macro_export(local_inner_macros)] macro_rules! assert_eq_size_val { ($x:expr, $($xs:expr),+ $(,)*) => { assert_eq_size_ptr!(&$x, $(&$xs),+); } } static_assertions-0.3.3/src/assert_eq_type.rs010064400007650000024000000036071350023602700176650ustar0000000000000000/// Asserts that types are equal. /// /// # Examples /// /// On stable Rust, using the macro requires a unique “label” when used in a /// module scope: /// #[cfg_attr(feature = "nightly", doc = "```ignore")] #[cfg_attr(not(feature = "nightly"), doc = "```")] /// # #[macro_use] extern crate static_assertions; /// # fn main() {} /// type A = u8; /// type B = A; /// /// assert_eq_type!(byte; u8, A, B); /// ``` /// /// The [labeling limitation](index.html#limitations) is not necessary if /// compiling on nightly Rust with the `nightly` feature enabled: /// #[cfg_attr(feature = "nightly", doc = "```")] #[cfg_attr(not(feature = "nightly"), doc = "```ignore")] /// #![feature(underscore_const_names)] /// # #[macro_use] extern crate static_assertions; /// # type A = u8; /// # type B = A; /// /// assert_eq_type!(u8, A, B); /// ``` /// /// The following produces a compilation failure because `String` and `str` do /// not refer to the same type: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// assert_eq_type!(String, str); /// # } /// ``` #[macro_export(local_inner_macros)] macro_rules! assert_eq_type { ($($xs:tt)+) => { _assert_eq_type!($($xs)+); }; } #[doc(hidden)] #[cfg(feature = "nightly")] #[macro_export(local_inner_macros)] macro_rules! _assert_eq_type { ($x:ty, $($xs:ty),+ $(,)*) => { const _: fn() = || { $( { struct Type(A); fn assert_eq_type(x: &Type<$x>) -> &Type<$xs> { x } } )+ }; }; } #[doc(hidden)] #[cfg(not(feature = "nightly"))] #[macro_export(local_inner_macros)] macro_rules! _assert_eq_type { ($x:ty, $($xs:ty),+ $(,)*) => { $( { struct Type(A); fn assert_eq_type(x: &Type<$x>) -> &Type<$xs> { x } } )+ }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_eq_type!($($xs)+); } }; } static_assertions-0.3.3/src/assert_fields.rs010064400007650000024000000043271350023334100174610ustar0000000000000000/// Asserts that the type has the given fields. /// /// # Examples /// /// One common use case is when types have fields defined multiple times as a /// result of `#[cfg]`. This can be an issue when exposing a public API. /// #[cfg_attr(feature = "nightly", doc = "```ignore")] #[cfg_attr(not(feature = "nightly"), doc = "```")] /// # #[macro_use] extern crate static_assertions; /// pub struct Ty { /// #[cfg(windows)] /// pub val1: u8, /// #[cfg(not(windows))] /// pub val1: usize, /// /// #[cfg(unix)] /// pub val2: u32, /// #[cfg(not(unix))] /// pub val2: usize, /// } /// /// // Requires a unique label in module scope /// assert_fields!(windows; Ty, val1); /// /// fn main() { /// // Always have `val2` regardless of OS /// assert_fields!(Ty, val2); /// } /// ``` /// /// The [labeling limitation](index.html#limitations) is not necessary if /// compiling on nightly Rust with the `nightly` feature enabled: /// #[cfg_attr(feature = "nightly", doc = "```")] #[cfg_attr(not(feature = "nightly"), doc = "```ignore")] /// #![feature(underscore_const_names)] /// # #[macro_use] extern crate static_assertions; /// /// use std::ops::Range; /// /// assert_fields!(Range, start, end); /// ``` /// /// Range does not have a field named `middle`: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// # use std::ops::Range; /// assert_fields!(Range, middle); /// # } /// ``` #[macro_export(local_inner_macros)] macro_rules! assert_fields { ($($xs:tt)+) => { _assert_fields!($($xs)+); }; } #[doc(hidden)] #[cfg(feature = "nightly")] #[macro_export(local_inner_macros)] macro_rules! _assert_fields { ($t:path, $($f:ident),+) => { #[allow(unknown_lints, unneeded_field_pattern)] const _: fn() -> () = || { $(let $t { $f: _, .. };)+ }; }; } #[doc(hidden)] #[cfg(not(feature = "nightly"))] #[macro_export(local_inner_macros)] macro_rules! _assert_fields { ($t:path, $($f:ident),+) => { #[allow(unknown_lints, unneeded_field_pattern)] { $(let $t { $f: _, .. };)+ } }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_fields!($($xs)+); } }; }static_assertions-0.3.3/src/assert_impl.rs010064400007650000024000000045061350023334100171530ustar0000000000000000/// Asserts that the type implements the given traits. /// /// This can be used to ensure types implement auto traits such as [`Send`] and /// [`Sync`], as well as traits with [blanket `impl`s][blanket]. /// /// # Examples /// /// On stable Rust, using the macro requires a unique “label” when used in a /// module scope: /// #[cfg_attr(feature = "nightly", doc = "```ignore")] #[cfg_attr(not(feature = "nightly"), doc = "```")] /// # #[macro_use] extern crate static_assertions; /// # fn main() {} /// assert_impl!(str; String, Send, Sync, From<&'static str>); /// assert_impl!(vec; &'static [u8], Into>); /// ``` /// /// The [labeling limitation](index.html#limitations) is not necessary if /// compiling on nightly Rust with the `nightly` feature enabled: /// #[cfg_attr(feature = "nightly", doc = "```")] #[cfg_attr(not(feature = "nightly"), doc = "```ignore")] /// #![feature(underscore_const_names)] /// # #[macro_use] extern crate static_assertions; /// /// assert_impl!(u32, Copy, Send); /// /// fn main() { /// assert_impl!(&str, Into); /// } /// ``` /// /// Raw pointers cannot be sent between threads safely: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// assert_impl!(*const u8, Send); /// # } /// ``` /// /// [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html /// [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html /// [blanket]: https://doc.rust-lang.org/book/second-edition/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods #[macro_export(local_inner_macros)] macro_rules! assert_impl { ($($xs:tt)+) => { _assert_impl!($($xs)+); }; } #[doc(hidden)] #[cfg(feature = "nightly")] #[macro_export(local_inner_macros)] macro_rules! _assert_impl { ($x:ty, $($t:path),+ $(,)*) => { const _: fn() -> () = || { fn assert_impl() where T: ?Sized $(+ $t)+ {} assert_impl::<$x>(); }; }; } #[doc(hidden)] #[cfg(not(feature = "nightly"))] #[macro_export(local_inner_macros)] macro_rules! _assert_impl { ($x:ty, $($t:path),+ $(,)*) => { { fn assert_impl() where T: ?Sized $(+ $t)+ {} assert_impl::<$x>(); } }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_impl!($($xs)+); } }; } static_assertions-0.3.3/src/assert_ne_type.rs010064400007650000024000000043611350023630600176600ustar0000000000000000/// Asserts that types are _not_ equal. /// /// # Examples /// /// On stable Rust, using the macro requires a unique “label” when used in a /// module scope: /// #[cfg_attr(feature = "nightly", doc = "```ignore")] #[cfg_attr(not(feature = "nightly"), doc = "```")] /// # #[macro_use] extern crate static_assertions; /// # fn main() {} /// assert_ne_type!(slices; [u8], [u16], str); /// ``` /// /// The [labeling limitation](index.html#limitations) is not necessary if /// compiling on nightly Rust with the `nightly` feature enabled: /// #[cfg_attr(feature = "nightly", doc = "```")] #[cfg_attr(not(feature = "nightly"), doc = "```ignore")] /// #![feature(underscore_const_names)] /// # #[macro_use] extern crate static_assertions; /// /// assert_ne_type!(u8, (u8,), [u8]); /// ``` /// /// The following produces a compilation failure because `c_uchar` is a type /// alias for `u8`: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// assert_ne_type!(std::os::raw::c_uchar, u8, u32); /// assert_ne_type!(std::os::raw::c_uchar, u32, u8); /// assert_ne_type!(u32, std::os::raw::c_uchar, u8); /// # } /// ``` #[macro_export(local_inner_macros)] macro_rules! assert_ne_type { ($($xs:tt)+) => { _assert_ne_type!($($xs)+); }; } #[doc(hidden)] #[cfg(feature = "nightly")] #[macro_export(local_inner_macros)] macro_rules! _assert_ne_type { ($x:ty, $y:ty $(,)*) => { const _: fn() = || { trait MutuallyExclusive {} impl MutuallyExclusive for $x {} impl MutuallyExclusive for $y {} }; }; ($x:ty, $y:ty, $($z:ty),+ $(,)*) => { _assert_ne_type!($x, $($z),+); _assert_ne_type!($y, $($z),+); }; } #[doc(hidden)] #[cfg(not(feature = "nightly"))] #[macro_export(local_inner_macros)] macro_rules! _assert_ne_type { ($x:ty, $y:ty $(,)*) => { { trait MutuallyExclusive {} impl MutuallyExclusive for $x {} impl MutuallyExclusive for $y {} } }; ($x:ty, $y:ty, $($z:ty),+ $(,)*) => { _assert_ne_type!($x, $($z),+); _assert_ne_type!($y, $($z),+); }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_ne_type!($($xs)+); } }; } static_assertions-0.3.3/src/assert_obj_safe.rs010064400007650000024000000055711350023334100177650ustar0000000000000000/// Asserts that the traits are [object-safe][object]. /// /// This is useful for when changes are made to a trait that accidentally /// prevent it from being used as an [object]. Such a case would be adding a /// generic method and forgetting to add `where Self: Sized` after it. If left /// unnoticed, that mistake will affect crate users and break both forward and /// backward compatibility. /// /// # Examples /// /// When exposing a public API, it's important that traits that could previously /// use dynamic dispatch can still do so in future compatible crate versions. /// #[cfg_attr(feature = "nightly", doc = "```ignore")] #[cfg_attr(not(feature = "nightly"), doc = "```")] /// # #[macro_use] extern crate static_assertions; /// // Requires a unique label in module scope /// assert_obj_safe!(basic; Send, Sync, AsRef); /// /// mod inner { /// // Works with traits that are not in the calling module /// pub trait BasicTrait { /// fn foo(&self); /// } /// } /// /// trait MySafeTrait { /// fn bar(&self) -> u32; /// } /// /// fn main() { /// assert_obj_safe!(MySafeTrait); /// assert_obj_safe!(inner::BasicTrait); /// } /// ``` /// /// The [labeling limitation](index.html#limitations) is not necessary if /// compiling on nightly Rust with the `nightly` feature enabled: /// #[cfg_attr(feature = "nightly", doc = "```")] #[cfg_attr(not(feature = "nightly"), doc = "```ignore")] /// #![feature(underscore_const_names)] /// # #[macro_use] extern crate static_assertions; /// /// use std::fmt; /// /// assert_obj_safe!(fmt::Write); /// /// fn main() { /// assert_obj_safe!(fmt::Debug); /// } /// ``` /// /// Raw pointers cannot be sent between threads safely: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// assert_impl!(*const u8, Send); /// # } /// ``` /// /// Generics without `where Self: Sized` are not allowed in /// [object-safe][object] trait methods: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// trait MyUnsafeTrait { /// fn baz(&self) -> T; /// } /// /// # fn main() { /// assert_obj_safe!(MyUnsafeTrait); /// # } /// ``` /// /// [object]: https://doc.rust-lang.org/book/2018-edition/ch17-02-trait-objects.html#object-safety-is-required-for-trait-objects #[macro_export(local_inner_macros)] macro_rules! assert_obj_safe { ($($xs:tt)+) => { _assert_obj_safe!($($xs)+); }; } #[doc(hidden)] #[cfg(feature = "nightly")] #[macro_export(local_inner_macros)] macro_rules! _assert_obj_safe { ($($xs:ty),+ $(,)*) => { $(const _: Option<&$xs> = None;)+ }; } #[doc(hidden)] #[cfg(not(feature = "nightly"))] #[macro_export(local_inner_macros)] macro_rules! _assert_obj_safe { ($($xs:ty),+ $(,)*) => { $(let _: &$xs;)+ }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { assert_obj_safe!($($xs)+); } }; } static_assertions-0.3.3/src/const_assert.rs010064400007650000024000000112761350023341700173460ustar0000000000000000/// Asserts that constant expressions evaluate to `true`. /// /// Constant expressions can be ensured to have certain properties via this /// macro If the expression evaluates to `false`, the file will fail to compile. /// This is synonymous to [`static_assert` in C++][static_assert]. /// /// # Alternatives /// /// There also exists [`const_assert_eq`](macro.const_assert_eq.html) for /// validating whether a sequence of expressions are equal to one another. /// /// # Examples /// /// Some expressions are blatantly false: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// const_assert!(1 >= 2); /// # } /// ``` /// /// Inputs are type-checked as booleans: /// /// ```compile_fail #[cfg_attr(feature = "nightly", doc = "#![feature(underscore_const_names)]")] /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// const_assert!(!0); /// # } /// ``` /// /// Despite this being a macro, we see this produces a type error: /// /// ```txt /// | const_assert!(!0); /// | ^^ expected bool, found integral variable /// | /// = note: expected type `bool` /// found type `{integer}` /// ``` /// /// On stable Rust, using the macro requires a unique “label” when used in a /// module scope: /// #[cfg_attr(feature = "nightly", doc = "```ignore")] #[cfg_attr(not(feature = "nightly"), doc = "```")] /// # #[macro_use] /// # extern crate static_assertions; /// # fn main() {} /// const_assert!(meaning_of_life; 42 == !!42); /// ``` /// /// The [labeling limitation](index.html#limitations) is not necessary if /// compiling on nightly Rust with the `nightly` feature enabled: /// #[cfg_attr(feature = "nightly", doc = "```")] #[cfg_attr(not(feature = "nightly"), doc = "```ignore")] /// #![feature(underscore_const_names)] /// # #[macro_use] extern crate static_assertions; /// /// const FIVE: usize = 5; /// /// const_assert!(FIVE * FIVE == 25); /// /// fn main() { /// const_assert!(2 + 2 == 4); /// const_assert!(FIVE - FIVE == 0); /// } /// ``` /// /// [static_assert]: http://en.cppreference.com/w/cpp/language/static_assert #[macro_export(local_inner_macros)] macro_rules! const_assert { ($($xs:tt)+) => { _const_assert!($($xs)+); }; } #[doc(hidden)] #[cfg(feature = "nightly")] #[macro_export(local_inner_macros)] #[allow(dead_code)] macro_rules! _const_assert { ($($xs:expr),+ $(,)*) => { #[allow(unknown_lints, eq_op)] const _: [(); 0 - !($({ const B: bool = $xs; B })&&+) as usize] = []; }; } #[doc(hidden)] #[cfg(not(feature = "nightly"))] #[macro_export(local_inner_macros)] macro_rules! _const_assert { ($($xs:expr),+ $(,)*) => { #[allow(unknown_lints, eq_op)] let _ = [(); 0 - !($({ const B: bool = $xs; B })&&+) as usize]; }; ($label:ident; $($xs:tt)+) => { #[allow(dead_code, non_snake_case)] fn $label() { const_assert!($($xs)+); } }; } /// Asserts that constants are equal in value. /// /// # Examples /// /// Works as a shorthand for `const_assert!(a == b)`: /// #[cfg_attr(feature = "nightly", doc = "```ignore")] #[cfg_attr(not(feature = "nightly"), doc = "```")] /// # #[macro_use] /// # extern crate static_assertions; /// const TWO: usize = 2; /// const_assert_eq!(two; TWO * TWO, TWO + TWO, 4); /// /// fn main() { /// const NUM: usize = 32; /// const_assert_eq!(NUM + NUM, 64); /// } /// ``` /// /// Just because 2 × 2 = 2 + 2 doesn't mean it holds true for other numbers: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// const_assert_eq!(4 + 4, 4 * 4); /// # } /// ``` #[macro_export(local_inner_macros)] macro_rules! const_assert_eq { ($x:expr, $($xs:expr),+ $(,)*) => { const_assert!($($x == $xs),+); }; ($label:ident; $x:expr, $($xs:expr),+ $(,)*) => { const_assert!($label; $($x == $xs),+); }; } /// Asserts that constants are _not_ equal in value. /// /// # Examples /// /// Works as a shorthand for `const_assert!(a != b)`: /// #[cfg_attr(feature = "nightly", doc = "```ignore")] #[cfg_attr(not(feature = "nightly"), doc = "```")] /// # #[macro_use] /// # extern crate static_assertions; /// const_assert_ne!(nums; 1, 2, 3, 4); /// /// fn main() { /// const NUM: usize = 32; /// const_assert_ne!(NUM * NUM, 64); /// } /// ``` /// /// The magic number 2, where 2 × 2 = 2 + 2: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; /// # fn main() { /// const_assert_ne!(2 + 2, 2 * 2); /// # } /// ``` #[macro_export(local_inner_macros)] macro_rules! const_assert_ne { ($x:expr, $($xs:expr),+ $(,)*) => { const_assert!($($x != $xs),+); }; ($label:ident; $x:expr, $($xs:expr),+ $(,)*) => { const_assert!($label; $($x != $xs),+); }; } static_assertions-0.3.3/src/lib.rs010064400007650000024000000105561350023676500154160ustar0000000000000000//! [![Banner](https://raw.githubusercontent.com/nvzqz/static-assertions-rs/assets/Banner.png)](https://github.com/nvzqz/static-assertions-rs) //! //! Compile-time assertions to ensure that invariants are met. //! //! _All_ assertions within this crate are performed at [compile-time]. This //! allows for finding errors quickly and early when it comes to ensuring //! certain features or aspects of a codebase. These macros are especially //! important when exposing a public API that requires types to be the same size //! or implement certain traits. //! //! # Usage //! //! This crate is available [on crates.io][crate] and can be used by adding the //! following to your project's [`Cargo.toml`]: //! //! ```toml //! [dependencies] //! static_assertions = "0.3.3" //! ``` //! //! and this to your crate root (`main.rs` or `lib.rs`): //! //! ``` //! #[macro_use] //! extern crate static_assertions; //! # fn main() {} //! ``` //! //! # Examples //! //! Very thorough examples are provided in the docs for //! [each individual macro](#macros). Failure case examples are also documented. //! //! # Limitations //! //! Due to implementation details, some macros can only be used normally from //! within the context of a function. To use these macros in other contexts, a //! unique label must be provided. //! //! ```compile_fail //! # #[macro_use] extern crate static_assertions; //! # fn main() {} //! // error: expected item after attributes //! const_assert!(true == true); //! ``` //! //! This can be fixed via: //! #![cfg_attr(feature = "nightly", doc = "```ignore")] #![cfg_attr(not(feature = "nightly"), doc = "```")] //! # #[macro_use] extern crate static_assertions; //! # fn main() {} //! const_assert!(label; true == true); //! ``` //! //! This can be followed at [issue #1][issue1]. //! //! ## Labeling Limitation Fix //! //! The labeling workaround is **not //! necessary** (and is removed) //! when compiling on nightly Rust with the `nightly` feature flag enabled. This //! can be done by having the following in your project's [`Cargo.toml`]: //! //! ```toml //! [dependencies.static_assertions] //! version = "0.3.3" //! features = ["nightly"] //! ``` //! //! To compile with nightly Rust, run the following in your //! [shell](https://en.wikipedia.org/wiki/Shell_(computing)) or //! [command prompt](https://en.wikipedia.org/wiki/Command_Prompt) of choice: //! //! ```sh //! rustup install nightly //! cargo +nightly build //! ``` //! //! Notice that this also requires enabling the //! [`underscore_const_names`](https://github.com/rust-lang/rust/issues/54912) //! nightly Rust feature: //! #![cfg_attr(feature = "nightly", doc = "```")] #![cfg_attr(not(feature = "nightly"), doc = "```ignore")] //! #![feature(underscore_const_names)] //! # #[macro_use] extern crate static_assertions; //! //! const_assert!(true != false); //! //! fn main() { //! const_assert!(false != true); //! } //! ``` //! //! # Changes //! //! See [`CHANGELOG.md`](https://github.com/nvzqz/static-assertions-rs/blob/master/CHANGELOG.md) //! for a complete list of what has changed from one version to another. //! //! # Donate //! //! This project is made freely available (as in free beer), but unfortunately //! not all beer is free! So, if you would like to buy me a beer (or coffee or //! *more*), then consider supporting my work that's benefited your project //! and thousands of others. //! //! //! Become a Patron! //! //! //! Buy me a coffee //! //! //! [issue1]: https://github.com/nvzqz/static-assertions-rs/issues/1 //! [crate]: https://crates.io/crates/static_assertions //! [compile-time]: https://en.wikipedia.org/wiki/Compile_time //! [`Cargo.toml`]: https://doc.rust-lang.org/cargo/reference/manifest.html #![doc(html_root_url = "https://docs.rs/static_assertions/0.3.3")] #![doc(html_logo_url = "https://raw.githubusercontent.com/nvzqz/static-assertions-rs/assets/Icon.png")] #![no_std] #![deny(unused_macros)] #[doc(hidden)] pub extern crate core as _core; mod assert_cfg; mod assert_eq_size; mod assert_eq_type; mod assert_fields; mod assert_impl; mod assert_ne_type; mod assert_obj_safe; mod const_assert; static_assertions-0.3.3/.cargo_vcs_info.json0000644000000001120000000000000146310ustar00{ "git": { "sha1": "46453ee19cc73b2f7888243a59ffe9a54ae23149" } }