Skip to content

Commit aae725a

Browse files
committed
Change storage access to leverage AnyExtensions
1 parent 5df0364 commit aae725a

File tree

5 files changed

+28
-33
lines changed

5 files changed

+28
-33
lines changed

Sources/Reflection/Any+Extensions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ extension AnyExtensions {
1818
return try Reflection.construct(self, dictionary: dictionary)
1919
}
2020

21+
static func value(from pointer: UnsafeRawPointer) -> Any {
22+
return pointer.assumingMemoryBound(to: self).pointee
23+
}
24+
2125
func write(to pointer: UnsafeMutableRawPointer) {
2226
pointer.assumingMemoryBound(to: type(of: self)).initialize(to: self)
2327
}

Sources/Reflection/Get.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/// Get value for key from instance
22
public func get(_ key: String, from instance: Any) throws -> Any {
3-
for property in try properties(instance) {
4-
if property.key == key {
5-
return property.value
6-
}
3+
guard let value = try properties(instance).first(where: { $0.key == key })?.value else {
4+
throw ReflectionError.instanceHasNoKey(type: type(of: instance), key: key)
75
}
8-
throw ReflectionError.instanceHasNoKey(type: type(of: instance), key: key)
6+
return value
97
}
108

119
/// Get value for key from instance as type `T`

Sources/Reflection/Properties.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,15 @@ public struct Property {
2727
/// Retrieve properties for `instance`
2828
public func properties(_ instance: Any) throws -> [Property] {
2929
let props = try properties(type(of: instance))
30-
var copy = instance
31-
let storage = storageForInstance(&copy)
30+
var copy = extensions(of: instance)
31+
let storage = copy.storage()
3232
return props.map { nextProperty(description: $0, storage: storage) }
3333
}
3434

3535
private func nextProperty(description: Property.Description, storage: UnsafeRawPointer) -> Property {
3636
return Property(
3737
key: description.key,
38-
value: AnyExistentialContainer(
39-
type: description.type,
40-
pointer: storage.advanced(by: description.offset)
41-
).any
38+
value: extensions(of: description.type).value(from: storage.advanced(by: description.offset))
4239
)
4340
}
4441

Sources/Reflection/Set.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// Set value for key of an instance
22
public func set(_ value: Any, key: String, for instance: inout Any) throws {
3-
let property = try propertyForType(type(of: instance), withName: key)
4-
try setValue(value, forKey: key, property: property, storage: mutableStorageForInstance(&instance))
3+
let property = try Reflection.property(type: type(of: instance), key: key)
4+
try setValue(value, forKey: key, property: property, storage: mutableStorage(instance: &instance))
55
}
66

77
/// Set value for key of an instance
@@ -12,12 +12,12 @@ public func set(_ value: Any, key: String, for instance: AnyObject) throws {
1212

1313
/// Set value for key of an instance
1414
public func set<T>(_ value: Any, key: String, for instance: inout T) throws {
15-
let property = try propertyForType(T.self, withName: key)
16-
try setValue(value, forKey: key, property: property, storage: mutableStorageForInstance(&instance))
15+
let property = try Reflection.property(type: T.self, key: key)
16+
try setValue(value, forKey: key, property: property, storage: mutableStorage(instance: &instance))
1717
}
1818

19-
private func propertyForType(_ type: Any.Type, withName key: String) throws -> Property.Description {
20-
guard let property = try properties(type).filter({ $0.key == key }).first else { throw ReflectionError.instanceHasNoKey(type: type, key: key) }
19+
private func property(type: Any.Type, key: String) throws -> Property.Description {
20+
guard let property = try properties(type).first(where: { $0.key == key }) else { throw ReflectionError.instanceHasNoKey(type: type, key: key) }
2121
return property
2222
}
2323

Sources/Reflection/Storage.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
func mutableStorageForInstance(_ instance: inout Any) -> UnsafeMutableRawPointer {
2-
return UnsafeMutableRawPointer(mutating: storageForInstance(&instance))
3-
}
4-
5-
func storageForInstance(_ instance: inout Any) -> UnsafeRawPointer {
6-
return withUnsafePointer(to: &instance) { pointer in
7-
if type(of: instance) is AnyClass {
8-
return UnsafeRawPointer(bitPattern: UnsafePointer<Int>(pointer).pointee)!
9-
} else if sizeofValue(instance) <= 3 * sizeof(Int.self) {
10-
return UnsafeRawPointer(pointer)
11-
} else {
12-
return UnsafeRawPointer(bitPattern: UnsafePointer<Int>(pointer).pointee)!
13-
}
1+
extension AnyExtensions {
2+
3+
mutating func mutableStorage() -> UnsafeMutableRawPointer {
4+
return Reflection.mutableStorage(instance: &self)
5+
}
6+
7+
mutating func storage() -> UnsafeRawPointer {
8+
return Reflection.storage(instance: &self)
149
}
10+
1511
}
1612

17-
func mutableStorageForInstance<T>(_ instance: inout T) -> UnsafeMutableRawPointer {
18-
return UnsafeMutableRawPointer(mutating: storageForInstance(&instance))
13+
func mutableStorage<T>(instance: inout T) -> UnsafeMutableRawPointer {
14+
return UnsafeMutableRawPointer(mutating: storage(instance: &instance))
1915
}
2016

21-
func storageForInstance<T>(_ instance: inout T) -> UnsafeRawPointer {
17+
func storage<T>(instance: inout T) -> UnsafeRawPointer {
2218
return withUnsafePointer(to: &instance) { pointer in
2319
if type(of: instance) is AnyClass {
2420
return UnsafeRawPointer(bitPattern: UnsafePointer<Int>(pointer).pointee)!

0 commit comments

Comments
 (0)