rickyyx / ParNVM

NVM research project @MSRA
0 stars 1 forks source link

Support multiple types within single transaction #4

Closed rickyyx closed 6 years ago

rickyyx commented 6 years ago

Problem:

Currently, a generic transaction Transaction<T> can only support reads/writes of the same type T. However, in order for the transaction to allow multiple types at runtime, it cannot be tied with a single type (even generic type).

Ideal Solution:

The best solution is to have generic associated types (GAT), which is still a rust feature being implemented. Tracking issue here: link

Current Solutions:

The current solution I can come up with is to use Box<Any>: Use of Any trait to have the users of the library to perform casting at runtime. This makes sense as the user (who defined the data) should know that type each read and write should be associated with. Such as below:

use std::any::Any;

pub struct MyStruct{
    val : Box<Any>
}

impl MyStruct {

    pub fn new<T: Any>(val: T) -> MyStruct{
        MyStruct {
            val : Box::new(val)
        }
    }

    pub fn read(self) -> Box<Any> {
        self.val
    } 
}
fn main() {
    let x = MyStruct::new(1 as u32);
    println!("{}", x.read().downcast::<u32>().unwrap())
}
rickyyx commented 6 years ago

Another thing if could be done, it will be awesome:

Generic Closure Use case:

let p = Piece::new(ArgsList, MyClosure);
p.run();

Potentially, a piece can hold on to any kind of function...but they need to belong to a certain trait object so that a common behaviour can be defined on them (aka, run()).

rickyyx commented 6 years ago

This is resolved by erasing type information by converting T to Any, and some other indirections.

Fixed by ac62d9b13f89c4996025b7dceca32d891b1e6af2