swift-library-design Skill
Swift 库和框架设计的专家指导。当开发者提到以下内容时使用:(1) 设计 Swift 库或框架,(2) 公共 API 设计模式,(3) 面向协议的架构或关联类型,(4) result builders 或 DSL 设计,(5) 库的性能优化,(6) @inlinable 或 @usableFromInline,(7) 面向 API 的 noncopyable 类型,(8) API 设计中的渐进式披露,(9) ResponseGenerator 或 builder 模式。
安装方式:把技能目录放入 ~/.claude/skills/(Claude Code)或在 claude.ai 设置中启用;也可复制右侧安装命令一键添加。
技能指令原文(SKILL.md)
Swift Library Design
Patterns and best practices for designing high-quality Swift libraries and frameworks.
Core Principles
1. Protocol-Oriented Design
Design around protocols with associated types for flexibility and testability. Use generics for type safety with runtime polymorphism.
2. Compile-Time Safety
Leverage Swift's type system to catch errors at compile time. Use noncopyable types, generics, and protocol constraints to make invalid states unrepresentable.
3. Performance by Default
Design APIs that are efficient by default. Use @inlinable for hot paths, avoid unnecessary allocations, and provide zero-copy options.
4. Progressive Disclosure
Simple things should be simple, complex things should be possible. Provide sensible defaults while allowing customization.
Key Patterns
Protocol with Associated Types
public protocol Handler<Context>: Sendable {
associatedtype Context
func handle(input: Input, context: Context) async throws -> Output
}
// Consumers accept any conforming type
public struct Pipeline<Context> {
public func add<H: Handler>(handler: H) where H.Context == Context {
// Type-safe composition
}
}
ResponseGenerator Pattern
Allow multiple return types with automatic conversion:
public protocol ResponseGenerator: Sendable {
func response(from request: Request, context: some RequestContext) throws -> Response
}
// Extend standard types
extension String: ResponseGenerator {
public func response(from request: Request, context: some RequestContext) -> Response {
Response(status: .ok, body: .init(string: self))
}
}
extension HTTPResponse.Status: ResponseGenerator {
public func response(from request: Request, context: some RequestContext) -> Response {
Response(status: self)
}
}
Result Builder for DSLs
@resultBuilder
public enum PipelineBuilder<Context> {
public static func buildExpression<M: Middleware>(_ m: M) -> M
where M.Context == Context { m }
public static func buildPartialBlock<M0: Middleware, M1: Middleware>(
accumulated m0: M0, next m1: M1
) -> CombinedMiddleware<M0, M1> {
CombinedMiddleware(m0, m1)
}
}
// Usage
let pipeline = PipelineBuilder {
LoggingMiddleware()
AuthMiddleware()
RateLimitMiddleware()
}
Static Builder Methods
public struct ServerBuilder: Sendable {
private let build: @Sendable () throws -> Server
public static func http1(config: HTTP1Config = .init()) -> ServerBuilder {
.init { HTTP1Server(config: config) }
}
public static func http2(tlsConfig: TLSConfiguration) -> ServerBuilder {
.init { HTTP2Server(tlsConfig: tlsConfig) }
}
}
// Fluent usage
let server = Application(server: .http1(config: .init(idleTimeout: .seconds(30))))
Reference Files
references/api-patterns.md- Protocol design, result builders, builder pattern, error handling, parameter extractionreferences/performance.md- Inlining, noncopyable types, data structures, lock-free patterns, state machines