Add convenience macros

next
Sayan Nandan 3 years ago
parent efec980fa6
commit 8cfab3f7d3

@ -27,7 +27,7 @@
#![deny(unused_crate_dependencies)]
#![deny(unused_imports)]
//! The core library for the Skytable
//! The core library for Skytable
//!
//! This contains modules which are shared by both the `cli` and the `server` modules

@ -283,7 +283,7 @@ impl ParsedConfig {
/// TOML file (represented as an object)
fn from_config(cfg_info: Config) -> Self {
ParsedConfig {
noart: libsky::option_unwrap_or!(cfg_info.server.noart, false),
noart: option_unwrap_or!(cfg_info.server.noart, false),
bgsave: if let Some(bgsave) = cfg_info.bgsave {
match (bgsave.enabled, bgsave.every) {
// TODO: Show a warning that there are unused keys
@ -301,7 +301,7 @@ impl ParsedConfig {
SnapshotConfig::Enabled(SnapshotPref::new(
snapshot.every,
snapshot.atmost,
libsky::option_unwrap_or!(snapshot.failsafe, true),
option_unwrap_or!(snapshot.failsafe, true),
))
})
.unwrap_or_else(SnapshotConfig::default),
@ -332,7 +332,7 @@ impl ParsedConfig {
port: cfg_info.server.port,
}
},
maxcon: libsky::option_unwrap_or!(cfg_info.server.maxclient, MAXIMUM_CONNECTION_LIMIT),
maxcon: option_unwrap_or!(cfg_info.server.maxclient, MAXIMUM_CONNECTION_LIMIT),
}
}
#[cfg(test)]
@ -562,7 +562,7 @@ pub fn get_config_file_or_return_cfg() -> Result<ConfigType<ParsedConfig, String
},
None => None,
};
let failsafe = if let Ok(failsafe) = libsky::option_unwrap_or!(
let failsafe = if let Ok(failsafe) = option_unwrap_or!(
matches
.value_of("stop-write-on-fail")
.map(|val| val.parse::<bool>()),

@ -153,7 +153,7 @@ impl CoreDB {
snap_count = Some(atmost);
}
let snapcfg =
libsky::option_unwrap_or!(snap_count.map(|max| Some(SnapshotStatus::new(*max))), None);
option_unwrap_or!(snap_count.map(|max| Some(SnapshotStatus::new(*max))), None);
let db = if let Some(coremap) = coremap {
CoreDB {
coremap,

@ -108,7 +108,7 @@ impl<'a> SnapshotEngine<'a> {
} else {
(maxtop, false)
};
let snap_dir = libsky::option_unwrap_or!(snap_dir, DIR_SNAPSHOT);
let snap_dir = option_unwrap_or!(snap_dir, DIR_SNAPSHOT);
match fs::create_dir(snap_dir) {
Ok(_) => (),
Err(e) => match e.kind() {

@ -1,78 +0,0 @@
/*
* Created on Thu Jul 01 2021
*
* This file is a part of Skytable
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
* NoSQL database written by Sayan Nandan ("the Author") with the
* vision to provide flexibility in data modelling without compromising
* on performance, queryability or scalability.
*
* Copyright (c) 2021, Sayan Nandan <ohsayan@outlook.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
//! Functions for validating the encoding of objects
use std::ptr;
pub struct BufferBlockReader<const N: usize> {
buffer_ptr: *const u8,
len: usize,
len_after_step: usize,
idx: usize,
}
impl<const N: usize> BufferBlockReader<N> {
pub const fn new(buffer: &[u8]) -> Self {
Self {
buffer_ptr: buffer.as_ptr(),
len: buffer.len(),
len_after_step: if buffer.len() < N {
0
} else {
buffer.len() - N
},
idx: 0,
}
}
pub const fn block_index(&self) -> usize {
self.idx
}
pub const fn has_full_block(&self) -> bool {
self.idx < self.len_after_step
}
pub fn full_block(&self) -> &u8 {
unsafe {
// UNSAFE(@ohsayan): We're dereferencing a ptr that we know will not be null
// because the len of the buffer gurantees that
&*self.buffer_ptr.add(self.idx)
}
}
pub unsafe fn get_remaining(&self, dst: *mut u8) -> usize {
if self.len == self.idx {
0
} else {
{
ptr::write_bytes(dst, 0x20, N);
ptr::copy_nonoverlapping(self.buffer_ptr.add(self.idx), dst, self.len - self.idx);
}
self.len - self.idx
}
}
pub fn advance(&mut self) {
self.idx += N
}
}

@ -33,7 +33,6 @@ use crate::coredb::htable::MapSingleReference;
use crate::coredb::htable::SharedValue;
use core::sync::atomic::AtomicBool;
use core::sync::atomic::Ordering;
mod encoding;
const ORD_RELAXED: Ordering = Ordering::Relaxed;

@ -45,6 +45,10 @@ use std::process;
use std::sync::Arc;
use std::thread;
use std::time;
#[macro_use]
mod util;
#[macro_use] // HACK(@ohsayan): macro_use will only work with extern crate for some moon reasons
extern crate libsky;
mod actions;
mod admin;
mod arbiter;
@ -60,7 +64,6 @@ mod resp;
mod services;
#[cfg(test)]
mod tests;
mod util;
const PATH: &str = ".sky_pid";

@ -57,3 +57,19 @@ unsafe impl<T> Unwrappable<T> for Option<T> {
}
}
}
#[macro_export]
macro_rules! consts {
($($(#[$attr:meta])* $ident:ident : $ty:ty = $expr:expr;)*) => {
$(
$(#[$attr])*
const $ident: $ty = $expr;
)*
};
($($(#[$attr:meta])* $vis:vis $ident:ident : $ty:ty = $expr:expr;)*) => {
$(
$(#[$attr])*
$vis const $ident: $ty = $expr;
)*
};
}

Loading…
Cancel
Save