Repository: vapor/vapor
File: Sources/Vapor/Utilities/Storage.swift (Swift)

Code before:
/// A container providing arbitrary storage for extensions of an existing type, designed to obviate
/// the problem of being unable to add stored properties to a type in an extension. Each stored item
/// is keyed by a type conforming to ``StorageKey`` protocol.
public struct Storage: Sendable {
    /// The internal storage area.
    var storage: [ObjectIdentifier: AnyStorageValue]

    /// A container for a stored value and an associated optional `deinit`-like closure.
    struct Value<T: Sendable>: AnyStorageValue {
        var value: T
        var onShutdown: (@Sendable (T) throws -> ())?
        var onAsyncShutdown: (@Sendable (T) async throws -> ())?
        func shutdown(logger: Logger) {
            do {
                try self.onShutdown?(self.value)
            } catch {
                logger.warning("Could not shutdown \(T.self): \(error)")
            }
        }
        func asyncShutdown(logger: Logger) async {
            do {
                try await self.onAsyncShutdown?(self.value)
            } catch {
                logger.warning("Could not shutdown \(T.self): \(error)")
            }
        }
    }
    
    /// The logger provided to shutdown closures.
    let logger: Logger

    /// Create a new ``Storage`` container using the given logger.
    public init(logger: Logger = .init(label: "codes.vapor.storage")) {
        self.storage = [:]
        self.logger = logger
    }

    /// Delete all values from the container. Does _not_ invoke shutdown closures.
    public mutating func clear() {
        self.storage = [:]
    }

    /// Read/write access to values via keyed subscript.
    public subscript<Key>(_ key: Key.Type) -> Key.Value?
        where Key: StorageKey
    {
        get {
            self.get(Key.self)
        }
        set {
            self.set(Key.self, to: newValue)

Review comment (suggestion):
I don't have a better suggestion, but this seems incredibly error-prone/easy to misuse 😕 

Code after:
/// A container providing arbitrary storage for extensions of an existing type, designed to obviate
/// the problem of being unable to add stored properties to a type in an extension. Each stored item
/// is keyed by a type conforming to ``StorageKey`` protocol.
public struct Storage: Sendable {
    /// The internal storage area.
    var storage: [ObjectIdentifier: AnyStorageValue]

    /// A container for a stored value and an associated optional `deinit`-like closure.
    struct Value<T: Sendable>: AnyStorageValue {
        var value: T
        var onShutdown: (@Sendable (T) throws -> ())?
        var onAsyncShutdown: (@Sendable (T) async throws -> ())?
        func shutdown(logger: Logger) {
            do {
                try self.onShutdown?(self.value)
            } catch {
                logger.warning("Could not shutdown \(T.self): \(error)")
            }
        }
        func asyncShutdown(logger: Logger) async {
            do {
                if let onAsyncShutdown {
                    try await onAsyncShutdown(self.value)
                } else {
                    try self.onShutdown?(self.value)
                }
            } catch {
                logger.warning("Could not shutdown \(T.self): \(error)")
            }
        }
    }
    
    /// The logger provided to shutdown closures.
    let logger: Logger

    /// Create a new ``Storage`` container using the given logger.
    public init(logger: Logger = .init(label: "codes.vapor.storage")) {
        self.storage = [:]
        self.logger = logger
    }

    /// Delete all values from the container. Does _not_ invoke shutdown closures.
    public mutating func clear() {
        self.storage = [:]
    }

    /// Read/write access to values via keyed subscript.
    public subscript<Key>(_ key: Key.Type) -> Key.Value?
        where Key: StorageKey
    {
        get {