awint_macros/
lib.rs

1//! Note: macro docs are in the main `awint` crate
2
3extern crate proc_macro;
4use awint_macro_internals::{
5    awint_macro_bits, awint_macro_cc, awint_macro_extawi, awint_macro_inlawi,
6    unstable_native_inlawi_ty, awint_macro_awi,
7};
8use proc_macro::TokenStream;
9
10// I can't get rustdoc to handle links in reexported macros at all
11
12/// Specifies an [InlAwi](awint_macro_internals::awint_core::InlAwi) _type_
13/// in terms of its bitwidth
14#[proc_macro]
15pub fn inlawi_ty(input: TokenStream) -> TokenStream {
16    let w = input
17        .to_string()
18        .parse::<u128>()
19        .expect("Input should parse as an unsigned integer");
20    assert!(
21        w != 0,
22        "Tried to make an `InlAwi` type with an invalid bitwidth of 0"
23    );
24    unstable_native_inlawi_ty(w).parse().unwrap()
25}
26
27// C4D
28/// Copy Corresponding Concatenations of Components Dynamically.
29///
30/// Takes concatenations of components as an input, and copies bits of the source to
31/// corresponding bits of the sinks. Returns `()` if the operation is
32/// infallible, otherwise returns `Option<()>`. Returns `None` if component
33/// indexes are out of bounds or if concatenation bitwidths mismatch. Performs
34/// allocation in general, but will try to avoid allocation if the common
35/// bitwdith can be determined statically, or if concatenations are all of
36/// single components. See `awint::macro_docs` for more.
37#[proc_macro]
38pub fn cc(input: TokenStream) -> TokenStream {
39    match awint_macro_cc(&input.to_string()) {
40        Ok(s) => s.parse().unwrap(),
41        Err(s) => panic!("{}", s),
42    }
43}
44
45/// A concatenations of components macro, additionally using the source value to
46/// construct an [InlAwi](awint_macro_internals::awint_core::InlAwi). See `awint::macro_docs` for more.
47#[proc_macro]
48pub fn inlawi(input: TokenStream) -> TokenStream {
49    match awint_macro_inlawi(&input.to_string()) {
50        Ok(s) => s.parse().unwrap(),
51        Err(s) => panic!("{}", s),
52    }
53}
54
55/// A concatenations of components macro, additionally using the source value to
56/// construct an [ExtAwi](awint_macro_internals::awint_ext::ExtAwi). See `awint::macro_docs` for more.
57#[proc_macro]
58pub fn extawi(input: TokenStream) -> TokenStream {
59    match awint_macro_extawi(&input.to_string()) {
60        Ok(s) => s.parse().unwrap(),
61        Err(s) => panic!("{}", s),
62    }
63}
64
65/// A concatenations of components macro, additionally using the source value to
66/// construct an [Awi](awint_macro_internals::awint_ext::Awi). See `awint::macro_docs` for more.
67#[proc_macro]
68pub fn awi(input: TokenStream) -> TokenStream {
69    match awint_macro_awi(&input.to_string()) {
70        Ok(s) => s.parse().unwrap(),
71        Err(s) => panic!("{}", s),
72    }
73}
74
75// We make the `bits` macro `&'static`, because making a relaxed `bits` or
76// `bits_mut` macro typically leads to unoptimality and weird compiler errors.
77// Users should use references from `extawi` or `inlawi` in any other case.
78
79// TODO The only thing we might change is making the configuration
80// `static_width: false` if `const` allocation is ever supported.
81
82/// A concatenations of components macro, additionally using the source value to
83/// construct a `&'static Bits`.
84///
85/// Requires `const_support` and some feature flags to work. See `awint::macro_docs` for more.
86#[proc_macro]
87pub fn bits(input: TokenStream) -> TokenStream {
88    match awint_macro_bits(&input.to_string()) {
89        Ok(s) => s.parse().unwrap(),
90        Err(s) => panic!("{}", s),
91    }
92}