Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 26 additions & 10 deletions src/features/contacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ fn ensure_is_on_whatsapp_jids_supported(jids: &[Jid]) -> Result<()> {
Ok(())
}

/// Mapping extractors as fn items, NOT closures. A closure returning
/// references tied to its argument is inferred at a concrete lifetime, and
/// because its type is embedded in the public methods' future types, callers
/// that box those futures (`#[async_trait]`, `Box<dyn Future + Send>`) hit
/// "implementation of `FnOnce` is not general enough" (issue #825). Fn items
Comment on lines +27 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Make the extractor comment concise

AGENTS.md says, “When adding comments to the code, dont be so verbose, also only explain why, not what.” This new doc block explains the implementation mechanics and downstream compiler behavior in detail, including what fn items do, so it violates the local comment guidance and leaves commit/issue-level rationale in source. Please trim it to the minimal why, or move the longer explanation to external documentation.

Useful? React with 👍 / 👎.

/// implement `Fn` for every lifetime by construction.
fn forward_lid_pair(r: &IsOnWhatsAppResult) -> (&Jid, Option<&Jid>) {
(&r.jid, r.lid.as_ref())
}

fn reverse_lid_pair(r: &IsOnWhatsAppResult) -> Option<(&Jid, Option<&Jid>)> {
if r.jid.is_lid() {
r.pn_jid.as_ref().map(|pn| (pn, Some(&r.jid)))
} else {
None
}
}

fn user_info_lid_pair(entry: &UserInfo) -> (&Jid, Option<&Jid>) {
(&entry.jid, entry.lid.as_ref())
}

pub struct Contacts<'a> {
client: &'a Client,
}
Expand Down Expand Up @@ -112,16 +134,10 @@ impl<'a> Contacts<'a> {
results.extend(self.client.execute(spec).await?);
}

self.persist_lid_mappings(results.iter().map(|r| (&r.jid, r.lid.as_ref())))
self.persist_lid_mappings(results.iter().map(forward_lid_pair))
.await;
self.persist_lid_mappings(results.iter().filter_map(reverse_lid_pair))
.await;
self.persist_lid_mappings(results.iter().filter_map(|r| {
if r.jid.is_lid() {
r.pn_jid.as_ref().map(|pn| (pn, Some(&r.jid)))
} else {
None
}
}))
.await;

Ok(results)
}
Expand Down Expand Up @@ -189,7 +205,7 @@ impl<'a> Contacts<'a> {
let spec = UserInfoSpec::new(jids.to_vec(), request_id);

let info = self.client.execute(spec).await?;
self.persist_lid_mappings(info.values().map(|entry| (&entry.jid, entry.lid.as_ref())))
self.persist_lid_mappings(info.values().map(user_info_lid_pair))
.await;
Ok(info)
}
Expand Down
66 changes: 66 additions & 0 deletions tests/async_trait_boxed_future_compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! Compile-time regression guard for issue #825.
//!
//! Public async methods whose future type embeds a closure returning
//! references (e.g. an iterator adapter passed to a generic helper) fail with
//! "implementation of `FnOnce` is not general enough" ONLY when a downstream
//! caller boxes the future, which is exactly what `#[async_trait]` does. The
//! library's own tests never box that way, so this file reproduces the
//! consumer shape: if it compiles, the guard passes.

use async_trait::async_trait;
use std::sync::Arc;
use tokio::sync::RwLock;
use wacore_binary::Jid;
use whatsapp_rust::client::Client;

#[async_trait]
pub trait WhatsAppGateway: Send + Sync {
async fn check_phones(&self, phones: Vec<String>) -> Result<Vec<bool>, String>;
async fn statuses(&self, phones: Vec<String>) -> Result<Vec<Option<String>>, String>;
}

pub struct GatewayImpl {
client: RwLock<Option<Arc<Client>>>,
}

#[async_trait]
impl WhatsAppGateway for GatewayImpl {
async fn check_phones(&self, phones: Vec<String>) -> Result<Vec<bool>, String> {
let guard = self.client.read().await;
let client = guard.as_ref().expect("client").clone();
drop(guard);

let jids: Vec<Jid> = phones.iter().map(|p| Jid::pn(p.as_str())).collect();
let results = client
.contacts()
.is_on_whatsapp(&jids)
.await
.map_err(|e| e.to_string())?;
Ok(results.into_iter().map(|r| r.is_registered).collect())
}

async fn statuses(&self, phones: Vec<String>) -> Result<Vec<Option<String>>, String> {
let guard = self.client.read().await;
let client = guard.as_ref().expect("client").clone();
drop(guard);

let jids: Vec<Jid> = phones.iter().map(|p| Jid::pn(p.as_str())).collect();
let info = client
.contacts()
.get_user_info(&jids)
.await
.map_err(|e| e.to_string())?;
Ok(jids
.iter()
.map(|jid| info.get(jid).and_then(|i| i.status.clone()))
.collect())
}
}

/// The guard is the compilation itself; this just keeps the test binary
/// non-empty and the trait impl reachable.
#[test]
fn async_trait_consumers_compile() {
fn assert_impl<T: WhatsAppGateway>() {}
assert_impl::<GatewayImpl>();
}
Loading