Skip to content

remote-derive: optional as_shared override on derive(ProtoBytes)#294

Merged
iainmcgin merged 2 commits into
mainfrom
iain/remote-derive-as-shared
Jul 9, 2026
Merged

remote-derive: optional as_shared override on derive(ProtoBytes)#294
iainmcgin merged 2 commits into
mainfrom
iain/remote-derive-as-shared

Conversation

@iainmcgin

Copy link
Copy Markdown
Collaborator

Follow-up to #284, stacked on iain/vectored-encode (it needs ProtoBytes::as_shared, which only exists there). Retarget to main if #284 lands first.

Problem

buffa-remote-derive (#251) generates the newtype's only permitted ProtoBytes impl, and that impl inherits the trait's as_shared default of None. So a remote bytes newtype — even one wrapping bytes::Bytes — always takes the copy path into a segmented (Rope) sink, and the user cannot override it: a manual impl alongside the derive is a coherence error (E0119), and the #[buffa(...)] grammar had no key for it. Output bytes are identical either way, so the missed zero-copy is silent.

Change

#[buffa(remote = ..., as_shared = path)] now generates the hook:

#[derive(Clone, PartialEq, Default, Debug, buffa_remote_derive::ProtoBytes)]
#[buffa(remote = bytes::Bytes, as_shared = share::handle)]
pub struct SharedBlob(pub bytes::Bytes);

mod share {
    pub fn handle(b: &bytes::Bytes) -> Option<bytes::Bytes> {
        Some(b.clone())
    }
}

The path is called as a free function on the wrapped field (path(&self.0) / path(&self.field)), shape fn(&Remote) -> Option<Bytes>; a signature mismatch is a type error at the generated call site. Absent the key, nothing is generated and the trait default applies — existing derives produce byte-identical output. This mirrors the existing parse_with_overrides mechanism used by ProtoBox/MapStorage, in a second mode: an opt-in hook with no default rather than a renamed inherent-method default (the helper's doc now describes both modes).

Testing

  • Unit tests: default stays None; override returns the wrapped handle (pointer equality); end-to-end Rope splice through put_shared_bytes_field (payload segment pointer-equal to the source Bytes, byte parity vs a contiguous sink; payload sized 2 * DEFAULT_MIN_SEGMENT so the splice threshold can move without breaking the test); named-field newtype shape.
  • Driven end-to-end from a scratch consumer: bytes_type_custom message encode into a Rope splices zero-copy; typo'd key and wrong-signature callable both produce legible errors.

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

Base automatically changed from iain/vectored-encode to main July 9, 2026 02:30
iainmcgin added 2 commits July 8, 2026 20:11
The generated ProtoBytes impl previously always inherited the trait's
as_shared default of None, so a remote bytes newtype - even one wrapping
bytes::Bytes - silently took the copy path when encoding into a
segmented (Rope) sink, with no way to override from within the derive
(the impl is owned by the derive, so a manual one is a coherence error).

#[buffa(remote = ..., as_shared = path)] now generates the hook, calling
the named path as a free function on the wrapped field
(fn(&Remote) -> Option<Bytes>). Absent the key, nothing is generated and
the trait default still applies, so existing derives produce identical
output. Unknown keys are still rejected, with as_shared added to the
error message's key list.

Tests cover the default (None), the override returning the wrapped
handle (pointer equality), an end-to-end Rope splice through
put_shared_bytes_field with parity against a contiguous sink, and the
named-field newtype shape.

:house: Remote-Dev: homespace
@iainmcgin
iainmcgin force-pushed the iain/remote-derive-as-shared branch from 67e2618 to 2687ba3 Compare July 9, 2026 03:12
@iainmcgin
iainmcgin marked this pull request as ready for review July 9, 2026 03:17
@iainmcgin
iainmcgin enabled auto-merge July 9, 2026 03:17
@iainmcgin
iainmcgin requested a review from azdagron July 9, 2026 03:18
@iainmcgin
iainmcgin added this pull request to the merge queue Jul 9, 2026
Merged via the queue into main with commit 43fd849 Jul 9, 2026
9 checks passed
@iainmcgin
iainmcgin deleted the iain/remote-derive-as-shared branch July 9, 2026 13:18
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants