Skip to content

Commit

Permalink
Removed: Guarantee (#62).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Aug 7, 2024
1 parent 33a41f5 commit 4578c6d
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 99 deletions.
48 changes: 0 additions & 48 deletions Sources/CoreKit/Guarantee+Validation.swift

This file was deleted.

45 changes: 0 additions & 45 deletions Sources/CoreKit/Guarantee.swift

This file was deleted.

45 changes: 44 additions & 1 deletion Sources/CoreKit/Models/Guarantees/Finite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@
/// init(unchecked:) // error: unchecked
/// ```
///
@frozen public struct Finite<Value>: Equatable, Guarantee where Value: BinaryInteger {
@frozen public struct Finite<Value>: Equatable where Value: BinaryInteger {

public typealias Value = Value

//=------------------------------------------------------------------------=
// MARK: Metadata
//=------------------------------------------------------------------------=

/// Indicates whether the given `value` can be trusted.
///
/// - Returns: `value ∈ ℤ`
///
@inlinable public static func predicate(_ value: /* borrowing */ Value) -> Bool {
!value.isInfinite // await borrowing fix
}
Expand All @@ -44,16 +50,53 @@
// MARK: Initializers
//=------------------------------------------------------------------------=

/// Creates a new instance without validation in release mode.
///
/// - Requires: `value ∈ ℤ`
///
/// - Warning: Only use this method when you know the `value` is valid.
///
@_disfavoredOverload // collection.map(Self.init)
@inlinable public init(unchecked value: consuming Value) {
Swift.assert(Self.predicate(value), String.brokenInvariant())
self.value = value
}

/// Creates a new instance by trapping on failure.
///
/// - Requires: `value ∈ ℤ`
///
@inlinable public init(_ value: consuming Value) {
precondition(Self.predicate(value), String.brokenInvariant())
self.value = value
}

/// Creates a new instance by returning `nil` on failure.
///
/// - Requires: `value ∈ ℤ`
///
@inlinable public init?(exactly value: consuming Value) {
guard Self.predicate(value) else { return nil }
self.value = value
}

/// Creates a new instance by throwing the `error()` on failure.
///
/// - Requires: `value ∈ ℤ`
///
@inlinable public init<Failure>(
_ value: consuming Value,
prune error: @autoclosure () -> Failure
) throws where Failure: Swift.Error {
guard Self.predicate(value) else { throw error() }
self.value = value
}

//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=

/// The `magnitude` of `self`.
@inlinable public consuming func magnitude() -> Finite<Value.Magnitude> {
Finite<Value.Magnitude>(unchecked: self.value.magnitude())
}
Expand Down
48 changes: 45 additions & 3 deletions Sources/CoreKit/Models/Guarantees/Natural.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@
/// however, that the inverse case is not as simple. `U8(255)` is natural,
/// for example, but it becomes negative when you reinterpret it as `I8(-1)`.
///
@frozen public struct Natural<Value>: Equatable, Guarantee where Value: BinaryInteger {
@frozen public struct Natural<Value>: Equatable where Value: BinaryInteger {

public typealias Value = Value

//=------------------------------------------------------------------------=
// MARK: Metadata
//=------------------------------------------------------------------------=

/// Indicates whether the given `value` can be trusted.
///
/// - Returns: `value ∈ ℕ`
///
@inlinable public static func predicate(_ value: /* borrowing */ Value) -> Bool {
!Bool(value.appendix) // await borrowing fix
}
Expand All @@ -51,17 +57,53 @@
// MARK: Initializers
//=------------------------------------------------------------------------=

@_disfavoredOverload // enables: elements.map(Self.init)
/// Creates a new instance without validation in release mode.
///
/// - Requires: `value ∈ ℕ`
///
/// - Warning: Only use this method when you know the `value` is valid.
///
@_disfavoredOverload // collection.map(Self.init)
@inlinable public init(unchecked value: consuming Value) {
Swift.assert(Self.predicate(value), String.brokenInvariant())
self.value = value
}

/// Creates a new instance by trapping on failure.
///
/// - Requires: `value ∈ ℕ`
///
@inlinable public init(_ value: consuming Value) {
precondition(Self.predicate(value), String.brokenInvariant())
self.value = value
}

/// Creates a new instance by returning `nil` on failure.
///
/// - Requires: `value ∈ ℕ`
///
@inlinable public init?(exactly value: consuming Value) {
guard Self.predicate(value) else { return nil }
self.value = value
}

/// Creates a new instance by throwing the `error()` on failure.
///
/// - Requires: `value ∈ ℕ`
///
@inlinable public init<Failure>(
_ value: consuming Value,
prune error: @autoclosure () -> Failure
) throws where Failure: Swift.Error {
guard Self.predicate(value) else { throw error() }
self.value = value
}

//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=

/// The magnitude of this value.
/// The `magnitude` of `self`.
///
/// - Note: This is a bit cast because `self ∈ ℕ → unsigned`.
///
Expand Down
46 changes: 45 additions & 1 deletion Sources/CoreKit/Models/Guarantees/Nonzero.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@
/// init(unchecked:) // error: unchecked
/// ```
///
@frozen public struct Nonzero<Value>: BitCastable, Equatable, Guarantee where Value: BinaryInteger {
@frozen public struct Nonzero<Value>: BitCastable, Equatable where Value: BinaryInteger {

public typealias Value = Value

public typealias BitPattern = Nonzero<Value.Magnitude>

//=------------------------------------------------------------------------=
// MARK: Metadata
//=------------------------------------------------------------------------=

/// Indicates whether the given `value` can be trusted.
///
/// - Returns: `value ≠ 0`
///
@inlinable public static func predicate(_ value: /* borrowing */ Value) -> Bool {
!value.isZero // await borrowing fix
}
Expand All @@ -46,12 +52,48 @@
// MARK: Initializers
//=------------------------------------------------------------------------=

/// Creates a new instance without validation in release mode.
///
/// - Requires: `value ≠ 0`
///
/// - Warning: Only use this method when you know the `value` is valid.
///
@_disfavoredOverload // collection.map(Self.init)
@inlinable public init(unchecked value: consuming Value) {
Swift.assert(Self.predicate(value), String.brokenInvariant())
self.value = value
}

/// Creates a new instance by trapping on failure.
///
/// - Requires: `value ≠ 0`
///
@inlinable public init(_ value: consuming Value) {
precondition(Self.predicate(value), String.brokenInvariant())
self.value = value
}

/// Creates a new instance by returning `nil` on failure.
///
/// - Requires: `value ≠ 0`
///
@inlinable public init?(exactly value: consuming Value) {
guard Self.predicate(value) else { return nil }
self.value = value
}

/// Creates a new instance by throwing the `error()` on failure.
///
/// - Requires: `value ≠ 0`
///
@inlinable public init<Failure>(
_ value: consuming Value,
prune error: @autoclosure () -> Failure
) throws where Failure: Swift.Error {
guard Self.predicate(value) else { throw error() }
self.value = value
}

//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
Expand All @@ -68,10 +110,12 @@
// MARK: Transformations
//=------------------------------------------------------------------------=

/// The `complement` of `self`.
@inlinable public consuming func complement() -> Nonzero<Value> {
Self(unchecked: self.value.complement())
}

/// The `magnitude` of `self`.
@inlinable public consuming func magnitude() -> Nonzero<Value.Magnitude> {
Nonzero<Value.Magnitude>(unchecked: self.value.magnitude())
}
Expand Down
42 changes: 41 additions & 1 deletion Sources/CoreKit/Models/Guarantees/Shift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/// init(unchecked:) // error: unchecked
/// ```
///
@frozen public struct Shift<Target>: Equatable, Guarantee where Target: UnsignedInteger {
@frozen public struct Shift<Target>: Equatable where Target: UnsignedInteger {

public typealias Target = Target

Expand All @@ -34,6 +34,10 @@
// MARK: Metadata
//=------------------------------------------------------------------------=

/// Indicates whether the given `value` can be trusted.
///
/// - Returns: `value ∈ 0 up to Value.size`
///
@inlinable public static func predicate(_ value: borrowing Value) -> Bool {
value < Target.size
}
Expand Down Expand Up @@ -77,12 +81,48 @@
// MARK: Initializers
//=------------------------------------------------------------------------=

/// Creates a new instance without validation in release mode.
///
/// - Requires: `value ∈ 0 up to Value.size`
///
/// - Warning: Only use this method when you know the `value` is valid.
///
@_disfavoredOverload // collection.map(Self.init)
@inlinable public init(unchecked value: consuming Value) {
Swift.assert(Self.predicate(value), String.brokenInvariant())
self.value = value
}

/// Creates a new instance by trapping on failure.
///
/// - Requires: `value ∈ 0 up to Value.size`
///
@inlinable public init(_ value: consuming Value) {
precondition(Self.predicate(value), String.brokenInvariant())
self.value = value
}

/// Creates a new instance by returning `nil` on failure.
///
/// - Requires: `value ∈ 0 up to Value.size`
///
@inlinable public init?(exactly value: consuming Value) {
guard Self.predicate(value) else { return nil }
self.value = value
}

/// Creates a new instance by throwing the `error()` on failure.
///
/// - Requires: `value ∈ 0 up to Value.size`
///
@inlinable public init<Failure>(
_ value: consuming Value,
prune error: @autoclosure () -> Failure
) throws where Failure: Swift.Error {
guard Self.predicate(value) else { throw error() }
self.value = value
}

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=
Expand Down

0 comments on commit 4578c6d

Please sign in to comment.