use std::{convert::AsRef, fmt::Debug, io::Write, sync::Arc}; use futures::Future; use serde::Serialize; use tuwunel_core::{Result, arrayvec::ArrayVec, implement}; use crate::{Handle, keyval::KeyBuf, ser}; /// Fetch a value from the database into cache, returning a reference-handle /// asynchronously. The key is serialized into an allocated buffer to perform /// the query. #[implement(super::Map)] #[inline] pub fn qry( self: &Arc, key: &K, ) -> impl Future>> + Send + use<'_, K> where K: Serialize + ?Sized + Debug, { let mut buf = KeyBuf::new(); self.bqry(key, &mut buf) } /// Fetch a value from the database into cache, returning a reference-handle /// asynchronously. The key is serialized into a fixed-sized buffer to perform /// the query. The maximum size is supplied as const generic parameter. #[implement(super::Map)] #[inline] pub fn aqry( self: &Arc, key: &K, ) -> impl Future>> + Send + use<'_, MAX, K> where K: Serialize + ?Sized + Debug, { let mut buf = ArrayVec::::new(); self.bqry(key, &mut buf) } /// Fetch a value from the database into cache, returning a reference-handle /// asynchronously. The key is serialized into a user-supplied Writer. #[implement(super::Map)] #[tracing::instrument(skip(self, buf), level = "trace")] pub fn bqry( self: &Arc, key: &K, buf: &mut B, ) -> impl Future>> + Send + use<'_, K, B> where K: Serialize + ?Sized + Debug, B: Write + AsRef<[u8]>, { let key = ser::serialize(buf, key).expect("failed to serialize query key"); self.get(key) }