Minor cleanup (#74)

This commit is contained in:
Sindre Sorhus 2021-06-10 13:06:19 +07:00 committed by GitHub
parent 63d93f97ad
commit 6ff23ff1f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 26 deletions

View File

@ -53,7 +53,7 @@ extension Defaults {
} }
extension Defaults { extension Defaults {
public struct RawRepresentableBridge<Value: RawRepresentable>: Defaults.Bridge { public struct RawRepresentableBridge<Value: RawRepresentable>: Bridge {
public typealias Value = Value public typealias Value = Value
public typealias Serializable = Value.RawValue public typealias Serializable = Value.RawValue
@ -72,7 +72,7 @@ extension Defaults {
} }
extension Defaults { extension Defaults {
public struct NSSecureCodingBridge<Value: NSSecureCoding>: Defaults.Bridge { public struct NSSecureCodingBridge<Value: NSSecureCoding>: Bridge {
public typealias Value = Value public typealias Value = Value
public typealias Serializable = Data public typealias Serializable = Data
@ -109,7 +109,7 @@ extension Defaults {
} }
extension Defaults { extension Defaults {
public struct OptionalBridge<Wrapped: Defaults.Serializable>: Defaults.Bridge { public struct OptionalBridge<Wrapped: Serializable>: Bridge {
public typealias Value = Wrapped.Value public typealias Value = Wrapped.Value
public typealias Serializable = Wrapped.Serializable public typealias Serializable = Wrapped.Serializable
@ -124,7 +124,7 @@ extension Defaults {
} }
extension Defaults { extension Defaults {
public struct ArrayBridge<Element: Defaults.Serializable>: Defaults.Bridge { public struct ArrayBridge<Element: Serializable>: Bridge {
public typealias Value = [Element] public typealias Value = [Element]
public typealias Serializable = [Element.Serializable] public typealias Serializable = [Element.Serializable]
@ -147,7 +147,7 @@ extension Defaults {
} }
extension Defaults { extension Defaults {
public struct DictionaryBridge<Key: LosslessStringConvertible & Hashable, Element: Defaults.Serializable>: Defaults.Bridge { public struct DictionaryBridge<Key: LosslessStringConvertible & Hashable, Element: Serializable>: Bridge {
public typealias Value = [Key: Element.Value] public typealias Value = [Key: Element.Value]
public typealias Serializable = [String: Element.Serializable] public typealias Serializable = [String: Element.Serializable]
@ -183,7 +183,7 @@ extension Defaults {
We need both `SetBridge` and `SetAlgebraBridge` because `Set` conforms to `Sequence` but `SetAlgebra` does not. `Set` conforms to `Sequence`, so we can convert it into an array with `Array.init<S>(S)` and store it in the `UserDefaults`. But `SetAlgebra` does not, so it is hard to convert it into an array. Thats why we need the `Defaults.SetAlgebraSerializable` protocol to convert it into an array. We need both `SetBridge` and `SetAlgebraBridge` because `Set` conforms to `Sequence` but `SetAlgebra` does not. `Set` conforms to `Sequence`, so we can convert it into an array with `Array.init<S>(S)` and store it in the `UserDefaults`. But `SetAlgebra` does not, so it is hard to convert it into an array. Thats why we need the `Defaults.SetAlgebraSerializable` protocol to convert it into an array.
*/ */
extension Defaults { extension Defaults {
public struct SetBridge<Element: Defaults.Serializable & Hashable>: Defaults.Bridge { public struct SetBridge<Element: Serializable & Hashable>: Bridge {
public typealias Value = Set<Element> public typealias Value = Set<Element>
public typealias Serializable = Any public typealias Serializable = Any
@ -221,7 +221,7 @@ extension Defaults {
} }
extension Defaults { extension Defaults {
public struct SetAlgebraBridge<Value: Defaults.SetAlgebraSerializable>: Defaults.Bridge where Value.Element: Defaults.Serializable { public struct SetAlgebraBridge<Value: SetAlgebraSerializable>: Bridge where Value.Element: Serializable {
public typealias Value = Value public typealias Value = Value
public typealias Element = Value.Element public typealias Element = Value.Element
public typealias Serializable = Any public typealias Serializable = Any
@ -260,7 +260,7 @@ extension Defaults {
} }
extension Defaults { extension Defaults {
public struct CollectionBridge<Value: Defaults.CollectionSerializable>: Defaults.Bridge where Value.Element: Defaults.Serializable { public struct CollectionBridge<Value: CollectionSerializable>: Bridge where Value.Element: Serializable {
public typealias Value = Value public typealias Value = Value
public typealias Element = Value.Element public typealias Element = Value.Element
public typealias Serializable = Any public typealias Serializable = Any

View File

@ -99,4 +99,4 @@ public protocol DefaultsSetAlgebraSerializable: SetAlgebra, Defaults.Serializabl
} }
/// Convenience protocol for `Codable`. /// Convenience protocol for `Codable`.
public protocol DefaultsCodableBridge: DefaultsBridge where Serializable == String, Value: Codable {} public protocol DefaultsCodableBridge: Defaults.Bridge where Serializable == String, Value: Codable {}

View File

@ -16,15 +16,15 @@ extension Defaults {
Defaults.migrate(.array, to: .v5) Defaults.migrate(.array, to: .v5)
``` ```
*/ */
public static func migrate<Value: Defaults.Serializable & Codable>(_ keys: Key<Value>..., to version: Version) { public static func migrate<Value: Serializable & Codable>(_ keys: Key<Value>..., to version: Version) {
migrate(keys, to: version) migrate(keys, to: version)
} }
public static func migrate<Value: Defaults.NativeType>(_ keys: Key<Value>..., to version: Version) { public static func migrate<Value: NativeType>(_ keys: Key<Value>..., to version: Version) {
migrate(keys, to: version) migrate(keys, to: version)
} }
public static func migrate<Value: Defaults.Serializable & Codable>(_ keys: [Key<Value>], to version: Version) { public static func migrate<Value: Serializable & Codable>(_ keys: [Key<Value>], to version: Version) {
switch version { switch version {
case .v5: case .v5:
for key in keys { for key in keys {
@ -34,7 +34,7 @@ extension Defaults {
} }
} }
public static func migrate<Value: Defaults.NativeType>(_ keys: [Key<Value>], to version: Version) { public static func migrate<Value: NativeType>(_ keys: [Key<Value>], to version: Version) {
switch version { switch version {
case .v5: case .v5:
for key in keys { for key in keys {

View File

@ -180,7 +180,7 @@ extension Defaults.SetAlgebraSerializable where Self: Defaults.NativeType, Eleme
public typealias CodableForm = [Element.CodableForm] public typealias CodableForm = [Element.CodableForm]
} }
extension Defaults.CodableType where Self: RawRepresentable, NativeForm: RawRepresentable, Self.RawValue == NativeForm.RawValue { extension Defaults.CodableType where Self: RawRepresentable, NativeForm: RawRepresentable, RawValue == NativeForm.RawValue {
public func toNative() -> NativeForm { public func toNative() -> NativeForm {
NativeForm(rawValue: self.rawValue)! NativeForm(rawValue: self.rawValue)!
} }

View File

@ -5,7 +5,7 @@ import Combine
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Defaults { extension Defaults {
final class Observable<Value: Serializable>: ObservableObject { final class Observable<Value: Serializable>: ObservableObject {
private var observation: DefaultsObservation? private var cancellable: AnyCancellable?
private let key: Defaults.Key<Value> private let key: Defaults.Key<Value>
let objectWillChange = ObservableObjectPublisher() let objectWillChange = ObservableObjectPublisher()
@ -21,7 +21,8 @@ extension Defaults {
init(_ key: Key<Value>) { init(_ key: Key<Value>) {
self.key = key self.key = key
self.observation = Defaults.observe(key, options: [.prior]) { [weak self] change in self.cancellable = Defaults.publisher(key, options: [.prior])
.sink { [weak self] change in
guard change.isPrior else { guard change.isPrior else {
return return
} }

View File

@ -174,9 +174,12 @@ extension Defaults.Serializable {
*/ */
static func toValue(_ anyObject: Any) -> Self? { static func toValue(_ anyObject: Any) -> Self? {
// Return directly if `anyObject` can cast to Value, since it means `Value` is a natively supported type. // Return directly if `anyObject` can cast to Value, since it means `Value` is a natively supported type.
if Self.isNativelySupportedType, let anyObject = anyObject as? Self { if
isNativelySupportedType,
let anyObject = anyObject as? Self
{
return anyObject return anyObject
} else if let value = Self.bridge.deserialize(anyObject as? Serializable) { } else if let value = bridge.deserialize(anyObject as? Serializable) {
return value as? Self return value as? Self
} }
@ -194,9 +197,9 @@ extension Defaults.Serializable {
*/ */
static func toSerializable(_ value: Self) -> Any? { static func toSerializable(_ value: Self) -> Any? {
// Return directly if `Self` is a natively supported type, since it does not need serialization. // Return directly if `Self` is a natively supported type, since it does not need serialization.
if Self.isNativelySupportedType { if isNativelySupportedType {
return value return value
} else if let serialized = Self.bridge.serialize(value as? Self.Value) { } else if let serialized = bridge.serialize(value as? Value) {
return serialized return serialized
} }