Having trouble getting HRL working


#1

So it’s very possible that what I’m trying to do can’t be done, but it all looks like it typechecks and everything, up until the point I try and use it. :smile:

So I have a trait, BatchColumnar<'b>, and its main property is that I can define a method

fn decode<'b, T: BatchColumnar<'b>>(bytes: &'b [u8]) -> &'b [T]

This includes all the usual suspect types, like u64 and i32 and other things that you could obviously just cast the &[u8] to. However, it also includes types like &'c str where 'b: 'c , and &'c [T] where 'b: 'c, T: BatchColumnar<'b>. The whole intended purpose is to allow you to back slices in T with data from bytes.

This is all well and good, and it goes really fast when I use it. For example, the following

#[bench]
fn _batch_bench_u64_str_dec(bencher: &mut Bencher) {

    let test = "hi there, it's me!";
    let vector = (0..256u64).map(|i| (i, test)).collect::<Vec<_>>();
    let mut bytes = Vec::new();
    encode(&vector[..], &mut bytes);

    bencher.bytes = bytes.len() as u64;
    bencher.iter(|| {
        decode::<(u64, &str)>(&bytes[..]);
    });
}

cruises along at 8GB/s, mostly because it isn’t really doing anything.

Now, the problem I have is when I want to write something like

fn test<T: BatchColumnar, F:Fn(&[T])>(func: F) {
    // get actual data from somewhere else
    let bytes = vec![0u8; 1024];
    func(decode::<T>(&bytes[..]));
}

clearly BatchColumnar needs a lifetime parameter, the intended lifetime is inside the method, so it seems like it is time for higher-ranked lifetimes. I go and write

fn test<T: for<'r> BatchColumnar<'r>, F:Fn(&[T])>(func: F) {
    // get actual data from somewhere else
    let bytes = vec![0u8; 1024];
    func(decode::<T>(&bytes[..]));
}

and I can’t figure out how to call test. For example, writing something like

test(|x: &[&str]| { } );

complains: error: the requirement for<'r> 'r : is not satisfied (expected bound lifetime parameter 'r, found concrete lifetime). Other attempts to use test meet similar fates (or fail to infer the type).

Is this supposed to work? Should I be able to indicate that the lifetime in &str is meant to be flexible in the HRL sense, and I’m just missing the syntax, or is this a no-no, HRL only for closures, etc.

Thanks very much for the help! If all goes well, I’ll even show you the implementation, which Mutabah declared to be an “abomonation”. I think in a good sense…