Files
continuwuity/src/database/util.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.5 KiB
Rust
Raw Normal View History

2024-12-14 21:58:01 -05:00
use conduwuit::{err, Result};
2024-11-27 06:28:32 +00:00
use rocksdb::{Direction, ErrorKind, IteratorMode};
2024-08-08 17:18:30 +00:00
//#[cfg(debug_assertions)]
macro_rules! unhandled {
($msg:literal) => {
unimplemented!($msg)
};
}
// activate when stable; we're not ready for this yet
#[cfg(disable)] // #[cfg(not(debug_assertions))]
macro_rules! unhandled {
($msg:literal) => {
// SAFETY: Eliminates branches for serializing and deserializing types never
// encountered in the codebase. This can promote optimization and reduce
// codegen. The developer must verify for every invoking callsite that the
// unhandled type is in no way involved and could not possibly be encountered.
unsafe {
std::hint::unreachable_unchecked();
}
};
}
pub(crate) use unhandled;
2024-08-08 17:18:30 +00:00
#[inline]
pub(crate) fn _into_direction(mode: &IteratorMode<'_>) -> Direction {
use Direction::{Forward, Reverse};
use IteratorMode::{End, From, Start};
match mode {
| Start | From(_, Forward) => Forward,
| End | From(_, Reverse) => Reverse,
2024-08-08 17:18:30 +00:00
}
}
2024-05-28 06:59:50 +00:00
#[inline]
pub(crate) fn result<T>(
r: std::result::Result<T, rocksdb::Error>,
) -> Result<T, conduwuit::Error> {
2024-05-28 06:59:50 +00:00
r.map_or_else(or_else, and_then)
}
#[inline(always)]
2024-12-14 21:58:01 -05:00
pub(crate) fn and_then<T>(t: T) -> Result<T, conduwuit::Error> { Ok(t) }
2024-05-28 06:59:50 +00:00
2024-12-14 21:58:01 -05:00
pub(crate) fn or_else<T>(e: rocksdb::Error) -> Result<T, conduwuit::Error> { Err(map_err(e)) }
2024-05-28 06:59:50 +00:00
2024-11-27 06:28:32 +00:00
#[inline]
pub(crate) fn is_incomplete(e: &rocksdb::Error) -> bool { e.kind() == ErrorKind::Incomplete }
2024-12-14 21:58:01 -05:00
pub(crate) fn map_err(e: rocksdb::Error) -> conduwuit::Error {
let string = e.into_string();
err!(Database(error!("{string}")))
}