rustolio-db 0.1.0

An DB extention for the rustolio HTTP-Server
Documentation
//
// SPDX-License-Identifier: MPL-2.0
//
// Copyright (c) 2026 Tobias Binnewies. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

use std::hash::{BuildHasher, BuildHasherDefault, Hasher};

#[derive(Debug, Default)]
pub struct StableState;

#[allow(deprecated)]
impl BuildHasher for StableState {
    type Hasher = std::hash::SipHasher;

    fn build_hasher(&self) -> Self::Hasher {
        Self::Hasher::new_with_keys(0, 0)
    }
}

#[cfg(test)]
mod tests {
    use std::hash::Hash;

    use crate::Key;

    use super::*;

    const INPUT1: u64 = 0;
    const EXPECTED1: u64 = 16738165381834614119;

    const INPUT2: &'static str = "bla - test";
    const EXPECTED2: u64 = 10437601276902349195;

    #[test]
    fn test_stable_state() {
        let state = StableState::default();
        let hash = state.hash_one(INPUT1);
        assert_eq!(hash, EXPECTED1);

        let mut hasher = state.build_hasher();
        INPUT1.hash(&mut hasher);
        let hash = hasher.finish();
        assert_eq!(hash, EXPECTED1);

        let hash = state.hash_one(INPUT2);
        assert_eq!(hash, EXPECTED2);

        let mut hasher = state.build_hasher();
        INPUT2.hash(&mut hasher);
        let hash = hasher.finish();
        assert_eq!(hash, EXPECTED2);
    }

    #[test]
    fn test_u64_state() {
        let state = U64State::default();
        let hash = state.hash_one(Key::Open(0));
        assert_eq!(hash, 0);

        let mut hasher = state.build_hasher();
        0u64.hash(&mut hasher);
        let hash = hasher.finish();
        assert_eq!(hash, 0);

        let hash = state.hash_one(123u64);
        assert_eq!(hash, 123);

        let mut hasher = state.build_hasher();
        123u64.hash(&mut hasher);
        let hash = hasher.finish();
        assert_eq!(hash, 123);
    }
}