Coder Social home page Coder Social logo

phantom-fields's People

Contributors

lokathor avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

phantom-fields's Issues

We might be able to drop the proc-macro part?

macro_rules! phantom_bool {
    (self . $tokens:tt, $t:ty, $l:literal, $getter:ident, $wither:ident, $setter:ident) => {
        #[inline]
        pub fn $getter(self) -> bool {
            self.$tokens & ((1 as $t) << $l) > 0
        }
        #[inline]
        pub fn $wither(self, new_val: bool) -> Self {
            Self((self.$tokens) ^ (((new_val as $t).wrapping_neg() ^ (self.$tokens)) & ((1 as $t) << $l)))
        }
        #[inline]
        pub fn $setter(&mut self, new_val: bool) {
            *self = self.$wither(new_val);
        }
    };
    (self . $tokens:tt, $t:ty, $l:literal, $getter:ident) => {
        #[inline]
        pub fn $getter(self) -> bool {
            self.$tokens & ((1 as $t) << $l) > 0
        }
    };
    (self . $tokens:tt, $t:ty, $i:ident, $getter:ident, $wither:ident, $setter:ident) => {
        #[inline]
        pub fn $getter(self) -> bool {
            self.$tokens & $i > 0
        }
        #[inline]
        pub fn $wither(self, new_val: bool) -> Self {
            Self((self.$tokens) ^ (((new_val as $t).wrapping_neg() ^ (self.$tokens)) & $i))
        }
        #[inline]
        pub fn $setter(&mut self, new_val: bool) {
            *self = self.$wither(new_val);
        }
    };
    (self . $tokens:tt, $t:ty, $i:ident, $getter:ident) => {
        #[inline]
        pub fn $getter(self) -> bool {
            self.$tokens & $i > 0
        }
    };
}

macro_rules! phantom_int {
    (self . $tokens:tt, $t:ty, $low:literal - $high:literal, $getter:ident, $wither:ident, $setter:ident) => {
        #[inline]
        pub fn $getter(self) -> $t {
            const BASE_MASK: $t = ((1<<($high-$low+1)) - 1);
            (self.$tokens >> $low) & BASE_MASK
        }
        #[inline]
        pub fn $wither(self, new_val: $t) -> Self {
            const BASE_MASK: $t = ((1<<($high-$low+1)) - 1);
            const MASK: $t = ((1<<($high-$low+1)) - 1) << $low;
            Self(
                ((((new_val & BASE_MASK) << $low) ^ self.$tokens) & MASK) ^ self.$tokens
            )
        }
        #[inline]
        pub fn $setter(&mut self, new_val: $t) {
            *self = self.$wither(new_val);
        }
    };
    (self . $tokens:tt, $t:ty, $low:literal - $high:literal, $getter:ident) => {
        #[inline]
        pub fn $getter(self) -> $t {
            const BASE_MASK: $t = ((1<<($high-$low+1)) - 1);
            (self.$tokens >> $low) & BASE_MASK
        }
    };
}

const BIT_ZERO: u32 = 0;

#[derive(Debug, Clone, Copy, Default)]
pub struct Example(u32);
impl Example {
    phantom_bool!(self.0, u32, 0, green, with_green, set_green);
    phantom_bool!(self.0, u32, 0, blue);
    phantom_bool!(self.0, u32, BIT_ZERO, red, with_red, set_red);
    phantom_bool!(self.0, u32, BIT_ZERO, purple);
    phantom_int!(self.0, u32, 1-2, val, with_val, set_val);
    phantom_int!(self.0, u32, 1-2, foo);
}

fn main() {
    let e = Example(0b110);
    println!("{}", e.val());
    for v in 0 .. 4 {
        println!("{}", e.with_val(v).val());
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.