Skip to content

Commit

Permalink
Add: Interoperable (#74).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Aug 25, 2024
1 parent 4afaa31 commit 2867ed8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Sources/CoreKit/Interoperable+Yield.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//=----------------------------------------------------------------------------=
// This source file is part of the Ultimathnum open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=

//*============================================================================*
// MARK: * Interoperable x Yield
//*============================================================================*

extension Interoperable {

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

/// Yields its `stdlib` representation.
///
/// - Note: Use the `stdlib()` method to transfer ownership.
///
@inlinable public var stdlib: Stdlib {
mutating _read {
let stdlib = self.stdlib()
yield stdlib
self = Self(stdlib)
}

mutating _modify {
var stdlib = self.stdlib()
yield &stdlib
self = Self(stdlib)
}
}
}
29 changes: 29 additions & 0 deletions Sources/CoreKit/Interoperable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//=----------------------------------------------------------------------------=
// This source file is part of the Ultimathnum open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=

//*============================================================================*
// MARK: * Interoperable
//*============================================================================*

/// A type with a standard-library-compatible representation.
public protocol Interoperable {

/// The standard-library-compatible representation of `Self`.
associatedtype Stdlib

//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=

/// Reinterprets the given `source` as this type.
@inlinable init(_ source: consuming Stdlib)

/// Reiinterprets `self` as a standard-library-compatible type.
@inlinable consuming func stdlib() -> Stdlib
}

0 comments on commit 2867ed8

Please sign in to comment.