Skip to content

Commit

Permalink
refactor: remove usage of unstable features (#316)
Browse files Browse the repository at this point in the history
* refactor: remove usage of unstable features

Signed-off-by: MrCroxx <[email protected]>

* refactor: some more

Signed-off-by: MrCroxx <[email protected]>

---------

Signed-off-by: MrCroxx <[email protected]>
  • Loading branch information
MrCroxx authored Apr 10, 2024
1 parent 3aa689d commit 2693669
Show file tree
Hide file tree
Showing 21 changed files with 173 additions and 77 deletions.
1 change: 1 addition & 0 deletions foyer-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ normal = ["foyer-workspace-hack"]
[dependencies]
anyhow = "1.0"
bytes = "1"
cfg-if = "1"
foyer-workspace-hack = { version = "0.3", path = "../foyer-workspace-hack" }
itertools = "0.12"
parking_lot = { version = "0.12", features = ["arc_lock"] }
Expand Down
38 changes: 33 additions & 5 deletions foyer-common/src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,21 @@ use std::{
ops::{Add, BitAnd, Not, Sub},
};

pub trait UnsignedTrait = Add<Output = Self>
// TODO(MrCroxx): Use `trait_alias` after stable.
// pub trait UnsignedTrait = Add<Output = Self>
// + Sub<Output = Self>
// + BitAnd<Output = Self>
// + Not<Output = Self>
// + Sized
// + From<u8>
// + Eq
// + Debug
// + Display
// + Clone
// + Copy;

pub trait Unsigned:
Add<Output = Self>
+ Sub<Output = Self>
+ BitAnd<Output = Self>
+ Not<Output = Self>
Expand All @@ -41,11 +55,25 @@ pub trait UnsignedTrait = Add<Output = Self>
+ Debug
+ Display
+ Clone
+ Copy;

pub trait Unsigned: UnsignedTrait {}
+ Copy
{
}

impl<U: UnsignedTrait> Unsigned for U {}
impl<
U: Add<Output = Self>
+ Sub<Output = Self>
+ BitAnd<Output = Self>
+ Not<Output = Self>
+ Sized
+ From<u8>
+ Eq
+ Debug
+ Display
+ Clone
+ Copy,
> Unsigned for U
{
}

#[inline(always)]
pub fn is_pow2<U: Unsigned>(v: U) -> bool {
Expand Down
94 changes: 76 additions & 18 deletions foyer-common/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,58 @@ use paste::paste;
pub type CodingError = anyhow::Error;
pub type CodingResult<T> = Result<T, CodingError>;

trait BufExt: Buf {
cfg_match! {
cfg(target_pointer_width = "16") => {
pub trait BufExt: Buf {
// TODO(MrCroxx): Use `cfg_match` after stable.
// cfg_match! {
// cfg(target_pointer_width = "16") => {
// fn get_usize(&mut self) -> usize {
// self.get_u16() as usize
// }

// fn get_isize(&mut self) -> isize {
// self.get_i16() as isize
// }
// }
// cfg(target_pointer_width = "32") => {
// fn get_usize(&mut self) -> usize {
// self.get_u32() as usize
// }

// fn get_isize(&mut self) -> isize {
// self.get_i32() as isize
// }
// }
// cfg(target_pointer_width = "64") => {
// fn get_usize(&mut self) -> usize {
// self.get_u64() as usize
// }

// fn get_isize(&mut self) -> isize {
// self.get_i64() as isize
// }
// }
// }
cfg_if::cfg_if! {
if #[cfg(target_pointer_width = "16")] {
fn get_usize(&mut self) -> usize {
self.get_u16() as usize
}

fn get_isize(&mut self) -> isize {
self.get_i16() as isize
}
}
cfg(target_pointer_width = "32") => {
else if #[cfg(target_pointer_width = "32")] {
fn get_usize(&mut self) -> usize {
self.get_u32() as usize
}

fn get_isize(&mut self) -> isize {
self.get_i32() as isize
}
}
cfg(target_pointer_width = "64") => {
else if #[cfg(target_pointer_width = "64")] {
fn get_usize(&mut self) -> usize {
self.get_u64() as usize
}

fn get_isize(&mut self) -> isize {
self.get_i64() as isize
}
Expand All @@ -54,31 +81,58 @@ trait BufExt: Buf {

impl<T: Buf> BufExt for T {}

trait BufMutExt: BufMut {
cfg_match! {
cfg(target_pointer_width = "16") => {
pub trait BufMutExt: BufMut {
// TODO(MrCroxx): Use `cfg_match` after stable.
// cfg_match! {
// cfg(target_pointer_width = "16") => {
// fn put_usize(&mut self, v: usize) {
// self.put_u16(v as u16);
// }

// fn put_isize(&mut self, v: isize) {
// self.put_i16(v as i16);
// }
// }
// cfg(target_pointer_width = "32") => {
// fn put_usize(&mut self, v: usize) {
// self.put_u32(v as u32);
// }

// fn put_isize(&mut self, v: isize) {
// self.put_i32(v as i32);
// }
// }
// cfg(target_pointer_width = "64") => {
// fn put_usize(&mut self, v: usize) {
// self.put_u64(v as u64);
// }

// fn put_isize(&mut self, v: isize) {
// self.put_i64(v as i64);
// }
// }
// }
cfg_if::cfg_if! {
if #[cfg(target_pointer_width = "16")] {
fn put_usize(&mut self, v: usize) {
self.put_u16(v as u16);
}

fn put_isize(&mut self, v: isize) {
self.put_i16(v as i16);
}
}
cfg(target_pointer_width = "32") => {
else if #[cfg(target_pointer_width = "32")] {
fn put_usize(&mut self, v: usize) {
self.put_u32(v as u32);
}

fn put_isize(&mut self, v: isize) {
self.put_i32(v as i32);
}
}
cfg(target_pointer_width = "64") => {
else if #[cfg(target_pointer_width = "64")] {
fn put_usize(&mut self, v: usize) {
self.put_u64(v as u64);
}

fn put_isize(&mut self, v: isize) {
self.put_i64(v as i64);
}
Expand All @@ -100,7 +154,9 @@ pub trait Cursor<T>: Send + Sync + 'static + std::io::Read + std::fmt::Debug {
pub trait Key:
Sized + Send + Sync + 'static + std::hash::Hash + Eq + PartialEq + Ord + PartialOrd + std::fmt::Debug + Clone
{
type Cursor: Cursor<Self> = UnimplementedCursor<Self>;
// TODO(MrCroxx): Restore this after `associated_type_defaults` is stable.
// type Cursor: Cursor<Self> = UnimplementedCursor<Self>;
type Cursor: Cursor<Self>;

/// memory weight
fn weight(&self) -> usize {
Expand All @@ -126,7 +182,9 @@ pub trait Key:
// TODO(MrCroxx): use `expect` after `lint_reasons` is stable.
#[allow(unused_variables)]
pub trait Value: Sized + Send + Sync + 'static + std::fmt::Debug + Clone {
type Cursor: Cursor<Self> = UnimplementedCursor<Self>;
// TODO(MrCroxx): Restore this after `associated_type_defaults` is stable.
// type Cursor: Cursor<Self> = UnimplementedCursor<Self>;
type Cursor: Cursor<Self>;

/// memory weight
fn weight(&self) -> usize {
Expand Down
3 changes: 0 additions & 3 deletions foyer-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(trait_alias)]
#![feature(bound_map)]
#![feature(associated_type_defaults)]
#![feature(cfg_match)]
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

pub mod async_queue;
Expand Down
19 changes: 14 additions & 5 deletions foyer-common/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ mod private {

use private::ZeroOne;

pub trait Idx =
PartialOrd<Self> + Add<Output = Self> + Sub<Output = Self> + Clone + Copy + Send + Sync + 'static + ZeroOne;

pub trait RangeBoundsExt<T: Idx>: RangeBounds<T> {
// TODO(MrCroxx): Use `trait_alias` after stable.
// pub trait Idx =
// PartialOrd<Self> + Add<Output = Self> + Sub<Output = Self> + Clone + Copy + Send + Sync + 'static + ZeroOne;

pub trait RangeBoundsExt<
T: PartialOrd<T> + Add<Output = T> + Sub<Output = T> + Clone + Copy + Send + Sync + 'static + ZeroOne,
>: RangeBounds<T>
{
fn start(&self) -> Option<T> {
match self.start_bound() {
Bound::Included(v) => Some(*v),
Expand Down Expand Up @@ -107,4 +111,9 @@ pub trait RangeBoundsExt<T: Idx>: RangeBounds<T> {
}
}

impl<T: Idx, RB: RangeBounds<T>> RangeBoundsExt<T> for RB {}
impl<
T: PartialOrd<T> + Add<Output = T> + Sub<Output = T> + Clone + Copy + Send + Sync + 'static + ZeroOne,
RB: RangeBounds<T>,
> RangeBoundsExt<T> for RB
{
}
3 changes: 2 additions & 1 deletion foyer-experimental/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ normal = ["foyer-workspace-hack"]
anyhow = "1.0"
bytes = "1"
crossbeam = { version = "0.8", features = ["std", "crossbeam-channel"] }
foyer-common = { version = "0.4", path = "../foyer-common" }
foyer-workspace-hack = { version = "0.3", path = "../foyer-workspace-hack" }
lazy_static = "1"
parking_lot = { version = "0.12", features = ["arc_lock"] }
paste = "1.0"
prometheus = "0.13"
Expand All @@ -27,7 +29,6 @@ tracing = "0.1"
[dev-dependencies]
bytesize = "1"
clap = { version = "4", features = ["derive"] }
foyer-common = { version = "0.4", path = "../foyer-common" }
hdrhistogram = "7"
itertools = "0.12"
rand = "0.8.5"
Expand Down
5 changes: 2 additions & 3 deletions foyer-experimental/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::backtrace::Backtrace;

#[derive(thiserror::Error, Debug)]
#[error("{0}")]
pub struct Error(Box<ErrorInner>);
Expand All @@ -23,7 +21,8 @@ pub struct Error(Box<ErrorInner>);
struct ErrorInner {
#[from]
source: ErrorKind,
backtrace: Backtrace,
// TODO(MrCroxx): Restore this after `error_generic_member_access` is stable.
// backtrace: Backtrace,
}

#[derive(thiserror::Error, Debug)]
Expand Down
5 changes: 0 additions & 5 deletions foyer-experimental/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(cfg_match)]
#![feature(error_generic_member_access)]
#![feature(lazy_cell)]

pub mod buf;
pub mod error;
pub mod metrics;
pub mod notify;
Expand Down
9 changes: 7 additions & 2 deletions foyer-experimental/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::{LazyLock, OnceLock};
use std::sync::OnceLock;

use prometheus::{
core::{AtomicU64, GenericGauge, GenericGaugeVec},
Expand Down Expand Up @@ -55,7 +55,12 @@ pub fn get_metrics_registry() -> &'static Registry {
REGISTRY.get_or_init(|| prometheus::default_registry().clone())
}

pub static METRICS: LazyLock<GlobalMetrics> = LazyLock::new(GlobalMetrics::default);
// TODO(MrCroxx): Use `LazyLock` after `lazy_cell` is stable.
// pub static METRICS: LazyLock<GlobalMetrics> = LazyLock::new(GlobalMetrics::default);

lazy_static::lazy_static! {
pub static ref METRICS: GlobalMetrics = GlobalMetrics::default();
}

#[derive(Debug)]
pub struct GlobalMetrics {
Expand Down
8 changes: 2 additions & 6 deletions foyer-experimental/src/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ use std::{

use bytes::{Buf, BufMut};
use crossbeam::channel;
use foyer_common::code::{BufExt, BufMutExt};
use parking_lot::{Condvar, Mutex};
use tokio::sync::oneshot;

use crate::{
asyncify,
buf::{BufExt, BufMutExt},
error::Result,
metrics::Metrics,
};
use crate::{asyncify, error::Result, metrics::Metrics};

pub trait HashValue: Send + Sync + 'static + Eq + std::fmt::Debug {
fn size() -> usize;
Expand Down
4 changes: 1 addition & 3 deletions foyer-intrusive/src/eviction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ use std::fmt::Debug;

use crate::core::adapter::Adapter;

pub trait Config = Send + Sync + 'static + Debug + Clone;

pub trait EvictionPolicy: Send + Sync + Debug + 'static {
type Adapter: Adapter;
type Config: Config;
type Config: Send + Sync + 'static + Debug + Clone;

fn new(config: Self::Config) -> Self;

Expand Down
2 changes: 0 additions & 2 deletions foyer-intrusive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(ptr_metadata)]
#![feature(trait_alias)]
#![feature(offset_of)]
// TODO(MrCroxx): use `expect` after `lint_reasons` is stable.
#![allow(clippy::new_without_default)]
Expand Down
1 change: 1 addition & 0 deletions foyer-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ foyer-intrusive = { version = "0.3", path = "../foyer-intrusive" }
foyer-workspace-hack = { version = "0.3", path = "../foyer-workspace-hack" }
futures = "0.3"
itertools = "0.12"
lazy_static = "1"
libc = "0.2"
lz4 = "1.24"
memoffset = "0.9"
Expand Down
4 changes: 2 additions & 2 deletions foyer-storage/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ mod tests {

let res = buffer.write(entry).await;
let entry = match res {
Err(BufferError::NeedRotate(entry)) => Box::into_inner(entry),
Err(BufferError::NeedRotate(entry)) => *entry,
_ => panic!("should be not enough error"),
};

Expand Down Expand Up @@ -397,7 +397,7 @@ mod tests {

let res = buffer.write(entry).await;
let entry = match res {
Err(BufferError::NeedRotate(entry)) => Box::into_inner(entry),
Err(BufferError::NeedRotate(entry)) => *entry,
_ => panic!("should be not enough error"),
};

Expand Down
Loading

0 comments on commit 2693669

Please sign in to comment.