Files

133 lines
5.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import struct NIO.ByteBuffer
/// A modification sequence value from RFC 7162 `CONDSTORE` extension.
///
/// A mod-sequence value is a 63-bit unsigned integer that tracks when a message or its flags last changed.
/// Servers that support the `CONDSTORE` extension assign and maintain mod-sequence values for each mailbox.
/// These values enable clients to efficiently resynchronize mailbox state by requesting only messages
/// modified since a known `MODSEQ` value.
///
/// Modification sequence values are monotonically increasing within a mailbox. They increase when:
/// - A message is added to the mailbox
/// - A message's flags are modified
/// - A message is expunged
///
/// The 63-bit constraint is enforced by the initializer; values must not exceed `Int64.max`.
///
/// - SeeAlso: [RFC 7162 CONDSTORE Extension](https://datatracker.ietf.org/doc/html/rfc7162)
public struct ModificationSequenceValue: Hashable, Sendable {
var value: UInt64
/// A zero `mod-sequence-value`
public static var zero: Self {
0
}
/// Creates a new ModificationSequenceValue.
/// - parameter value: The raw value.
public init(_ value: UInt64) {
precondition(value <= UInt64(Int64.max), "mod-sequence-values are 63-bit")
self.value = value
}
/// Creates a `ModificationSequenceValue` from some `BinaryInteger`, ensuring that the given value fits within the valid range `0...Int64.max`.
/// - parameter source: The raw value to use.
/// - returns: `nil` if `source` is not within the valid range.
@inlinable
public init?<T>(exactly source: T) where T: BinaryInteger {
guard
source >= 0,
let value = UInt64(exactly: source),
value <= UInt64(Int64.max)
else { return nil }
self.init(value)
}
}
// MARK: - ExpressibleByIntegerLiteral
extension ModificationSequenceValue: ExpressibleByIntegerLiteral {
/// A `ModificationSequenceValue` is defined as a 63-bit number. This means
/// that the IntegerLiteralType is best represented as `UInt64`.
public typealias IntegerLiteralType = UInt64
/// Creates a `ModificationSequenceValue` from an integer literal.
/// - parameter value: The literal value.
public init(integerLiteral value: UInt64) {
self.value = value
}
}
// MARK: - CustomDebugStringConvertible
extension ModificationSequenceValue: CustomDebugStringConvertible {
/// `value` as a `String`.
public var debugDescription: String {
"\(self.value)"
}
}
// MARK: - BinaryInteger
extension BinaryInteger {
public init(_ modificationSequenceValue: ModificationSequenceValue) {
self = Self(modificationSequenceValue.value)
}
}
// MARK: - Strideable
extension ModificationSequenceValue: Strideable {
/// Evaluates if one `ModificationSequenceValue` (`lhs`) is strictly less than another (`rhs`).
/// - parameter lhs: The first `ModificationSequenceValue` to evaluate.
/// - parameter rhs: The second `ModificationSequenceValue` to evaluate.
/// - returns: `true` if `lhs` strictly less than`rhs`, otherwise `false`.
public static func < (lhs: ModificationSequenceValue, rhs: ModificationSequenceValue) -> Bool {
lhs.value < rhs.value
}
/// Evaluates if one `ModificationSequenceValue` (`lhs`) is less than or equal to another (`rhs`).
/// - parameter lhs: The first `ModificationSequenceValue` to evaluate.
/// - parameter rhs: The second `ModificationSequenceValue` to evaluate.
/// - returns: `true` if `lhs` is less than or equal to `rhs`, otherwise `false`.
public static func <= (lhs: ModificationSequenceValue, rhs: ModificationSequenceValue) -> Bool {
lhs.value <= rhs.value
}
/// Gets the distance to the given `ModificationSequenceValue`.
/// - parameter other: The `ModificationSequenceValue` to get the distance to.
/// - returns: The distance.
public func distance(to other: ModificationSequenceValue) -> Int64 {
Int64(other.value) - Int64(self.value)
}
/// Advances the current ModificationSequenceValue by `n`.
/// - parameter n: How many to advance by.
/// - returns: A new `ModificationSequenceValue`.
public func advanced(by n: Int64) -> ModificationSequenceValue {
ModificationSequenceValue(UInt64(Int64(self.value) + n))
}
}
// MARK: - Encoding
extension EncodeBuffer {
@discardableResult mutating func writeModificationSequenceValue(_ value: ModificationSequenceValue) -> Int {
self.writeString("\(value.value)")
}
}