From 5320c23e3484c47ef68e8af61dc4d76a23493aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Mon, 26 Aug 2024 07:54:04 +0200 Subject: [PATCH] [README] Call standard library code with Interoperable (#74). --- README.md | 16 ++++++++++++++++ Sources/CoreKit/Interoperable+Yield.swift | 2 +- Sources/CoreKit/Interoperable.swift | 4 ++-- Sources/RandomIntKit/FuzzerInt.swift | 4 +--- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a5e923cb..9e5d08af 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ - [Generic logic gates with BitOperable](#corekit-bitwise-logic) - [Three-way comparisons return Signum](#corekit-signum) - [Division by multiplication with Divider\](#corekit-divider) + - [Call standard library code with Interoperable](#corekit-interoperable) * [DoubleIntKit](#doubleintkit) - [A big systems integer](#doubleintkit-systems-integer) - [A non-recursive model](#doubleintkit-non-recursive-model) @@ -476,6 +477,21 @@ You know how the compiler sometimes replaces division with multiplication? Well, now you can be a wizard too! Divider\ finds same-size magic constants and replaces division with: multiplication, addition, and shifts. + + +#### Call standard library code with Interoperable + +Types conforming to Interoperable have a standard-library-compatible representation, +which is determined by their associated Stdlib type. I32 yields Int32 and U64 yields UInt64, +for example. Interoperable requires bidirectional consuming conversions: *init(\_:)* +and *stdlib()*. It then derives mutating read and modify accessors available through +*stdlib*. You may use these operations to call standard library code, as seen below: + +```swift +var randomness = RandomInt() // from RandomIntKit +let random = Bool.random(using: &randomness.stdlib) +``` + ## DoubleIntKit diff --git a/Sources/CoreKit/Interoperable+Yield.swift b/Sources/CoreKit/Interoperable+Yield.swift index 2ac527fc..b3fbb927 100644 --- a/Sources/CoreKit/Interoperable+Yield.swift +++ b/Sources/CoreKit/Interoperable+Yield.swift @@ -17,7 +17,7 @@ extension Interoperable { // MARK: Transformations //=------------------------------------------------------------------------= - /// Yields its `stdlib` representation. + /// Yields a standard-library-compatible representation. /// /// - Note: Use the `stdlib()` method to transfer ownership. /// diff --git a/Sources/CoreKit/Interoperable.swift b/Sources/CoreKit/Interoperable.swift index 6187b87a..f7543848 100644 --- a/Sources/CoreKit/Interoperable.swift +++ b/Sources/CoreKit/Interoperable.swift @@ -14,7 +14,7 @@ /// A type with a standard-library-compatible representation. public protocol Interoperable { - /// The standard-library-compatible representation of `Self`. + /// A standard-library-compatible representation of `Self`. associatedtype Stdlib //=------------------------------------------------------------------------= @@ -24,6 +24,6 @@ public protocol Interoperable { /// Reinterprets the given `source` as this type. @inlinable init(_ source: consuming Stdlib) - /// Reiinterprets `self` as a standard-library-compatible type. + /// Reinterprets `self` as a standard-library-compatible type. @inlinable consuming func stdlib() -> Stdlib } diff --git a/Sources/RandomIntKit/FuzzerInt.swift b/Sources/RandomIntKit/FuzzerInt.swift index 568f8cfc..13702ae6 100644 --- a/Sources/RandomIntKit/FuzzerInt.swift +++ b/Sources/RandomIntKit/FuzzerInt.swift @@ -15,11 +15,9 @@ import CoreKit /// A deterministic source of random data. /// -/// - Important: This model is most suitable for fuzzing. -/// /// ### Algorithm /// -/// It uses `SplitMix64` because it's simple and light-weight. +/// It adapts `SplitMix64` because it's simple and light-weight. /// /// - Important: It may use a different algorithm in the future. ///