Skip to content

Commit

Permalink
Code format
Browse files Browse the repository at this point in the history
  • Loading branch information
khvorost-dish committed Mar 14, 2024
1 parent 6964086 commit 1dc1e34
Show file tree
Hide file tree
Showing 7 changed files with 608 additions and 609 deletions.
44 changes: 22 additions & 22 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.
//
// Package.swift
Expand Down Expand Up @@ -28,25 +28,25 @@
import PackageDescription

let package = Package(
name: "KeyValueCoding",
platforms: [
.iOS(.v12),
.macOS(.v10_14),
.tvOS(.v12),
.watchOS(.v5)
],
products: [
.library(
name: "KeyValueCoding",
targets: ["KeyValueCoding"]),
],
targets: [
.target(
name: "KeyValueCoding",
dependencies: []),
.testTarget(
name: "KeyValueCodingTests",
dependencies: ["KeyValueCoding"]),
],
swiftLanguageVersions: [.v5]
name: "KeyValueCoding",
platforms: [
.iOS(.v12),
.macOS(.v10_14),
.tvOS(.v12),
.watchOS(.v5)
],
products: [
.library(
name: "KeyValueCoding",
targets: ["KeyValueCoding"]),
],
targets: [
.target(
name: "KeyValueCoding",
dependencies: []),
.testTarget(
name: "KeyValueCodingTests",
dependencies: ["KeyValueCoding"]),
],
swiftLanguageVersions: [.v5]
)
38 changes: 19 additions & 19 deletions Sources/KeyValueCoding/Accessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@
protocol Accessor {}

extension Accessor {

static func get(from pointer: UnsafeRawPointer) -> Any {
return pointer.assumingMemoryBound(to: Self.self).pointee
}

static func set(value: Any, pointer: UnsafeMutableRawPointer) {
if let value = value as? Self {
pointer.assumingMemoryBound(to: self).pointee = value
}
}

static var size: Int {
MemoryLayout<Self>.size

static func get(from pointer: UnsafeRawPointer) -> Any {
return pointer.assumingMemoryBound(to: Self.self).pointee
}

static func set(value: Any, pointer: UnsafeMutableRawPointer) {
if let value = value as? Self {
pointer.assumingMemoryBound(to: self).pointee = value
}
}

static var size: Int {
MemoryLayout<Self>.size
}
}

struct ProtocolTypeContainer {
let type: Any.Type
let witnessTable = 0
var accessor: Accessor.Type {
unsafeBitCast(self, to: Accessor.Type.self)
}
let type: Any.Type
let witnessTable = 0

var accessor: Accessor.Type {
unsafeBitCast(self, to: Accessor.Type.self)
}
}
12 changes: 6 additions & 6 deletions Sources/KeyValueCoding/Existential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import Foundation
let ExistentialHeaderSize = 16 // 64bit

struct ExistentialContainer {
let buffer: ExistentialContainerBuffer
let type: Any.Type
let witnessTable: Int
let buffer: ExistentialContainerBuffer
let type: Any.Type
let witnessTable: Int
}

struct ExistentialContainerBuffer {
let buffer1: Int
let buffer2: Int
let buffer3: Int
let buffer1: Int
let buffer2: Int
let buffer3: Int
}
188 changes: 94 additions & 94 deletions Sources/KeyValueCoding/KeyValueCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,66 +24,66 @@
//

fileprivate func withPointer<T>(_ instance: inout T, _ body: (UnsafeMutableRawPointer, Metadata) -> Any?) -> Any? {
withUnsafePointer(to: &instance) {
let metadata = swift_metadata(of: T.self)
if metadata.kind == .struct {
return body(UnsafeMutableRawPointer(mutating: $0), metadata)
withUnsafePointer(to: &instance) {
let metadata = swift_metadata(of: T.self)
if metadata.kind == .struct {
return body(UnsafeMutableRawPointer(mutating: $0), metadata)
}
else if metadata.kind == .class {
return $0.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1) {
body($0.pointee, metadata)
}
}
else if metadata.kind == .existential {
return $0.withMemoryRebound(to: ExistentialContainer.self, capacity: 1) {
let type = $0.pointee.type
let metadata = swift_metadata(of: type)
if metadata.kind == .class {
return $0.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1) {
body($0.pointee, metadata)
}
}
else if metadata.kind == .class {
else if metadata.kind == .struct {
if metadata.size > MemoryLayout<ExistentialContainerBuffer>.size {
return $0.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1) {
body($0.pointee, metadata)
}
}
else if metadata.kind == .existential {
return $0.withMemoryRebound(to: ExistentialContainer.self, capacity: 1) {
let type = $0.pointee.type
let metadata = swift_metadata(of: type)
if metadata.kind == .class {
return $0.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1) {
body($0.pointee, metadata)
}
}
else if metadata.kind == .struct {
if metadata.size > MemoryLayout<ExistentialContainerBuffer>.size {
return $0.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1) {
body($0.pointee.advanced(by: ExistentialHeaderSize), metadata)
}
}
else {
return body(UnsafeMutableRawPointer(mutating: $0), metadata)
}
}
return nil
body($0.pointee.advanced(by: ExistentialHeaderSize), metadata)
}
}
else {
return body(UnsafeMutableRawPointer(mutating: $0), metadata)
}
}
return nil
}
}
return nil
}
}

@discardableResult
fileprivate func withProperty<T>(_ instance: inout T, keyPath: [String], _ body: (Metadata, UnsafeMutableRawPointer) -> Any?) -> Any? {
withPointer(&instance) { pointer, metadata in
var keys = keyPath
guard let key = keys.popLast(), let property = (metadata.properties.first { $0.name == key }) else {
return nil
}

let pointer = pointer.advanced(by: property.offset)

if keys.isEmpty {
return body(property.metadata, pointer)
}
else if var value = property.metadata.get(from: pointer) {
defer {
let metadata = swift_metadata(of: type(of: value))
if metadata.kind == .struct {
property.metadata.set(value: value, pointer: pointer)
}
}
return withProperty(&value, keyPath: keys, body)
withPointer(&instance) { pointer, metadata in
var keys = keyPath
guard let key = keys.popLast(), let property = (metadata.properties.first { $0.name == key }) else {
return nil
}

let pointer = pointer.advanced(by: property.offset)

if keys.isEmpty {
return body(property.metadata, pointer)
}
else if var value = property.metadata.get(from: pointer) {
defer {
let metadata = swift_metadata(of: type(of: value))
if metadata.kind == .struct {
property.metadata.set(value: value, pointer: pointer)
}
return nil
}
return withProperty(&value, keyPath: keys, body)
}
return nil
}
}

// MARK: -
Expand All @@ -94,7 +94,7 @@ fileprivate func withProperty<T>(_ instance: inout T, keyPath: [String], _ body:
/// - type: Type of a metatype instance.
/// - Returns: Metadata of the type.
public func swift_metadata(of type: Any.Type) -> Metadata {
MetadataCache.shared.metadata(of: type)
MetadataCache.shared.metadata(of: type)
}

/// Returns the metadata of the instance.
Expand All @@ -103,8 +103,8 @@ public func swift_metadata(of type: Any.Type) -> Metadata {
/// - instance: Instance of any type.
/// - Returns: Metadata of the type.
public func swift_metadata(of instance: Any) -> Metadata {
let type = type(of: instance)
return swift_metadata(of: type)
let type = type(of: instance)
return swift_metadata(of: type)
}

/// Returns the value for the instance's property identified by a given name or a key path.
Expand All @@ -116,10 +116,10 @@ public func swift_metadata(of instance: Any) -> Metadata {
/// for example “department.name” or “department.manager.lastName.”
/// - Returns: The value for the property identified by a name or a key path.
public func swift_value<T>(of instance: inout T, key: String) -> Any? {
let keyPath: [String] = key.components(separatedBy: ".").reversed()
return withProperty(&instance, keyPath: keyPath) { metadata, pointer in
metadata.get(from: pointer)
}
let keyPath: [String] = key.components(separatedBy: ".").reversed()
return withProperty(&instance, keyPath: keyPath) { metadata, pointer in
metadata.get(from: pointer)
}
}

/// Sets a property of an instance specified by a given name or a key path to a given value.
Expand All @@ -131,10 +131,10 @@ public func swift_value<T>(of instance: inout T, key: String) -> Any? {
/// relationship.property (with one or more relationships):
/// for example “department.name” or “department.manager.lastName.”
public func swift_setValue<T>(_ value: Any?, to: inout T, key: String) {
let keyPath: [String] = key.components(separatedBy: ".").reversed()
withProperty(&to, keyPath: keyPath) { metadata, pointer in
metadata.set(value: value as Any, pointer: pointer)
}
let keyPath: [String] = key.components(separatedBy: ".").reversed()
withProperty(&to, keyPath: keyPath) { metadata, pointer in
metadata.set(value: value as Any, pointer: pointer)
}
}

// MARK: - KeyValueCoding
Expand All @@ -143,41 +143,41 @@ public func swift_setValue<T>(_ value: Any?, to: inout T, key: String) {
public protocol KeyValueCoding {}

extension KeyValueCoding {

/// Returns the metadata of the instance type.
public var metadata: Metadata {
swift_metadata(of: self)

/// Returns the metadata of the instance type.
public var metadata: Metadata {
swift_metadata(of: self)
}

/// Returns a value for a property identified by a given name or a key path.
///
/// - Parameters:
/// - key: The name of one of the instance's properties or a key path of the form
/// relationship.property (with one or more relationships):
/// for example “department.name” or “department.manager.lastName.”
/// - Returns: The value for the property identified by a name or a key path.
public mutating func value(key: String) -> Any? {
swift_value(of: &self, key: key)
}

/// Sets a property specified by a given name or a key path to a given value.
///
/// - Parameters:
/// - value: The value for the property identified by a name or a key path.
/// - key: The name of one of the instance's properties or a key path of the form
/// relationship.property (with one or more relationships):
/// for example “department.name” or “department.manager.lastName.”
public mutating func setValue(_ value: Any?, key: String) {
swift_setValue(value, to: &self, key: key)
}

/// Gets and sets a value for a property identified by a given name or a key path.
public subscript(key: String) -> Any? {
mutating get {
value(key: key)
}

/// Returns a value for a property identified by a given name or a key path.
///
/// - Parameters:
/// - key: The name of one of the instance's properties or a key path of the form
/// relationship.property (with one or more relationships):
/// for example “department.name” or “department.manager.lastName.”
/// - Returns: The value for the property identified by a name or a key path.
public mutating func value(key: String) -> Any? {
swift_value(of: &self, key: key)
}

/// Sets a property specified by a given name or a key path to a given value.
///
/// - Parameters:
/// - value: The value for the property identified by a name or a key path.
/// - key: The name of one of the instance's properties or a key path of the form
/// relationship.property (with one or more relationships):
/// for example “department.name” or “department.manager.lastName.”
public mutating func setValue(_ value: Any?, key: String) {
swift_setValue(value, to: &self, key: key)
}

/// Gets and sets a value for a property identified by a given name or a key path.
public subscript(key: String) -> Any? {
mutating get {
value(key: key)
}
set {
setValue(newValue, key: key)
}
set {
setValue(newValue, key: key)
}
}
}
Loading

0 comments on commit 1dc1e34

Please sign in to comment.