Open Mattsi-Jansky opened 1 year ago
And if I may ask as an aside, why use so many generic lifetime parameters anyway? That's a genuine question, not intended as a criticism, I'm just trying to learn more about Rust patterns. It seems like if the models didn't use str and Cow they wouldn't need explicit lifetimes. So what was the intention behind using Cow and str rather than Box or Arc and String? Was it to reduce allocations?
It's been a while since I've touched this project but IIRC yes! the intention was that if you don't explicitly need to store heap-allocated strings in the blocks then they won't force you to - since they never actually manipulate your text
Have I understood this right? Is there no way to use blox! without static lifetimes?
hmm.. I'm fairly confident your usecase should be possible, and hopefully without having to think too much about lifetimes yourself - could you try moving the block expression to a local and providing the inferred type?
a la
let result = listener.next().await;
// expected `___`, found `()`
let blocks: () = vec![Block::Section(blox! {
<section_block>
<text kind=plain>"test"</text>
</section_block>
})];
assert_eq!(
result.unwrap(),
SocketMessage::Event {
envelope_id: String::from("fake-enve-lope-i-d"),
payload: Payload {
event: Event {
id: "1686321337.206879".to_string(),
event_type: "message".to_string(),
text: Some("test".to_string()),
user: Some("F4K3USER1D".to_string()),
blocks,
channel: Some("F4K3CH4NN3L1D".to_string()),
channel_type: Some("im".to_string()),
}
}
}
);
I thought I'd try swap out
slack-morphism
's slack blocks forslack-block-rs
but I hit a bit of a snag. I'll start with the code producing the issue, though most details aren't significant:It's a simple test with a bunch of setup. The important part is that we're getting a result from a socket (
listener.next()
) and comparing it to an expected result, part of which is built usingblox!
. That results in this compile error:It's a weird compile error. It is definitely caused by
blox!
. If I replace the vector withvec![Block::Divider]
, it works. Using a builder also works.If I've understood what is happening here correctly, I think it is that:
SectionBlock
relies on lifetimes andmox
is automatically providing a'static
lifetime. Which makes sense, because we're providing this data statically. Butresult
is not using static lifetimes. And I'll often be receiving data from sockets that will only last a short time, certainly not until the end of the program.Have I understood this right? Is there no way to use
blox!
without static lifetimes?And if I may ask as an aside, why use so many generic lifetime parameters anyway? That's a genuine question, not intended as a criticism, I'm just trying to learn more about Rust patterns. It seems like if the models didn't use
str
andCow
they wouldn't need explicit lifetimes. So what was the intention behind usingCow
andstr
rather thanBox
orArc
andString
? Was it to reduce allocations?