Files
swift-nio-imap/Sources/NIOIMAPCore/NIOIMAPCore.docc/ParsingAndEncoding.md
T
Daniel Eggert f45f5df879 Add encoder output types
IMAPBuffer / IMAPByteStorage (owner + range, no pointer), IMAPBufferList,
OutputSegment, the EncoderOutputBuffer sink protocol and its two
conformers: IMAPOutputBuffer (core, donates to DispatchData) and
ByteBufferOutput (NIOIMAP, fills a ByteBuffer in place).

StableAddressByteRegion is the public opt-in for a foreign region to be
donated by makeDispatchData() rather than copied: arrays conform, and
NIOIMAP conforms ByteBuffer with public API only (CoW keeps a held
copy's readable bytes in place).

makeDispatchData() donates only regions of at least
IMAPBuffer.dispatchDonationThreshold (512) bytes; each run of shorter
or unstable regions is copied into one malloc'd region freed inline
(.free). Measured: a donated region costs ~4 mallocs, 300-500 B and
~400 ns plus an async deallocator block; a copy costs a memcpy. The
deallocator captures the owner's concrete type, not an existential.

Public API for adapting a buffer type, so NIOIMAP's ByteBuffer support
needs no SPI and a third-party type can do the same:
IMAPBuffer.copyThreshold and dispatchDonationThreshold;
backingRegion(as:), which recovers the backing region and range without
copying; withUnsafeBytesAndOwner(_:), which hands out bytes with a fixed
address and their owner, under the same rule makeDispatchData() uses.
IMAPByteStorage.init(_:) has an overload for a StableAddressByteRegion,
which records the fixed address statically: the generic init isn't
specialized across modules, so its dynamic cast would run once per
wrapped ByteBuffer.

Additive; nothing uses them yet. Review focus: OutputSegment's
@unchecked Sendable (handed-out ranges never overlap the write tail),
IMAPOutputBuffer's copy-on-write state box and its
@exclusivity(unchecked) properties, and makeDispatchData()'s copy runs.
2026-11-24 09:47:33 +09:00

1.4 KiB

Parsing and Encoding

Parse IMAP server responses and encode client commands using these streaming parsers and encode buffers.

Overview

NIOIMAPCore provides comprehensive parsing and encoding capabilities. These types handle the conversion between the IMAP wire format and strongly-typed Swift structures, including error handling and advanced features like pipelining.

Topics

Parsers

Streaming parsers for different contexts.

  • ResponseParser
  • CommandParser
  • SynchronizingLiteralParser

Encoders

Buffers for encoding protocol elements back to wire format.

  • CommandEncodeBuffer
  • ResponseEncodeBuffer
  • CommandEncodingOptions
  • ResponseEncodingOptions

Bytes

Types for carrying raw message payloads without copying them.

  • IMAPBuffer
  • IMAPBufferList
  • IMAPByteStorage
  • ByteRegion
  • StableAddressByteRegion

Encoder output

Where encoded bytes go.

  • EncoderOutputBuffer
  • IMAPOutputBuffer

Pipelining

Types related to command pipelining behavior.

  • PipeliningRequirement
  • PipeliningBehavior

Errors

Parsing and encoding errors that may occur.

  • BadCommand
  • ParserError
  • TooMuchRecursion
  • ExceededMaximumMessageAttributesError
  • ExceededMaximumBodySizeError
  • ExceededLiteralSizeLimitError

Utilities

Utility types and protocol defaults.

  • ModifiedUTF7
  • IMAPDefaults