Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various changes based on review #583

Merged
merged 8 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions src/kv/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ pub trait Source {
///
/// A source that can provide a more efficient implementation of this method
/// should override it.
#[cfg(not(test))]
fn get(&self, key: Key) -> Option<Value<'_>> {
get_default(self, key)
}

#[cfg(test)]
fn get(&self, key: Key) -> Option<Value<'_>>;

/// Count the number of key-value pairs that can be visited.
///
/// # Implementation notes
Expand All @@ -47,17 +43,13 @@ pub trait Source {
///
/// A subsequent call to `visit` should yield the same number of key-value pairs
/// to the visitor, unless that visitor fails part way through.
#[cfg(not(test))]
fn count(&self) -> usize {
count_default(self)
}

#[cfg(test)]
fn count(&self) -> usize;
}

/// The default implementation of `Source::get`
pub(crate) fn get_default<'v>(source: &'v (impl Source + ?Sized), key: Key) -> Option<Value<'v>> {
fn get_default<'v>(source: &'v (impl Source + ?Sized), key: Key) -> Option<Value<'v>> {
struct Get<'k, 'v> {
key: Key<'k>,
found: Option<Value<'v>>,
Expand All @@ -80,7 +72,7 @@ pub(crate) fn get_default<'v>(source: &'v (impl Source + ?Sized), key: Key) -> O
}

/// The default implementation of `Source::count`.
pub(crate) fn count_default(source: impl Source) -> usize {
fn count_default(source: impl Source) -> usize {
struct Count(usize);

impl<'kvs> Visitor<'kvs> for Count {
Expand Down Expand Up @@ -158,7 +150,7 @@ where
}

fn count(&self) -> usize {
self.len()
self.iter().map(Source::count).sum()
}
}

Expand All @@ -167,7 +159,7 @@ where
S: Source,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
if let Some(ref source) = *self {
if let Some(source) = self {
source.visit(visitor)?;
}

Expand Down Expand Up @@ -233,6 +225,8 @@ mod std_support {
use std::borrow::Borrow;
use std::collections::{BTreeMap, HashMap};
use std::hash::{BuildHasher, Hash};
use std::rc::Rc;
use std::sync::Arc;

impl<S> Source for Box<S>
where
Expand All @@ -251,6 +245,40 @@ mod std_support {
}
}

impl<S> Source for Arc<S>
where
S: Source + ?Sized,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
Source::visit(&**self, visitor)
}

fn get(&self, key: Key) -> Option<Value<'_>> {
Source::get(&**self, key)
}

fn count(&self) -> usize {
Source::count(&**self)
}
}

impl<S> Source for Rc<S>
where
S: Source + ?Sized,
{
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
Source::visit(&**self, visitor)
}

fn get(&self, key: Key) -> Option<Value<'_>> {
Source::get(&**self, key)
}

fn count(&self) -> usize {
Source::count(&**self)
}
}

impl<S> Source for Vec<S>
where
S: Source,
Expand Down Expand Up @@ -714,14 +742,6 @@ mod tests {
fn visit<'kvs>(&'kvs self, visitor: &mut dyn Visitor<'kvs>) -> Result<(), Error> {
visitor.visit_pair(self.key.to_key(), self.value.to_value())
}

fn get(&self, key: Key) -> Option<Value<'_>> {
get_default(self, key)
}

fn count(&self) -> usize {
count_default(self)
}
}

assert_eq!(1, Source::count(&("a", 1)));
Expand Down
24 changes: 22 additions & 2 deletions src/kv/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,14 @@ impl<'v> From<&'v i128> for Value<'v> {
impl<'v> From<&'v std::num::NonZeroU128> for Value<'v> {
fn from(v: &'v std::num::NonZeroU128) -> Value<'v> {
// SAFETY: `NonZeroU128` and `u128` have the same ABI
Value::from_value_bag(unsafe { std::mem::transmute::<&std::num::NonZeroU128, &u128>(v) })
Value::from_value_bag(unsafe { &*(v as *const std::num::NonZeroU128 as *const u128) })
}
}

impl<'v> From<&'v std::num::NonZeroI128> for Value<'v> {
fn from(v: &'v std::num::NonZeroI128) -> Value<'v> {
// SAFETY: `NonZeroI128` and `i128` have the same ABI
Value::from_value_bag(unsafe { std::mem::transmute::<&std::num::NonZeroI128, &i128>(v) })
Value::from_value_bag(unsafe { &*(v as *const std::num::NonZeroI128 as *const i128) })
}
}

Expand Down Expand Up @@ -589,6 +589,8 @@ impl<'v> Value<'v> {
#[cfg(feature = "kv_unstable_std")]
mod std_support {
use std::borrow::Cow;
use std::rc::Rc;
use std::sync::Arc;

use super::*;

Expand All @@ -601,6 +603,24 @@ mod std_support {
}
}

impl<T> ToValue for Arc<T>
where
T: ToValue + ?Sized,
{
fn to_value(&self) -> Value {
(**self).to_value()
}
}

impl<T> ToValue for Rc<T>
where
T: ToValue + ?Sized,
{
fn to_value(&self) -> Value {
(**self).to_value()
}
}

impl ToValue for String {
fn to_value(&self) -> Value {
Value::from(&**self)
Expand Down