Skip to content

Inserting fails unpredictably with structs & alignment #14

Description

@otisdog8

For some reason, inserting will either freeze or will not properly insert if the struct isn't a multiple of 64 bits in size.

See code below:

If you change b to a u64, the code works as expected.

use std::fmt::{Debug};
use lazy_static::lazy_static;
use leapfrog::{LeapMap, Value};


#[derive(Debug, Copy, Clone, PartialEq)]
struct TestStruct {
    a: u64,
    b: u32,
}

macro_rules! value_impl {
    ($type:ty, $redirect_expr:expr, $null_expr:expr) => {
        impl Value for $type {
            #[inline]
            fn is_redirect(&self) -> bool {
                *self == $redirect_expr
            }
            #[inline]
            fn is_null(&self) -> bool {
                *self == $null_expr
            }
            #[inline]
            fn redirect() -> Self {
                $redirect_expr
            }
            #[inline]
            fn null() -> Self {
                $null_expr
            }
        }
    };
}

value_impl!(TestStruct, TestStruct {a: u64::MAX, b: 0 }, TestStruct {a: u64::MAX - 1, b: 0});

lazy_static! {
    static ref A_MAP: LeapMap<u64, TestStruct> = LeapMap::new();
    static ref B_MAP: LeapMap<u64, u64> = LeapMap::new();
}

fn main() {
    let C_MAP: LeapMap<u64, TestStruct> = LeapMap::new();
    let D_MAP: LeapMap<u64, u64> = LeapMap::new();

    let mut key = 0;
    for i in 0..100 {
        key += 1;
        let result = A_MAP.insert(key, TestStruct { a: key, b: 0});
        println!("Inserting new key {} sz {} res {:?}", key, A_MAP.len(), result);
        let result = C_MAP.insert(key, TestStruct { a: key, b: 0});
        println!("Inserting new key {} sz {} res {:?}", key, C_MAP.len(), result);
        let result = B_MAP.insert(key, key);
        println!("Inserting new key {} sz {} res {:?}", key, B_MAP.len(), result);
        let result = D_MAP.insert(key, key);
        println!("Inserting new key {} sz {} res {:?}", key, D_MAP.len(), result);

    }

    println!("Hello, world!");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions