Skip to content

Discuss shared broadcast receives without a Clone bound #312

Description

@tisonkun

Problem

Both bounded and unbounded broadcast currently require T: Clone at construction and return T from recv / try_recv. Multiple subscribers need access to each published value, but requiring an owned T for every receive is an API choice rather than a requirement of the backing storage.

The shared backlog already stores VecDeque<Arc<T>>. Receiving obtains an Arc<T>, and take_msg converts it to T: it clones the payload when needed and attempts to move it out when the message is reclaimed. Payload cloning and destruction happen outside channel locks.

Callers can already use T = Arc<State> without requiring State: Clone, but the internal representation then becomes Arc<Arc<State>>. Exposing shared ownership of the existing message could avoid the additional Arc layer.

This issue is an open design discussion. The API below is a candidate, not an accepted implementation specification.

Candidate: add shared receives and preserve owning receives

Relax the T: Clone bound on both constructors. On both receiver types, add methods available for any T:

pub async fn recv_shared(&mut self) -> Result<Arc<T>, RecvError>;
pub fn try_recv_shared(&mut self) -> Result<Arc<T>, TryRecvError>;

Keep the existing recv and try_recv methods returning T, with T: Clone required only for those methods.

The two receive forms would consume from the same subscription cursor. Choosing a return representation would neither create a new subscription nor deliver the same message twice to one subscription. Bounded and unbounded channels should expose consistent APIs.

Compatibility

  • Relaxing constructor bounds preserves existing calls and permits non-Clone payloads.
  • Adding shared receive methods preserves the existing owning API. As with any new Rust inherent method, a name collision with a downstream extension trait is a possible compatibility edge case; see the Cargo guidance.
  • Changing the existing recv / try_recv return type to Arc<T>, or renaming the old methods to give their names to shared receives, breaks existing callers.
  • Preserve the existing move-out optimization. Implementing owning receives as an unconditional clone of a shared receive would lose the current guarantee that a channel with a single receiver does not clone its payload.
  • At this discussion's starting point, unbounded broadcast is present in v0.7.2, while bounded broadcast was added for the next release in feat(broadcast): add a bounded MPMC broadcast channel #253. Changing only the unreleased bounded API would not break a released API, but would leave the two channel variants inconsistent.

Semantics and tradeoffs

Shared receives must preserve message ordering, cancellation safety, disconnection behavior, and capacity accounting. Reclaimed bounded capacity must be released before running payload clone or drop callbacks.

Once every subscription has consumed a message or been dropped, the message can leave the backlog and release its queue capacity. An Arc<T> retained by a caller can nevertheless keep the payload allocation alive. Bounded capacity would limit retained backlog messages, not all memory held by completed receives.

A shared result gives subscribers ownership of the same allocation. An owning result gives them a T according to that type's Clone semantics, which need not be a deep copy. These are useful distinct contracts, but exposing both adds API surface and commits the shared path to Arc-based ownership.

Keeping the current API and asking callers to choose T = Arc<State> remains an alternative. It keeps the public API smaller at the cost of the extra Arc layer. Replacing owning receives with shared receives gives one default ownership model, but requires an explicit breaking-change decision.

Questions to resolve

  1. Is avoiding payload Clone requirements and nested Arc storage worth adding a permanent shared receive API?
  2. Are recv_shared / try_recv_shared the right names and the right place to express the ownership choice?
  3. Should we adopt the additive design, retain the current caller-selected Arc approach, or consider a shared-by-default API in a future incompatible release?

If the additive design is selected, validation should cover non-Clone payload fan-out, mixed shared and owning receives on one cursor, cancellation, bounded capacity release while returned Arcs remain alive, and preservation of existing owning-receive behavior.

Related: #311 narrows Watch's constructor bound without changing its return-value ownership model. Part of the channel-family discussion in #206.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions