+2
-13
README.md
+2
-13
README.md
······
+3
-9
crates/jacquard-api/src/app_bsky/feed/get_timeline.rs
+3
-9
crates/jacquard-api/src/app_bsky/feed/get_timeline.rs
······+serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq, jacquard_derive::IntoStatic,···
+146
-132
crates/jacquard-common/src/lib.rs
+146
-132
crates/jacquard-common/src/lib.rs
···-//! strings and other data directly from the response buffer instead of allocating owned copies.-//! This is great for performance, but it creates some interesting challenges when combined with+//! Jacquard has a couple of `.send()` methods. One is stateless. it's the output of a method that creates a request builder, implemented as an extension trait, `XrpcExt`, on any http client which implements a very simple HttpClient trait. You can use a bare `reqwest::Client` to make XRPC requests. You call `.xrpc(base_url)` and get an `XrpcCall` struct. `XrpcCall` is a builder, which allows you to pass authentication, atproto proxy settings, labeler headings, and set other options for the final request. There's also a similar trait `DpopExt` in the `jacquard-oauth` crate, which handles that form of authenticated request in a similar way. For basic stuff, this works great, and it's a useful building block for more complex logic, or when one size does **not** in fact fit all.+//! The other, `XrpcClient`, is stateful, and can be implemented on anything with a bit of internal state to store the base URI (the URL of the PDS being contacted) and the default options. It's the one you're most likely to interact with doing normal atproto API client stuff. The Agent struct in the initial example implements that trait, as does the session struct it wraps, and the `.send()` method used is that trait method.+//! >`XrpcClient` implementers don't *have* to implement token auto-refresh and so on, but realistically they *should* implement at least a basic version. There is an `AgentSession` trait which does require full session/state management.+//! Here is the entire text of `XrpcCall::send()`. [`build_http_request()`](https://tangled.org/@nonbinary.computer/jacquard/blob/main/crates/jacquard-common/src/xrpc.rs#L400) and [`process_response()`](https://tangled.org/@nonbinary.computer/jacquard/blob/main/crates/jacquard-common/src/xrpc.rs#L344) are public functions and can be used in other crates. The first does more or less what it says on the tin. The second does less than you might think. It mostly surfaces authentication errors at an earlier level so you don't have to fully parse the response to know if there was an error or not.+//! >A core goal of Jacquard is to not only provide an easy interface to atproto, but to also make it very easy to build something that fits your needs, and making "helper" functions like those part of the API surface is a big part of that, as are "stateless" implementations like `XrpcExt` and `XrpcCall`.-//! The `for<'any>` bound says "this type must implement `XrpcRequest` for *every possible lifetime*",-//! which is effectively the same as requiring `DeserializeOwned`. You've just thrown away your-//! zero-copy optimization, and this also won't work on most of the types in jacquard. The vast+//! `.send()` works for any endpoint and any type that implements the required traits, regardless of what crate it's defined in. There's no `KnownRecords` enum which defines a complete set of known records, and no restriction of Service endpoints in the agent/client, or anything like that, nothing that privileges any set of lexicons or way of working with the library, as much as possible. There's one primary method and you can put pretty much anything relevant into it. Whatever atproto API you need to call, just `.send()` it. Okay there are a couple of additional helpers, but we're focusing on the core one, because pretty much everything else is just wrapping the above `send()` in one way or another, and they use the same pattern.-//! It gets worse with async. If you want to return borrowed data from an async method, where does-//! consumed by the HTTP call. You end up with "cannot infer appropriate lifetime" errors or even-//! more confusing errors because the compiler can't prove the buffer will stay alive. You *could*-//! do some lifetime laundering with `unsafe`, but you don't actually *need* to tell rustc to "trust+//! So how does this work? How does `send()` and its helper functions know what to do? The answer shouldn't be surprising to anyone familiar with Rust. It's traits! Specifically, the following traits, which have generated implementations for every lexicon type ingested by Jacquard's API code generation, but which honestly aren't hard to just implement yourself (more tedious than anything). XrpcResp is always implemented on a unit/marker struct with no fields. They provide all the request-specific instructions to the functions.-//! The fix is to use Generic Associated Types (GATs) on the trait's associated types, while keeping+//! Here are the implementations for [`GetTimeline`](https://tangled.org/@nonbinary.computer/jacquard/blob/main/crates/jacquard-api/src/app_bsky/feed/get_timeline.rs). You'll also note that `send()` doesn't return the fully decoded response on success. It returns a Response struct which has a generic parameter that must implement the XrpcResp trait above. Here's its definition. It's essentially just a cheaply cloneable byte buffer and a type marker.+//! ) -> Result<<Resp as XrpcResp>::Output<'static>, XrpcError<<Resp as XrpcResp>::Err<'static>>>+//! You decode the response (or the endpoint-specific error) out of this, borrowing from the buffer or taking ownership so you can drop the buffer. There are two reasons for this. One is separation of concerns. By two-staging the parsing, it's easier to distinguish network and authentication problems from application-level errors. The second is lifetimes and borrowed deserialization.-//! // preferences union, so that if you implement a similar lexicon type in your AppView or App+//! Jacquard is designed around zero-copy/borrowed deserialization: types like [`Post<'a>`](https://tangled.org/@nonbinary.computer/jacquard/blob/main/crates/jacquard-api/src/app_bsky/feed/post.rs) can borrow strings and other data directly from the response buffer instead of allocating owned copies. This is great for performance, but it creates some interesting challenges, especially in async contexts. So how do you specify the lifetime of the borrow?-//! The compiler can monomorphize for concrete lifetimes instead of trying to prove bounds hold+//! This looks reasonable until you try to use it in a generic context. If you have a function that works with *any* lifetime, you need a Higher-ranked trait bound:-//! This lets async methods return a `Response` that owns its buffer, then the *caller* decides+//! The `for<'any>` bound says "this type must implement `XrpcRequest` for *every possible lifetime*", which, for `Deserialize`, is effectively the same as requiring `DeserializeOwned`. You've probably just thrown away your zero-copy optimization, and furthermore that trait bound just straight-up won't work on most of the types in Jacquard. The vast majority of them have either a custom Deserialize implementation which will borrow if it can, a `#[serde(borrow)]` attribute on one or more fields, or an equivalent lifetime bound attribute, associated with the Deserialize derive macro. You will get "Deserialize implementation not general enough" if you try. And no, you cannot have an additional deserialize implementation for the `'static` lifetime due to how serde works.+//! If you instead try something like the below function signature and specify a specific lifetime, it will compile in isolation, but when you go to use it, the Rust compiler will not generally be able to figure out the lifetimes at the call site, and will complain about things being dropped while still borrowed, even if you convert the response to an owned/ `'static` lifetime version of the type.+//!fn parse<'s, R: XrpcRequest<'s>>(response: &'s [u8]) ... // return type with the same lifetime-//! The async method doesn't need to know or care about lifetimes - it just returns the `Response`.-//! The caller gets full control over whether to use borrowed or owned data. It can even decide-//! after the fact that it doesn't want to parse out the API response type that it asked for. Instead-//! it can call `.parse_data()` or `.parse_raw()` on the response to get loosely typed, validated+//! It gets worse with async. If you want to return borrowed data from an async method, where does the lifetime come from? The response buffer needs to outlive the borrow, but the buffer is consumed or potentially has to have an unbounded lifetime. You end up with confusing and frustrating errors because the compiler can't prove the buffer will stay alive or that you have taken ownership of the parts of it you care about. You *could* do some lifetime laundering with `unsafe`, but that road leads to potential soundness issues, and besides, you don't actually *need* to tell `rustc` to "trust me, bro", you can, with some cleverness, explain this to the compiler in a way that it can reason about perfectly well.+//! The fix is to use Generic Associated Types (GATs) on the trait's associated types, while keeping the trait itself lifetime-free:+//!Now you can write trait bounds without HRTBs, and with lifetime bounds that are actually possible for Jacquard's borrowed deserializing types to meet:+//!// This is part of a trait from jacquard itself, used to genericize updates to things like the Bluesky+//!// preferences union, so that if you implement a similar lexicon type in your app, you don't have+//!// to special-case it. Instead you can do a relatively simple trait implementation and then call+//!// .update_vec() with a modifier function or .update_vec_item() with a single item you want to set.+//!The compiler can monomorphize for concrete lifetimes instead of trying to prove bounds hold for *all* lifetimes at once, or struggle to figure out when you're done with a buffer. `XrpcResp` being separate and lifetime-free lets async methods like `.send()` return a `Response` that owns the response buffer, and then the *caller* decides the lifetime strategy:+//! The async method doesn't need to know or care about lifetimes for the most part - it just returns the `Response`. The caller gets full control over whether to use borrowed or owned data. It can even decide after the fact that it doesn't want to parse out the API response type that it asked for. Instead it can call `.parse_data()` or `.parse_raw()` on the response to get loosely typed, validated data or minimally typed maximally accepting data values out.
+1
-21
crates/jacquard-common/src/xrpc.rs
+1
-21
crates/jacquard-common/src/xrpc.rs
···
+17
-14
crates/jacquard/Cargo.toml
+17
-14
crates/jacquard/Cargo.toml
···api_full = ["api", "jacquard-api/bluesky", "jacquard-api/other", "jacquard-api/lexicon_community"]···
+6
-7
crates/jacquard/src/client.rs
+6
-7
crates/jacquard/src/client.rs
···············
-2
crates/jacquard/src/lib.rs
-2
crates/jacquard/src/lib.rs
···
+11
-4
examples/create_whitewind_post.rs
+11
-4
examples/create_whitewind_post.rs
·········
+4
-10
examples/post_with_image.rs
+4
-10
examples/post_with_image.rs
············
+1
-1
examples/public_atproto_feed.rs
+1
-1
examples/public_atproto_feed.rs
···
examples/read_whitewind_posts.rs
examples/read_whitewind_post.rs
examples/read_whitewind_posts.rs
examples/read_whitewind_post.rs
+2
-2
justfile
+2
-2
justfile
···