Coder Social home page Coder Social logo

bitfield-register's Introduction

bitfield-register

Rust bitfield library for low-level registers

Build Status Crates.io

usage

#![feature(proc_macro)]

extern crate bitfield_register;
use bitfield_register::register;

use std::convert::*;

#[derive(Debug)]
enum RW {R, W}

impl From<[u8;1]> for RW { fn from(value: [u8;1]) -> Self {
    return match value[0] {
        0 => RW::R,
        1 => RW::W,
        _ => unreachable!()
    }
}}
impl Into<[u8;1]> for RW { fn into(self) -> [u8;1] {
    return match self {
        RW::R => [0],
        RW::W => [1]
    }
}}


#[derive(Debug)]
struct Address(u16);

impl From<[u8;2]> for Address { fn from(value: [u8;2]) -> Self {
    println!("{:?}", value);
    return Address(((value[1] as u16) << 8) + value[0] as u16);
}}
impl Into<[u8;2]> for Address { fn into(self) -> [u8;2] {
    return [(self.0 & 0xFF) as u8, ((self.0 & (0xFF << 8)) >> 8) as u8];
}}

#[register()]
struct Test {
    #[bitfield(from=1, to=10)]
    address: Address,
    #[bitfield(at=14)]
    rw: RW
}


fn main() {

    let mut test: Test = Default::default();

    test.set_address(Address(1023));

    test.set_rw(RW::W);

    println!("raw value:");
    for i in 0..test.0.len() {
        print!("{:0>8b} ", test.0[test.0.len() - i - 1]);
    }
    println!();

    println!("address: {:?}", test.get_address());
    println!("rw: {:?}", test.get_rw());
}

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.