Enable fixed len mutable params in `action!` macro

next
Sayan Nandan 3 years ago
parent 589ef85e2c
commit b2c0b9ecf2

@ -33,8 +33,7 @@ use bytes::Bytes;
action!( action!(
/// Run a `GET` query /// Run a `GET` query
fn get(handle: &crate::coredb::CoreDB, con: &mut T, act: ActionIter) { fn get(handle: &crate::coredb::CoreDB, con: &mut T, mut act: ActionIter) {
let mut act = act;
err_if_len_is!(act, con, not 1); err_if_len_is!(act, con, not 1);
let res: Option<Bytes> = { let res: Option<Bytes> = {
let reader = handle.get_ref(); let reader = handle.get_ref();

@ -30,8 +30,7 @@ action!(
/// Run a `KEYLEN` query /// Run a `KEYLEN` query
/// ///
/// At this moment, `keylen` only supports a single key /// At this moment, `keylen` only supports a single key
fn keylen(handle: &crate::coredb::CoreDB, con: &mut T, act: ActionIter) { fn keylen(handle: &crate::coredb::CoreDB, con: &mut T, mut act: ActionIter) {
let mut act = act;
err_if_len_is!(act, con, not 1); err_if_len_is!(act, con, not 1);
let res: Option<usize> = { let res: Option<usize> = {
let reader = handle.get_ref(); let reader = handle.get_ref();

@ -30,8 +30,7 @@ use bytes::Bytes;
action!( action!(
/// Run an `LSKEYS` query /// Run an `LSKEYS` query
fn lskeys(handle: &crate::coredb::CoreDB, con: &mut T, act: ActionIter) { fn lskeys(handle: &crate::coredb::CoreDB, con: &mut T, mut act: ActionIter) {
let mut act = act;
err_if_len_is!(act, con, gt 1); err_if_len_is!(act, con, gt 1);
let item_count = if let Some(cnt) = act.next() { let item_count = if let Some(cnt) = act.next() {
if let Ok(cnt) = cnt.parse::<usize>() { if let Ok(cnt) = cnt.parse::<usize>() {

@ -29,8 +29,7 @@ use crate::dbnet::connection::prelude::*;
action!( action!(
/// Run an `MSET` query /// Run an `MSET` query
fn mset(handle: &crate::coredb::CoreDB, con: &mut T, act: ActionIter) { fn mset(handle: &crate::coredb::CoreDB, con: &mut T, mut act: ActionIter) {
let mut act = act;
let howmany = act.len(); let howmany = act.len();
if is_lowbit_set!(howmany) || howmany == 0 { if is_lowbit_set!(howmany) || howmany == 0 {
// An odd number of arguments means that the number of keys // An odd number of arguments means that the number of keys

@ -29,8 +29,7 @@ use crate::dbnet::connection::prelude::*;
action!( action!(
/// Run an `MUPDATE` query /// Run an `MUPDATE` query
fn mupdate(handle: &crate::coredb::CoreDB, con: &mut T, act: ActionIter) { fn mupdate(handle: &crate::coredb::CoreDB, con: &mut T, mut act: ActionIter) {
let mut act = act;
let howmany = act.len(); let howmany = act.len();
if is_lowbit_set!(howmany) || howmany == 0 { if is_lowbit_set!(howmany) || howmany == 0 {
// An odd number of arguments means that the number of keys // An odd number of arguments means that the number of keys

@ -35,8 +35,7 @@ use coredb::Data;
action!( action!(
/// Run a `SET` query /// Run a `SET` query
fn set(handle: &crate::coredb::CoreDB, con: &mut T, act: ActionIter) { fn set(handle: &crate::coredb::CoreDB, con: &mut T, mut act: ActionIter) {
let mut act = act;
err_if_len_is!(act, con, not 2); err_if_len_is!(act, con, not 2);
let did_we = { let did_we = {
if handle.is_poisoned() { if handle.is_poisoned() {

@ -44,8 +44,7 @@ action!(
/// ///
/// This either returns `Okay` if all the keys were set, or it returns an /// This either returns `Okay` if all the keys were set, or it returns an
/// `Overwrite Error` or code `2` /// `Overwrite Error` or code `2`
fn sset(handle: &crate::coredb::CoreDB, con: &mut T, act: ActionIter) { fn sset(handle: &crate::coredb::CoreDB, con: &mut T, mut act: ActionIter) {
let mut act = act;
let howmany = act.len(); let howmany = act.len();
if is_lowbit_set!(howmany) || howmany == 0 { if is_lowbit_set!(howmany) || howmany == 0 {
return con.write_response(responses::groups::ACTION_ERR).await; return con.write_response(responses::groups::ACTION_ERR).await;
@ -150,8 +149,7 @@ action!(
/// ///
/// This either returns `Okay` if all the keys were updated, or it returns `Nil` /// This either returns `Okay` if all the keys were updated, or it returns `Nil`
/// or code `1` /// or code `1`
fn supdate(handle: &crate::coredb::CoreDB, con: &mut T, act: ActionIter) { fn supdate(handle: &crate::coredb::CoreDB, con: &mut T, mut act: ActionIter) {
let mut act = act;
let howmany = act.len(); let howmany = act.len();
if is_lowbit_set!(howmany) || howmany == 0 { if is_lowbit_set!(howmany) || howmany == 0 {
return con.write_response(responses::groups::ACTION_ERR).await; return con.write_response(responses::groups::ACTION_ERR).await;

@ -33,8 +33,7 @@ use crate::dbnet::connection::prelude::*;
action!( action!(
/// Run an `UPDATE` query /// Run an `UPDATE` query
fn update(handle: &CoreDB, con: &mut T, act: ActionIter) { fn update(handle: &CoreDB, con: &mut T, mut act: ActionIter) {
let mut act = act;
err_if_len_is!(act, con, not 2); err_if_len_is!(act, con, not 2);
let did_we = { let did_we = {
if handle.is_poisoned() { if handle.is_poisoned() {

@ -33,8 +33,7 @@ action!(
/// Run an `USET` query /// Run an `USET` query
/// ///
/// This is like "INSERT or UPDATE" /// This is like "INSERT or UPDATE"
fn uset(handle: &crate::coredb::CoreDB, con: &mut T, act: ActionIter) { fn uset(handle: &crate::coredb::CoreDB, con: &mut T, mut act: ActionIter) {
let mut act = act;
let howmany = act.len(); let howmany = act.len();
if is_lowbit_set!(howmany) || howmany == 0 { if is_lowbit_set!(howmany) || howmany == 0 {
// An odd number of arguments means that the number of keys // An odd number of arguments means that the number of keys

@ -63,6 +63,7 @@ const PAIR_MAP_LUT: [u8; 200] = [
0x39, 0x38, 0x39, 0x39, // 0x39 0x39, 0x38, 0x39, 0x39, // 0x39
]; ];
#[allow(dead_code)]
/// A 32-bit integer buffer with one extra byte /// A 32-bit integer buffer with one extra byte
pub type Integer32Buffer = Integer32BufferRaw<11>; pub type Integer32Buffer = Integer32BufferRaw<11>;
@ -74,6 +75,7 @@ pub struct Integer32BufferRaw<const N: usize> {
inner_stack: Array<u8, 11>, inner_stack: Array<u8, 11>,
} }
#[allow(dead_code)]
impl<const N: usize> Integer32BufferRaw<N> { impl<const N: usize> Integer32BufferRaw<N> {
/// Initialize a buffer /// Initialize a buffer
pub fn init(integer: u32) -> Self { pub fn init(integer: u32) -> Self {

@ -134,7 +134,7 @@ macro_rules! assert_hmeq {
/// ///
/// ## Limitations /// ## Limitations
/// ///
/// This macro cannot currently handle mutable parameters /// This macro can only handle mutable parameters for a fixed number of arguments (three)
/// ///
macro_rules! action { macro_rules! action {
( (
@ -149,4 +149,16 @@ macro_rules! action {
Strm: AsyncReadExt + AsyncWriteExt + Unpin + Send + Sync, Strm: AsyncReadExt + AsyncWriteExt + Unpin + Send + Sync,
$block $block
}; };
(
$(#[$attr:meta])*
fn $fname:ident($argone:ident: $argonety:ty, $argtwo:ident: $argtwoty:ty, mut $argthree:ident: $argthreety:ty)
$block:block
) => {
$(#[$attr])*
pub async fn $fname<T, Strm>($argone: $argonety, $argtwo: $argtwoty, mut $argthree: $argthreety) -> std::io::Result<()>
where
T: ProtocolConnectionExt<Strm>,
Strm: AsyncReadExt + AsyncWriteExt + Unpin + Send + Sync,
$block
};
} }

Loading…
Cancel
Save