concat method

KeyExpr concat(
  1. String right
)

Returns a new key expression with right appended, with no separator.

KeyExpr('FOO').concat('BAR') is FOOBAR. Prefer join where a / belongs between the two: zenoh can exploit the hierarchical separation it inserts.

right crosses to native as a pointer and a byte length, so an interior NUL is carried rather than truncating the operand. An empty right is legal and returns a copy of this expression.

The result is an owned key expression the caller must dispose. This receiver is unaffected.

Throws ZenohException if the composition is invalid — notably -128 for concatenating an expression starting with * onto one ending with *, which canon forbids outright, and for a right operand that is not itself composable. Throws StateError if this key expression is no longer alive.

Implementation

KeyExpr concat(String right) {
  final loaned = loanedKeyExpr;
  final encoded = utf8.encode(right);
  // Never NULL, even for an empty right: canon builds a Rust slice from
  // this pointer and slice::from_raw_parts(NULL, 0) is undefined behaviour.
  final rightPtr = _copyToNative(encoded);
  final slot = calloc.allocate<Void>(bindings.zd_keyexpr_sizeof());
  try {
    final rc = bindings.zd_keyexpr_concat(
      slot.cast(),
      loaned.cast(),
      rightPtr.cast(),
      encoded.length,
    );
    if (rc != 0) {
      calloc.free(slot);
      throw ZenohException('Failed to concatenate key expression', rc);
    }
    return KeyExpr._owned(slot);
  } finally {
    malloc.free(rightPtr);
  }
}