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

Implemented a reference version of Plucker/ByNameFieldPlucker #240

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
47 changes: 44 additions & 3 deletions core/src/hlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,38 @@ where
}
}

/// Implementation when target is reference and the pluck target is in head
impl<'a, T, Tail: ToRef<'a>> Plucker<&'a T, Here> for &'a HCons<T, Tail> {
type Remainder = <Tail as ToRef<'a>>::Output;

fn pluck(self) -> (&'a T, Self::Remainder) {
(&self.head, self.tail.to_ref())
}
}

/// Implementation when target is reference the pluck target is in the tail
impl<'a, Head, Tail, FromTail, TailIndex> Plucker<&'a FromTail, There<TailIndex>>
for &'a HCons<Head, Tail>
where
&'a Tail: Plucker<&'a FromTail, TailIndex>,
{
type Remainder = HCons<&'a Head, <&'a Tail as Plucker<&'a FromTail, TailIndex>>::Remainder>;

fn pluck(self) -> (&'a FromTail, Self::Remainder) {
let (target, tail_remainder): (
&'a FromTail,
<&'a Tail as Plucker<&'a FromTail, TailIndex>>::Remainder,
) = <&'a Tail as Plucker<&'a FromTail, TailIndex>>::pluck(&self.tail);
(
target,
HCons {
head: &self.head,
tail: tail_remainder,
},
)
}
}

/// Trait for pulling out some subset of an HList, using type inference.
///
/// This trait is part of the implementation of the inherent method
Expand Down Expand Up @@ -1579,6 +1611,7 @@ where
mod tests {
use super::*;

use alloc::string::ToString;
use alloc::vec;

#[test]
Expand Down Expand Up @@ -1607,10 +1640,18 @@ mod tests {

#[test]
fn test_pluck() {
let h = hlist![1, "hello", true, 42f32];
let (t, r): (f32, _) = h.pluck();
let h = hlist![1, "hello".to_string(), true, 42f32];
let (t, r): (f32, _) = h.clone().pluck();
assert_eq!(t, 42f32);
assert_eq!(r, hlist![1, "hello", true])
assert_eq!(r, hlist![1, "hello".to_string(), true]);
}

#[test]
fn test_ref_pluck() {
let h = &hlist![1, "hello".to_string(), true, 42f32];
let (t, r): (&f32, _) = h.pluck();
assert_eq!(t, &42f32);
assert_eq!(r, hlist![&1, &"hello".to_string(), &true]);
}

#[test]
Expand Down
63 changes: 62 additions & 1 deletion core/src/labelled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@

use crate::hlist::*;
use crate::indices::*;
use crate::traits::ToRef;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -627,6 +628,40 @@ where
}
}

/// Implementation when target is reference and the pluck target key is in the head.
impl<'a, K, V, Tail: ToRef<'a>> ByNameFieldPlucker<K, Here> for &'a HCons<Field<K, V>, Tail> {
type TargetValue = &'a V;
type Remainder = <Tail as ToRef<'a>>::Output;

#[inline(always)]
fn pluck_by_name(self) -> (Field<K, Self::TargetValue>, Self::Remainder) {
let field = field_with_name(self.head.name, &self.head.value);
(field, self.tail.to_ref())
}
}

/// Implementation when target is reference and the pluck target key is in the tail.
impl<'a, Head, Tail, K, TailIndex> ByNameFieldPlucker<K, There<TailIndex>> for &'a HCons<Head, Tail>
where
&'a Tail: ByNameFieldPlucker<K, TailIndex>,
{
type TargetValue = <&'a Tail as ByNameFieldPlucker<K, TailIndex>>::TargetValue;
type Remainder = HCons<&'a Head, <&'a Tail as ByNameFieldPlucker<K, TailIndex>>::Remainder>;

#[inline(always)]
fn pluck_by_name(self) -> (Field<K, Self::TargetValue>, Self::Remainder) {
let (target, tail_remainder) =
<&'a Tail as ByNameFieldPlucker<K, TailIndex>>::pluck_by_name(&self.tail);
(
target,
HCons {
head: &self.head,
tail: tail_remainder,
},
)
}
}

/// Trait for transmogrifying a `Source` type into a `Target` type.
///
/// What is "transmogrifying"? In this context, it means to convert some data of type `A`
Expand Down Expand Up @@ -917,7 +952,7 @@ mod tests {
use super::chars::*;
use super::*;
use alloc::collections::{LinkedList, VecDeque};
use alloc::{boxed::Box, format, vec, vec::Vec};
use alloc::{boxed::Box, format, string::ToString, vec, vec::Vec};

// Set up some aliases
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -976,6 +1011,32 @@ mod tests {
assert_eq!(name.value, "Joe")
}

#[test]
fn test_pluck_by_name() {
let record = hlist![
field!(is_admin, true),
field!(name, "Joe".to_string()),
field!((a, g, e), 30),
];

let (name, r): (Field<name, _>, _) = record.clone().pluck_by_name();
assert_eq!(name.value, "Joe");
assert_eq!(r, hlist![field!(is_admin, true), field!((a, g, e), 30),]);
}

#[test]
fn test_ref_pluck_by_name() {
let record = &hlist![
field!(is_admin, true),
field!(name, "Joe".to_string()),
field!((a, g, e), 30),
];

let (name, r): (Field<name, _>, _) = record.pluck_by_name();
assert_eq!(name.value, "Joe");
assert_eq!(r, hlist![&field!(is_admin, true), &field!((a, g, e), 30),]);
}

#[test]
fn test_unlabelling() {
let labelled_hlist = hlist![field!(name, "joe"), field!((a, g, e), 3)];
Expand Down
Loading