Coder Social home page Coder Social logo

zst-volatile's Introduction

zst-volatile

Warning

This is work in progress.

Usage

Take a look in the tests directory for an example.

Explanation

Offset

Volatile is a zero sized wrapper.

Volatile<T, O> has two generics:

  1. T is that datatype of the pointer.
  2. O is an offset from a pointer encoded as a type.

Volatile::<T, O>::read calculates the real pointer by offsetting self by O bytes. This allows having multiple zero sized volatile references pointing to the same address while still maintaining the information about where the reference actually points.

Structs

For each struct that derives VolatileStruct, we generate another struct with the same fields, but using the Volatile wrapper.

As an example take this struct:

#[derive(VolatileStruct)]
#[repr(C)]
pub struct Child1 {
    field1: u32,
    field2: u32,
    field3: u32,
    field4: u32,
}

The VolatileStruct derive macro expands this into:

pub struct VolatileChild1 {
    field1: Volatile<u32, offset::Align<offset::Zero, u32>>,
    field2: Volatile<u32, offset::Align<offset::PastField<offset::Zero, u32>, u32>>,
    field3: Volatile<u32, offset::Align<offset::PastField<offset::PastField<offset::Zero, u32>, u32>, u32>>,
    field4: Volatile<u32, offset::Align<offset::PastField<offset::PastField<offset::PastField<offset::Zero, u32>, u32>, u32, >, u32>,>,
}

We also generate an implementation for VolatileStruct.

unsafe impl VolatileStruct for Child1 {
    type Struct = VolatileChild1;
}

This VolatileStruct implementation is used with the Deref & DerefMut impls that allow Volatile<Struct> to decompose into VolatileStruct. This is what allows accessing fields of children directly.

impl<T, O> Deref for Volatile<T, O>
where
    T: VolatileStruct,
    O: Offset,
{
    type Target = T::Struct;

    fn deref(&self) -> &Self::Target {
        // ...
    }
}

impl<T, O> DerefMut for Volatile<T, O>
where
    T: VolatileStruct,
    O: Offset,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        // ...
    }
}

rust-analyzer can cope suprisingly well with this offering code completitions even to nested fields.

zst-volatile's People

Contributors

freax13 avatar

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.