Skip to content

Commit

Permalink
Merge pull request #7 from wickwirew/master
Browse files Browse the repository at this point in the history
Sorry I've been unresponsive, if it passes our test suite then I'm happy to merge it. If any issues arise, I'm sure we'll hear about them. :)
  • Loading branch information
bradhilton authored Jun 1, 2017
2 parents 9400cd8 + d1f70b7 commit 84ebf21
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Sources/Reflection/Set.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Set value for key of an instance
public func set(_ value: Any, key: String, for instance: inout Any) throws {
try property(type: type(of: instance), key: key).write(value, to: mutableStorage(instance: &instance))
let type = type(of: instance)
try property(type: type, key: key).write(value, to: mutableStorage(instance: &instance, type: type))
}

/// Set value for key of an instance
Expand Down
12 changes: 10 additions & 2 deletions Sources/Reflection/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ extension AnyExtensions {
}

func mutableStorage<T>(instance: inout T) -> UnsafeMutableRawPointer {
return UnsafeMutableRawPointer(mutating: storage(instance: &instance))
return mutableStorage(instance: &instance, type: type(of: instance))
}

func mutableStorage<T>(instance: inout T, type: Any.Type) -> UnsafeMutableRawPointer {
return UnsafeMutableRawPointer(mutating: storage(instance: &instance, type: type))
}

func storage<T>(instance: inout T) -> UnsafeRawPointer {
return storage(instance: &instance, type: type(of: instance))
}

func storage<T>(instance: inout T, type: Any.Type) -> UnsafeRawPointer {
return withUnsafePointer(to: &instance) { pointer in
if type(of: instance) is AnyClass {
if type is AnyClass {
return UnsafeRawPointer(bitPattern: UnsafePointer<Int>(pointer).pointee)!
} else {
return UnsafeRawPointer(pointer)
Expand Down

0 comments on commit 84ebf21

Please sign in to comment.