use core::{
convert::Infallible,
future::Future,
num::NonZeroUsize,
pin::pin,
task::{Context, Poll, Waker},
};
use crate::retrieve::{AsyncFusedRead, Fusable, Rooted};
fn block_on<F: Future>(future: F) -> F::Output {
let mut future = pin!(future);
let mut cx = Context::from_waker(Waker::noop());
loop {
if let Poll::Ready(value) = future.as_mut().poll(&mut cx) {
return value;
}
}
}
fn depth(n: usize) -> NonZeroUsize {
NonZeroUsize::new(n).expect("the fixture depths are not zero")
}
struct Shots;
struct ShotsOptions;
struct ShotsQuestion;
impl Fusable for Shots {
type Options = ShotsOptions;
type Question = ShotsQuestion;
}
impl<E> AsyncFusedRead<E> for Shots {
type Ctx<'a>
= &'a [(u32, u64)]
where
E: 'a;
type Error = Infallible;
type Evidence = f32;
type Root = u32;
fn keys<'a>(
ctx: Self::Ctx<'a>,
_options: Option<&Self::Options>,
_question: &Self::Question,
limit: NonZeroUsize,
) -> impl Future<Output = Result<Vec<Rooted<Self::Root, Self::Evidence>>, Self::Error>>
where
E: 'a,
{
let rows = ctx
.iter()
.take(limit.get())
.map(|&(root, rank)| Rooted::new(root, rank, 1.0_f32))
.collect();
core::future::ready(Ok(rows))
}
}
struct Captions;
struct CaptionsOptions;
struct CaptionsQuestion;
impl Fusable for Captions {
type Options = CaptionsOptions;
type Question = CaptionsQuestion;
}
impl<E> AsyncFusedRead<E> for Captions {
type Ctx<'a>
= &'a [(u32, u64)]
where
E: 'a;
type Error = Infallible;
type Evidence = f32;
type Root = u32;
fn keys<'a>(
ctx: Self::Ctx<'a>,
_options: Option<&Self::Options>,
_question: &Self::Question,
limit: NonZeroUsize,
) -> impl Future<Output = Result<Vec<Rooted<Self::Root, Self::Evidence>>, Self::Error>>
where
E: 'a,
{
let rows = ctx
.iter()
.take(limit.get())
.map(|&(root, rank)| Rooted::new(root, rank, 2.0_f32))
.collect();
core::future::ready(Ok(rows))
}
}
struct Journal;
fn ask_vector<M, E>(
ctx: M::Ctx<'_>,
options: &M::Options,
question: &M::Question,
) -> Vec<Rooted<u32, f32>>
where
M: Fusable + AsyncFusedRead<E, Root = u32, Evidence = f32, Error = Infallible>,
{
block_on(M::keys(ctx, Some(options), question, depth(10)))
.expect("the fixture member refuses nothing")
}
#[test]
fn a_hand_written_vector_member_answers_through_the_projection() {
let rows = ask_vector::<Shots, ()>(&[(7, 1), (9, 2)], &ShotsOptions, &ShotsQuestion);
assert_eq!(rows.len(), 2);
assert_eq!(rows[0].root_ref(), &7);
assert_eq!(rows[0].rank(), 1);
}
#[test]
fn two_modalities_travel_one_road_and_fuse_over_one_root() {
let vector = block_on(<Shots as AsyncFusedRead<()>>::keys(
&[(7, 1), (9, 2)],
Some(&ShotsOptions),
&ShotsQuestion,
depth(10),
))
.expect("the fixture refuses nothing");
let fulltext = block_on(<Captions as AsyncFusedRead<()>>::keys(
&[(9, 1), (11, 2)],
Some(&CaptionsOptions),
&CaptionsQuestion,
depth(10),
))
.expect("the fixture refuses nothing");
let fused = crate::retrieve::fuse_weighted::<u32, f32, crate::retrieve::HashJoin<u32>>(
vec![(1.0, vector), (1.0, fulltext)],
crate::retrieve::Fusion::default(),
);
assert_eq!(
fused[0].root(),
9,
"the root both modalities agreed about did not lead: {fused:?}",
);
assert_eq!(
fused[0].evidence().len(),
2,
"the nested provenance lost a member: {fused:?}",
);
}
#[test]
fn the_seam_is_the_demand_and_a_member_without_one_has_no_package() {
const fn has_a_package<M: Fusable>() {}
has_a_package::<Shots>();
has_a_package::<Captions>();
let _ = Journal;
}