-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathANKBitPatternConvertible.swift
87 lines (77 loc) · 4.12 KB
/
ANKBitPatternConvertible.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//=----------------------------------------------------------------------------=
// This source file is part of the AwesomeNumbersKit open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
//*============================================================================*
// MARK: * ANK x Bit Pattern Convertible
//*============================================================================*
/// A type that can be converted to and from a bit pattern representation.
///
/// `init(bitPattern:)` is a type-safe alternative to `unsafeBitCast(_:to:)`.
///
public protocol ANKBitPatternConvertible<BitPattern> {
/// The bit pattern of this type.
///
/// Types with compatible bit patterns should have the same bit pattern type.
///
associatedtype BitPattern: ANKBitPatternConvertible<BitPattern> & Sendable
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
/// Creates a new instance from the given bit pattern.
///
/// ```
/// ┌─────────── ⇄ ─────────── ⇄ ────────────┐
/// │ Int256 │ │ UInt256 │
/// ├─────────── ⇄ ─────────── ⇄ ────────────┤
/// │ Int256( 1) │ 0.........1 │ UInt256( 1) │
/// │ Int256( 0) │ 0.......... │ UInt256( 0) │
/// │ Int256(-1) │ 1.......... │ UInt256(~0) │
/// │ Int256(-2) │ 1.........0 │ UInt256(~1) │
/// └─────────── ⇄ ─────────── ⇄ ────────────┘
/// ```
///
@inlinable init(bitPattern: BitPattern)
/// The bit pattern of this value.
///
/// ```
/// ┌─────────── ⇄ ─────────── ⇄ ────────────┐
/// │ Int256 │ │ UInt256 │
/// ├─────────── ⇄ ─────────── ⇄ ────────────┤
/// │ Int256( 1) │ 0.........1 │ UInt256( 1) │
/// │ Int256( 0) │ 0.......... │ UInt256( 0) │
/// │ Int256(-1) │ 1.......... │ UInt256(~0) │
/// │ Int256(-2) │ 1.........0 │ UInt256(~1) │
/// └─────────── ⇄ ─────────── ⇄ ────────────┘
/// ```
///
@inlinable var bitPattern: BitPattern { get }
}
//=----------------------------------------------------------------------------=
// MARK: + Details
//=----------------------------------------------------------------------------=
extension ANKBitPatternConvertible {
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
/// Creates a new instance from the given bit pattern.
///
/// ```
/// ┌─────────── ⇄ ─────────── ⇄ ────────────┐
/// │ Int256 │ │ UInt256 │
/// ├─────────── ⇄ ─────────── ⇄ ────────────┤
/// │ Int256( 1) │ 0.........1 │ UInt256( 1) │
/// │ Int256( 0) │ 0.......... │ UInt256( 0) │
/// │ Int256(-1) │ 1.......... │ UInt256(~0) │
/// │ Int256(-2) │ 1.........0 │ UInt256(~1) │
/// └─────────── ⇄ ─────────── ⇄ ────────────┘
/// ```
///
@inlinable public init(bitPattern source: some ANKBitPatternConvertible<BitPattern>) {
self.init(bitPattern: source.bitPattern)
}
}