diff --git a/Sources/RubyGateway/CRubyMacros.swift b/Sources/RubyGateway/CRubyMacros.swift index 1a6325d..c102c3c 100644 --- a/Sources/RubyGateway/CRubyMacros.swift +++ b/Sources/RubyGateway/CRubyMacros.swift @@ -25,13 +25,13 @@ internal import RubyGatewayHelpers // From platform to NUM -- BIGNUM or FIXNUM depending on size. -func RB_LONG2NUM(_ x: Int) -> VALUE { return rb_long2num_inline(x) } -func RB_ULONG2NUM(_ x: UInt) -> VALUE { return rb_ulong2num_inline(x) } +func RB_LONG2NUM(_ x: Int) -> VALUE { rb_long2num_inline(x) } +func RB_ULONG2NUM(_ x: UInt) -> VALUE { rb_ulong2num_inline(x) } // MARK: - Floating point conversions // From platform to NUM -- FLONUM or CLASS(FLOAT) depending -func DBL2NUM(_ dbl: Double) -> VALUE { return rb_float_new(dbl) } +func DBL2NUM(_ dbl: Double) -> VALUE { rb_float_new(dbl) } // MARK: - Useful VALUE constants and macros diff --git a/Sources/RubyGateway/RbBlockCall.swift b/Sources/RubyGateway/RbBlockCall.swift index b2a742b..e8aa67e 100644 --- a/Sources/RubyGateway/RbBlockCall.swift +++ b/Sources/RubyGateway/RbBlockCall.swift @@ -107,7 +107,7 @@ private class RbBlockContext { // A EXC_BAD_ACCESS here usually means the blockRetention has been // set wrongly - at any rate, the `RbProcContext` has been deallocated // while Ruby was still using it. - return Unmanaged.fromOpaque(raw).takeUnretainedValue() + Unmanaged.fromOpaque(raw).takeUnretainedValue() } } diff --git a/Sources/RubyGateway/RbClass.swift b/Sources/RubyGateway/RbClass.swift index b34e3a7..aac0517 100644 --- a/Sources/RubyGateway/RbClass.swift +++ b/Sources/RubyGateway/RbClass.swift @@ -20,7 +20,7 @@ private protocol RbBoundClassProtocol { } private struct RbBoundClass : RbBoundClassProtocol { - var initializer: () -> T + let initializer: () -> T func createInstance() -> UnsafeMutableRawPointer { let instance = initializer() diff --git a/Sources/RubyGateway/RbComplex.swift b/Sources/RubyGateway/RbComplex.swift index 928d21d..64309f7 100644 --- a/Sources/RubyGateway/RbComplex.swift +++ b/Sources/RubyGateway/RbComplex.swift @@ -52,7 +52,7 @@ public struct RbComplex: RbObjectConvertible { /// ```swift /// let compl = RbComplex("1+2.3i") /// ``` - public init?(_ value: RbObjectConvertible) { + public init?(_ value: any RbObjectConvertible) { self.init(value.rubyObject) } diff --git a/Sources/RubyGateway/RbConversions.swift b/Sources/RubyGateway/RbConversions.swift index 1636355..f02af5b 100644 --- a/Sources/RubyGateway/RbConversions.swift +++ b/Sources/RubyGateway/RbConversions.swift @@ -34,7 +34,7 @@ extension RbObject { /// /// RubyGateway conforms most of the Swift standard library types /// to `RbObjectConvertible`. - public convenience init(_ value: RbObjectConvertible) { + public convenience init(_ value: any RbObjectConvertible) { self.init(value.rubyObject) } } @@ -44,7 +44,7 @@ extension RbObject: RbObjectConvertible { /// Returns `self`, the `RbObject`. /// /// :nodoc: - public var rubyObject: RbObject { return self } + public var rubyObject: RbObject { self } // Quick sanity check here: RbObject is a ref type so this `return self` is OK, // it returns a second ref-counted ptr to the single `RbObj` which has a single // `Rbb_val`. There is no aliasing of `Rbb_val` ownership. @@ -169,7 +169,7 @@ extension Bool: RbObjectConvertible { /// A Ruby object for the boolean value. public var rubyObject: RbObject { - return RbObject(rubyValue: self ? Qtrue : Qfalse) + RbObject(rubyValue: self ? Qtrue : Qfalse) } } @@ -352,7 +352,7 @@ extension Array: RbObjectConvertible where Element: RbObjectConvertible { extension ArraySlice: RbObjectConvertible where Element: RbObjectConvertible { /// No sense in converting :nodoc: public init?(_ value: RbObject) { - return nil + nil } /// Create a Ruby array object for this `ArraySlice`. All the sliceness @@ -489,8 +489,8 @@ extension Optional: RbObjectConvertible where Wrapped == RbObjectConvertible { public var rubyObject: RbObject { switch self { - case .some(let w): return w.rubyObject - case .none: return .nilObject + case .some(let w): w.rubyObject + case .none: .nilObject } } } @@ -517,7 +517,7 @@ private func decodeRange(_ object: RbObject, halfOpen: Bool) -> (T, T)? where /// Helper to create a Ruby Range from Swift types. private func makeRange(lower: T, upper: T, halfOpen: Bool) -> RbObject where T: RbObjectConvertible { - return RbObject(ofClass: "Range", args: [lower, upper, halfOpen]) ?? .nilObject + RbObject(ofClass: "Range", args: [lower, upper, halfOpen]) ?? .nilObject } // One day Swift will catch up with C++ and let me write this as a single generic... @@ -538,7 +538,7 @@ extension Range: RbObjectConvertible where Bound: RbObjectConvertible { /// A Ruby object for the range. public var rubyObject: RbObject { - return makeRange(lower: lowerBound, upper: upperBound, halfOpen: true) + makeRange(lower: lowerBound, upper: upperBound, halfOpen: true) } } @@ -560,7 +560,7 @@ extension ClosedRange: RbObjectConvertible where Bound: RbObjectConvertible { /// A Ruby object for the range. public var rubyObject: RbObject { - return makeRange(lower: lowerBound, upper: upperBound, halfOpen: false) + makeRange(lower: lowerBound, upper: upperBound, halfOpen: false) } } diff --git a/Sources/RubyGateway/RbError.swift b/Sources/RubyGateway/RbError.swift index c63c25d..230e90c 100644 --- a/Sources/RubyGateway/RbError.swift +++ b/Sources/RubyGateway/RbError.swift @@ -12,7 +12,6 @@ internal import RubyGatewayHelpers /// generates `RbError.rubyJump(_:)`, and the other cases correspond /// to error conditions encountered by the Swift software. public enum RbError: Error { - // MARK: - Cases /// The Ruby VM could not be set up. @@ -124,19 +123,19 @@ extension RbError: CustomStringConvertible { public var description: String { switch self { case let .setup(msg): - return "Can't set up Ruby: \(msg)" + "Can't set up Ruby: \(msg)" case let .badType(msg): - return "Object has bad type: \(msg)" + "Object has bad type: \(msg)" case let .badParameter(msg): - return "Parameter has bad value: \(msg)" + "Parameter has bad value: \(msg)" case let .badIdentifier(type, id): - return "Bad Ruby identifier: '\(id)' does not look like \(type) name." + "Bad Ruby identifier: '\(id)' does not look like \(type) name." case let .duplicateKwArg(key): - return "Duplicate keyword arg \(key) on call()." + "Duplicate keyword arg \(key) on call()." case let .rubyException(exn): - return "Ruby exception: \(exn)" + "Ruby exception: \(exn)" case let .rubyJump(tag): - return "Ruby jump flow control: \(tag)" + "Ruby jump flow control: \(tag)" } } } @@ -162,7 +161,7 @@ public struct RbBreak: Error { /// /// - parameter object: the value to give as the result of the iteration. /// Default `nil` equivalent to raw `break` in Ruby. - public init(with object: RbObjectConvertible? = nil) { + public init(with object: (any RbObjectConvertible)? = nil) { self.object = object?.rubyObject } } diff --git a/Sources/RubyGateway/RbFailableAccess.swift b/Sources/RubyGateway/RbFailableAccess.swift index b5f8566..5faeed9 100644 --- a/Sources/RubyGateway/RbFailableAccess.swift +++ b/Sources/RubyGateway/RbFailableAccess.swift @@ -39,7 +39,7 @@ extension RbObjectAccess { /// Get a version of this API that returns `nil` instead of throwing errors. /// See `RbFailableAccess`. public var failable: RbFailableAccess { - return RbFailableAccess(access: self) + RbFailableAccess(access: self) } } @@ -56,9 +56,9 @@ extension RbFailableAccess { /// - parameter kwArgs: The keyword arguments to the method, none by default. /// - returns: An `RbObject` for the result of the method, or `nil` if an error occurred. public func call(_ method: String, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:]) -> RbObject? { - return try? access.call(method, args: args, kwArgs: kwArgs) + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:]) -> RbObject? { + try? access.call(method, args: args, kwArgs: kwArgs) } /// Call a method of a Ruby object passing Swift code as a block. @@ -74,11 +74,11 @@ extension RbFailableAccess { /// - parameter blockCall: Swift code to pass as a block to the method. /// - returns: An `RbObject` for the result of the method, or `nil` if an error occurred. public func call(_ method: String, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:], + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:], blockRetention: RbBlockRetention = .none, blockCall: @escaping RbBlockCallback) -> RbObject? { - return try? access.call(method, args: args, kwArgs: kwArgs, blockRetention: blockRetention, blockCall: blockCall) + try? access.call(method, args: args, kwArgs: kwArgs, blockRetention: blockRetention, blockCall: blockCall) } /// Call a method of a Ruby object passing a Ruby Proc as a block. @@ -92,10 +92,10 @@ extension RbFailableAccess { /// - parameter block: A Ruby proc to pass as a block to the method. /// - returns: An `RbObject` for the result of the method, or `nil` if an error occurred. public func call(_ method: String, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:], - block: RbObjectConvertible) -> RbObject? { - return try? access.call(method, args: args, kwArgs: kwArgs, block: block) + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:], + block: any RbObjectConvertible) -> RbObject? { + try? access.call(method, args: args, kwArgs: kwArgs, block: block) } /// Call a method of a Ruby object using a symbol. @@ -108,10 +108,10 @@ extension RbFailableAccess { /// - parameter kwArgs: The keyword arguments to the method, none by default. /// - returns: An `RbObject` for the result of the method, or `nil` if an error occurred. @discardableResult - public func call(symbol: RbObjectConvertible, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:]) -> RbObject? { - return try? access.call(symbol: symbol, args: args, kwArgs: kwArgs) + public func call(symbol: any RbObjectConvertible, + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:]) -> RbObject? { + try? access.call(symbol: symbol, args: args, kwArgs: kwArgs) } /// Call a method of a Ruby object using a symbol passing Swift code as a block. @@ -127,12 +127,12 @@ extension RbFailableAccess { /// - parameter blockCall: Swift code to pass as a block to the method. /// - returns: An `RbObject` for the result of the method, or `nil` if an error occurred. @discardableResult - public func call(symbol: RbObjectConvertible, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:], + public func call(symbol: any RbObjectConvertible, + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:], blockRetention: RbBlockRetention = .none, blockCall: @escaping RbBlockCallback) -> RbObject? { - return try? access.call(symbol: symbol, args: args, kwArgs: kwArgs, blockRetention: blockRetention, blockCall: blockCall) + try? access.call(symbol: symbol, args: args, kwArgs: kwArgs, blockRetention: blockRetention, blockCall: blockCall) } /// Call a method of a Ruby object using a symbol passing a Ruby Proc as a block. @@ -146,11 +146,11 @@ extension RbFailableAccess { /// - parameter block: A Ruby proc to pass as a block to the method. /// - returns: An `RbObject` for the result of the method, or `nil` if an error occurred. @discardableResult - public func call(symbol: RbObjectConvertible, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:], - block: RbObjectConvertible) -> RbObject? { - return try? access.call(symbol: symbol, args: args, kwArgs: kwArgs, block: block) + public func call(symbol: any RbObjectConvertible, + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:], + block: any RbObjectConvertible) -> RbObject? { + try? access.call(symbol: symbol, args: args, kwArgs: kwArgs, block: block) } } @@ -165,7 +165,7 @@ extension RbFailableAccess { /// - parameter name: The attribute to access. /// - returns: The value of the attribute, or `nil` if an error occurred. public func getAttribute(_ name: String) -> RbObject? { - return try? access.getAttribute(name) + try? access.getAttribute(name) } /// Set an attribute of a Ruby object. @@ -176,8 +176,8 @@ extension RbFailableAccess { /// - parameter name: The attribute to set. /// - parameter newValue: The new value for the attribute. /// - returns: The value set to the attribute, or `nil` if an error occurred. - public func setAttribute(_ name: String, newValue: RbObjectConvertible?) -> RbObject? { - return try? access.setAttribute(name, newValue: newValue) + public func setAttribute(_ name: String, newValue: (any RbObjectConvertible)?) -> RbObject? { + try? access.setAttribute(name, newValue: newValue) } } @@ -192,7 +192,7 @@ extension RbFailableAccess { /// - parameter name: The name of the constant to look up. /// - returns: An `RbObject` for the constant or `nil` if an error occurred. public func getConstant(_ name: String) -> RbObject? { - return try? access.getConstant(name) + try? access.getConstant(name) } /// Get an `RbObject` that represents a Ruby class. @@ -203,7 +203,7 @@ extension RbFailableAccess { /// - parameter name: The name of the class to look up. /// - returns: An `RbObject` for the class or `nil` if an error occurred. public func getClass(_ name: String) -> RbObject? { - return try? access.getClass(name) + try? access.getClass(name) } /// Bind an object to a constant name. @@ -215,8 +215,8 @@ extension RbFailableAccess { /// - parameter newValue: The value for the constant. /// - returns: The value set for the constant or `nil` if an error occurred. @discardableResult - public func setConstant(_ name: String, newValue: RbObjectConvertible?) -> RbObject? { - return try? access.setConstant(name, newValue: newValue) + public func setConstant(_ name: String, newValue: (any RbObjectConvertible)?) -> RbObject? { + try? access.setConstant(name, newValue: newValue) } } @@ -231,7 +231,7 @@ extension RbFailableAccess { /// - parameter name: Name of the IVar. Must begin with a single `@`. /// - returns: Value of the IVar, or `nil` if an error occurred. public func getInstanceVar(_ name: String) -> RbObject? { - return try? access.getInstanceVar(name) + try? access.getInstanceVar(name) } /// Set a Ruby instance variable. @@ -243,8 +243,8 @@ extension RbFailableAccess { /// - parameter newValue: The new value for the IVar. /// - returns: The value that was set, or nil if an error occurred. @discardableResult - public func setInstanceVar(_ name: String, newValue: RbObjectConvertible?) -> RbObject? { - return try? access.setInstanceVar(name, newValue: newValue) + public func setInstanceVar(_ name: String, newValue: (any RbObjectConvertible)?) -> RbObject? { + try? access.setInstanceVar(name, newValue: newValue) } } @@ -261,7 +261,7 @@ extension RbFailableAccess { /// - parameter name: Name of the CVar. Must begin with `@@`. /// - returns: Value of the CVar, or `nil` if an error occurred. public func getClassVar(_ name: String) -> RbObject? { - return try? access.getClassVar(name) + try? access.getClassVar(name) } /// Set or create a Ruby class variable. @@ -275,8 +275,8 @@ extension RbFailableAccess { /// - parameter newValue: The new value for the CVar. /// - returns: The value that was set, or `nil` if an error occurred. @discardableResult - public func setClassVar(_ name: String, newValue: RbObjectConvertible?) -> RbObject? { - return try? access.setClassVar(name, newValue: newValue) + public func setClassVar(_ name: String, newValue: (any RbObjectConvertible)?) -> RbObject? { + try? access.setClassVar(name, newValue: newValue) } } @@ -291,7 +291,7 @@ extension RbFailableAccess { /// - parameter name: Name of the global variable. Must begin with `$`. /// - returns: Value of the variable, or `nil` if an error occurred. public func getGlobalVar(_ name: String) -> RbObject? { - return try? access.getGlobalVar(name) + try? access.getGlobalVar(name) } /// Set a Ruby global variable. @@ -303,8 +303,8 @@ extension RbFailableAccess { /// - parameter newValue: The new value for the variable. /// - returns: The value that was set, or `nil` if an error occurred. @discardableResult - public func setGlobalVar(_ name: String, newValue: RbObjectConvertible?) -> RbObject? { - return try? access.setGlobalVar(name, newValue: newValue) + public func setGlobalVar(_ name: String, newValue: (any RbObjectConvertible)?) -> RbObject? { + try? access.setGlobalVar(name, newValue: newValue) } } @@ -320,6 +320,6 @@ extension RbFailableAccess { /// - returns: Retrieved object, or `nil` if an error occurred. @discardableResult public func get(_ name: String) -> RbObject? { - return try? access.get(name) + try? access.get(name) } } diff --git a/Sources/RubyGateway/RbGateway.swift b/Sources/RubyGateway/RbGateway.swift index f362a40..c484d36 100644 --- a/Sources/RubyGateway/RbGateway.swift +++ b/Sources/RubyGateway/RbGateway.swift @@ -159,7 +159,7 @@ public final class RbGateway: RbObjectAccess, @unchecked Sendable { /// - throws: `RbError.badIdentifier(type:id:)` if `name` looks wrong. /// `RbError.rubyException(_:)` if Ruby has a problem. @discardableResult - public override func setInstanceVar(_ name: String, newValue: RbObjectConvertible?) throws -> RbObject { + public override func setInstanceVar(_ name: String, newValue: (any RbObjectConvertible)?) throws -> RbObject { try setup() try name.checkRubyInstanceVarName() @@ -309,7 +309,7 @@ extension RbGateway { public func require(filename: String) throws -> Bool { // Have to use eval so that gems work - rubygems/kernel_require.rb replaces // `Kernel#require` so it can do the gem thing, so `rb_require` is no good. - return try eval(ruby: "require '\(filename)'").isTruthy + try eval(ruby: "require '\(filename)'").isTruthy } /// See Ruby `Kernel#load`. Load a file, reloads if already loaded. diff --git a/Sources/RubyGateway/RbGlobalVar.swift b/Sources/RubyGateway/RbGlobalVar.swift index 6364900..c5e091d 100644 --- a/Sources/RubyGateway/RbGlobalVar.swift +++ b/Sources/RubyGateway/RbGlobalVar.swift @@ -14,7 +14,7 @@ internal import RubyGatewayHelpers // MARK: Callbacks from C code (rbg_value.m) private func rbobject_gvar_get_callback(id: ID) -> VALUE { - return RbGlobalVar.get(id: id) + RbGlobalVar.get(id: id) } private func rbobject_gvar_set_callback(id: ID, diff --git a/Sources/RubyGateway/RbMethod.swift b/Sources/RubyGateway/RbMethod.swift index 44e5c0b..f9505f6 100644 --- a/Sources/RubyGateway/RbMethod.swift +++ b/Sources/RubyGateway/RbMethod.swift @@ -107,8 +107,8 @@ private struct RbMethodId: Hashable { let mid: Rbg_method_id public static func == (lhs: RbMethodId, rhs: RbMethodId) -> Bool { - return (lhs.mid.method == rhs.mid.method) && - (lhs.mid.target == rhs.mid.target) + (lhs.mid.method == rhs.mid.method) && + (lhs.mid.target == rhs.mid.target) } public func hash(into hasher: inout Hasher) { @@ -123,7 +123,7 @@ private struct RbMethodExec { let argsSpec: RbMethodArgsSpec /// The Swift implementation of the method. - var callback: RbMethodCallback + let callback: RbMethodCallback /// Validate the given args against the spec and if good, /// invoke the user function. @@ -205,7 +205,7 @@ public struct RbMethod { /// Has the method been passed a block? public var isBlockGiven: Bool { - return rb_block_given_p() != 0 + rb_block_given_p() != 0 } /// Raise an exception if the method has not been passed a block. @@ -227,8 +227,8 @@ public struct RbMethod { /// You should not attempt to handle `rubyJump` errors: rethrow them /// back to Ruby as soon as possible. @discardableResult - public func yieldBlock(args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:]) throws -> RbObject { + public func yieldBlock(args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:]) throws -> RbObject { let rubyArgs = try RbObjectAccess.flattenArgs(args: args, kwArgs: kwArgs) return RbObject(rubyValue: try rubyArgs.withRubyValues { argValues in try RbVM.doProtect { tag in @@ -263,8 +263,8 @@ public struct RbMethod { /// - Returns: The value returned by the superclass method. /// - Throws: `RbError.rubyException(_:)` if there is a Ruby exception. /// `RbError.duplicateKwArg(_:)` if there are duplicate keywords in `kwArgs`. - public func callSuper(args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:]) throws -> RbObject { + public func callSuper(args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:]) throws -> RbObject { let rubyArgs = try RbObjectAccess.flattenArgs(args: args, kwArgs: kwArgs) return RbObject(rubyValue: try rubyArgs.withRubyValues { rubyValues in try RbVM.doProtect { tag in @@ -324,7 +324,7 @@ public struct RbMethodArgsSpec { public let optionalValues: [() -> RbObject] /// The number of optional positional arguments. public var optionalCount: Int { - return optionalValues.count + optionalValues.count } /// Does the method support variable-length splatted arguments? public let supportsSplat: Bool @@ -332,7 +332,7 @@ public struct RbMethodArgsSpec { public let trailingMandatoryCount: Int /// The number of all mandatory positional arguments. public var totalMandatoryCount: Int { - return leadingMandatoryCount + trailingMandatoryCount + leadingMandatoryCount + trailingMandatoryCount } /// Names of mandatory keyword arguments. public let mandatoryKeywords: Set @@ -340,7 +340,7 @@ public struct RbMethodArgsSpec { public let optionalKeywordValues: [String : () -> RbObject] /// Does the method support keyword arguments? public var supportsKeywords: Bool { - return mandatoryKeywords.count > 0 || optionalKeywordValues.count > 0 + mandatoryKeywords.count > 0 || optionalKeywordValues.count > 0 } /// Does the method require a block? public let requiresBlock: Bool @@ -373,11 +373,11 @@ public struct RbMethodArgsSpec { /// - requiresBlock: Whether the method requires a block, `false` by default. If this is /// `true` then the method may or may not be called with a block. public init(leadingMandatoryCount: Int = 0, - optionalValues: [RbObjectConvertible?] = [], + optionalValues: [(any RbObjectConvertible)?] = [], supportsSplat: Bool = false, trailingMandatoryCount: Int = 0, mandatoryKeywords: Set = [], - optionalKeywordValues: [String: RbObjectConvertible?] = [:], + optionalKeywordValues: [String: (any RbObjectConvertible)?] = [:], requiresBlock: Bool = false) { self.leadingMandatoryCount = leadingMandatoryCount self.optionalValues = optionalValues.map { val in { val.rubyObject } } @@ -399,7 +399,7 @@ public struct RbMethodArgsSpec { /// - parameter count: the number of arguments not including any block that the /// method must be passed. public static func basic(_ count: Int) -> RbMethodArgsSpec { - return RbMethodArgsSpec(leadingMandatoryCount: count) + RbMethodArgsSpec(leadingMandatoryCount: count) } /// Decode the arguments passed to a function and make them available. diff --git a/Sources/RubyGateway/RbNumericConversions.swift b/Sources/RubyGateway/RbNumericConversions.swift index 1c948e4..73cf64b 100644 --- a/Sources/RubyGateway/RbNumericConversions.swift +++ b/Sources/RubyGateway/RbNumericConversions.swift @@ -30,7 +30,7 @@ extension UInt64: RbObjectConvertible { /// A Ruby object for the number. public var rubyObject: RbObject { - return UInt(self).rubyObject + UInt(self).rubyObject } } @@ -54,7 +54,7 @@ extension UInt32: RbObjectConvertible { /// A Ruby object for the number. public var rubyObject: RbObject { - return UInt(self).rubyObject + UInt(self).rubyObject } } @@ -78,7 +78,7 @@ extension UInt16: RbObjectConvertible { /// A Ruby object for the number. public var rubyObject: RbObject { - return UInt(self).rubyObject + UInt(self).rubyObject } } @@ -102,7 +102,7 @@ extension UInt8: RbObjectConvertible { /// A Ruby object for the number. public var rubyObject: RbObject { - return UInt(self).rubyObject + UInt(self).rubyObject } } @@ -127,7 +127,7 @@ extension Int64: RbObjectConvertible { /// A Ruby object for the number. public var rubyObject: RbObject { - return Int(self).rubyObject + Int(self).rubyObject } } @@ -150,7 +150,7 @@ extension Int32: RbObjectConvertible { /// A Ruby object for the number. public var rubyObject: RbObject { - return Int(self).rubyObject + Int(self).rubyObject } } @@ -173,7 +173,7 @@ extension Int16: RbObjectConvertible { /// A Ruby object for the number. public var rubyObject: RbObject { - return Int(self).rubyObject + Int(self).rubyObject } } @@ -196,7 +196,7 @@ extension Int8: RbObjectConvertible { /// A Ruby object for the number. public var rubyObject: RbObject { - return Int(self).rubyObject + Int(self).rubyObject } } @@ -226,6 +226,6 @@ extension Float: RbObjectConvertible { /// A Ruby object for the number. public var rubyObject: RbObject { - return Double(self).rubyObject + Double(self).rubyObject } } diff --git a/Sources/RubyGateway/RbObject.swift b/Sources/RubyGateway/RbObject.swift index 202e1ce..b0bd0de 100644 --- a/Sources/RubyGateway/RbObject.swift +++ b/Sources/RubyGateway/RbObject.swift @@ -246,8 +246,8 @@ extension RbObject { /// - parameter args: positional arguments to pass to `new` call for the object. Default none. /// - parameter kwArgs: keyword arguments to pass to the `new` call for the object. Default none. public convenience init?(ofClass className: String, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:]) { + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:]) { guard let obj = try? Ruby.get(className).call("new", args: args, kwArgs: kwArgs) else { return nil } @@ -268,8 +268,8 @@ extension RbObject { /// using `Proc#new`. /// - parameter blockCall: Swift code to pass as a block to the method. public convenience init?(ofClass className: String, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:], + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:], retainBlock: Bool = false, blockCall: @escaping RbBlockCallback) { let retention: RbBlockRetention = retainBlock ? .returned : .none @@ -326,7 +326,7 @@ extension RbObject: CustomStringConvertible, /// The text from `description`. public var playgroundDescription: Any { - return description + description } } diff --git a/Sources/RubyGateway/RbObjectAccess.swift b/Sources/RubyGateway/RbObjectAccess.swift index c3ed848..2094668 100644 --- a/Sources/RubyGateway/RbObjectAccess.swift +++ b/Sources/RubyGateway/RbObjectAccess.swift @@ -109,13 +109,13 @@ public class RbObjectAccess { /// - throws: `RbError.badIdentifier(type:id:)` if `name` looks wrong. /// `RbError.rubyException(_:)` if Ruby has a problem. @discardableResult - public func setInstanceVar(_ name: String, newValue: RbObjectConvertible?) throws -> RbObject { + public func setInstanceVar(_ name: String, newValue: (any RbObjectConvertible)?) throws -> RbObject { try Ruby.setup() try name.checkRubyInstanceVarName() let id = try Ruby.getID(for: name) return RbObject(rubyValue: newValue.rubyObject.withRubyValue { newRubyValue in - return rb_ivar_set(getValue(), id, newRubyValue) + rb_ivar_set(getValue(), id, newRubyValue) }) } } @@ -189,7 +189,7 @@ extension RbObjectAccess { /// `rubyException(_:)` if there is a Ruby exception. /// `RbError.badType(_:)` if the current object is not a class or module. @discardableResult - public func setConstant(_ name: String, newValue: RbObjectConvertible?) throws -> RbObject { + public func setConstant(_ name: String, newValue: (any RbObjectConvertible)?) throws -> RbObject { try Ruby.setup() try name.checkRubyConstantPath() @@ -266,8 +266,8 @@ extension RbObjectAccess { /// For a version that does not throw, see `failable`. @discardableResult public func call(_ methodName: String, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:]) throws -> RbObject { + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:]) throws -> RbObject { try Ruby.setup() let methodId = try Ruby.getID(for: methodName) return try doCall(id: methodId, args: args, kwArgs: kwArgs) @@ -288,8 +288,8 @@ extension RbObjectAccess { /// For a version that does not throw, see `failable`. @discardableResult public func call(_ methodName: String, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:], + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:], blockRetention: RbBlockRetention = .none, blockCall: @escaping RbBlockCallback) throws -> RbObject { try Ruby.setup() @@ -314,9 +314,9 @@ extension RbObjectAccess { /// For a version that does not throw, see `failable`. @discardableResult public func call(_ methodName: String, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:], - block: RbObjectConvertible) throws -> RbObject { + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:], + block: any RbObjectConvertible) throws -> RbObject { try Ruby.setup() let methodId = try Ruby.getID(for: methodName) return try doCall(id: methodId, args: args, kwArgs: kwArgs, block: block) @@ -334,9 +334,9 @@ extension RbObjectAccess { /// /// For a version that does not throw, see `failable`. @discardableResult - public func call(symbol: RbObjectConvertible, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:]) throws -> RbObject { + public func call(symbol: any RbObjectConvertible, + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:]) throws -> RbObject { try Ruby.setup() return try symbol.rubyObject.withSymbolId { methodId in try doCall(id: methodId, args: args, kwArgs: kwArgs) @@ -358,9 +358,9 @@ extension RbObjectAccess { /// /// For a version that does not throw, see `failable`. @discardableResult - public func call(symbol: RbObjectConvertible, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:], + public func call(symbol: any RbObjectConvertible, + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:], blockRetention: RbBlockRetention = .none, blockCall: @escaping RbBlockCallback) throws -> RbObject { try Ruby.setup() @@ -383,10 +383,10 @@ extension RbObjectAccess { /// /// For a version that does not throw, see `failable`. @discardableResult - public func call(symbol: RbObjectConvertible, - args: [RbObjectConvertible?] = [], - kwArgs: KeyValuePairs = [:], - block: RbObjectConvertible) throws -> RbObject { + public func call(symbol: any RbObjectConvertible, + args: [(any RbObjectConvertible)?] = [], + kwArgs: KeyValuePairs = [:], + block: any RbObjectConvertible) throws -> RbObject { try Ruby.setup() return try symbol.rubyObject.withSymbolId { methodId in try doCall(id: methodId, args: args, kwArgs: kwArgs, block: block) @@ -395,10 +395,10 @@ extension RbObjectAccess { /// Backend to method-call / message-send. private func doCall(id: ID, - args: [RbObjectConvertible?], - kwArgs: KeyValuePairs, + args: [(any RbObjectConvertible)?], + kwArgs: KeyValuePairs, blockRetention: RbBlockRetention = .none, - block: RbObjectConvertible? = nil, + block: (any RbObjectConvertible)? = nil, blockCall: RbBlockCallback? = nil) throws -> RbObject { // Sort out unlikely block errors let blockObj: RbObject? @@ -452,9 +452,8 @@ extension RbObjectAccess { } /// Helper to massage Swift-format args ready for the API - internal static func flattenArgs(args: [RbObjectConvertible?], - kwArgs: KeyValuePairs) throws -> [RbObject] { - + internal static func flattenArgs(args: [(any RbObjectConvertible)?], + kwArgs: KeyValuePairs) throws -> [RbObject] { var argObjects = args.map { $0.rubyObject } if kwArgs.count > 0 { @@ -464,7 +463,7 @@ extension RbObjectAccess { } /// Build a keyword args hash. The keys are Symbols of the keywords. - private static func buildKwArgsHash(from kwArgs: KeyValuePairs) throws -> RbObject { + private static func buildKwArgsHash(from kwArgs: KeyValuePairs) throws -> RbObject { let hashValue = rb_hash_new() try kwArgs.forEach { (key, value) in try RbSymbol(key).rubyObject.withRubyValue { symValue in @@ -514,7 +513,7 @@ extension RbObjectAccess { /// `RbError.rubyException(_:)` if Ruby has a problem, probably means /// `attribute` doesn't exist. @discardableResult - public func setAttribute(_ name: String, newValue: RbObjectConvertible?) throws -> RbObject { + public func setAttribute(_ name: String, newValue: (any RbObjectConvertible)?) throws -> RbObject { try name.checkRubyMethodName() return try call("\(name)=", args: [newValue]) } @@ -575,7 +574,7 @@ extension RbObjectAccess { /// `RbError.badType(_:)` if the object is not a class. /// `RbError.rubyException(_:)` if Ruby has a problem. @discardableResult - public func setClassVar(_ name: String, newValue: RbObjectConvertible?) throws -> RbObject { + public func setClassVar(_ name: String, newValue: (any RbObjectConvertible)?) throws -> RbObject { try Ruby.setup() try name.checkRubyClassVarName() try hasClassVars() @@ -626,7 +625,7 @@ extension RbObjectAccess { /// make construction of `RbFailableAccess` a bit easier. Best practice probably /// to avoid calling the `RbObject` version.) @discardableResult - public func setGlobalVar(_ name: String, newValue: RbObjectConvertible?) throws -> RbObject { + public func setGlobalVar(_ name: String, newValue: (any RbObjectConvertible)?) throws -> RbObject { try Ruby.setup() try name.checkRubyGlobalVarName() diff --git a/Sources/RubyGateway/RbObjectCollection.swift b/Sources/RubyGateway/RbObjectCollection.swift index 951eacf..9f10dc7 100644 --- a/Sources/RubyGateway/RbObjectCollection.swift +++ b/Sources/RubyGateway/RbObjectCollection.swift @@ -42,7 +42,7 @@ public struct RbObjectCollection: RandomAccessCollection, } public var startIndex: Int { - return 0 + 0 } public var endIndex: Int { @@ -55,7 +55,7 @@ public struct RbObjectCollection: RandomAccessCollection, public subscript(index: Int) -> RbObject { get { - return rubyObject[index] + rubyObject[index] } set { rubyObject[index] = newValue diff --git a/Sources/RubyGateway/RbOperators.swift b/Sources/RubyGateway/RbOperators.swift index f58ff56..978af37 100644 --- a/Sources/RubyGateway/RbOperators.swift +++ b/Sources/RubyGateway/RbOperators.swift @@ -169,7 +169,7 @@ extension RbObject { /// /// - note: Calls Ruby `[]` and `[]=` methods. Crashes the process (`fatalError`) /// if anything goes wrong - Swift can't throw from subscripts yet. - public subscript(args: RbObjectConvertible...) -> RbObject { + public subscript(args: any RbObjectConvertible...) -> RbObject { get { do { return try call("[]", args: args) diff --git a/Sources/RubyGateway/RbProc.swift b/Sources/RubyGateway/RbProc.swift index a9e472d..d31a082 100644 --- a/Sources/RubyGateway/RbProc.swift +++ b/Sources/RubyGateway/RbProc.swift @@ -24,10 +24,10 @@ internal import RubyGatewayHelpers /// `RbObjectAccess.call(_:args:kwArgs:blockRetention:blockCall:)` directly, /// no need for either `RbProc` or `RbObject`. public struct RbProc: RbObjectConvertible { - private let sourceObject: RbObjectConvertible + private let sourceObject: any RbObjectConvertible /// Initialize from something that can be turned into a Ruby object. - public init(object: RbObjectConvertible) { + public init(object: any RbObjectConvertible) { sourceObject = object } diff --git a/Sources/RubyGateway/RbRational.swift b/Sources/RubyGateway/RbRational.swift index 64ccb3d..df2b089 100644 --- a/Sources/RubyGateway/RbRational.swift +++ b/Sources/RubyGateway/RbRational.swift @@ -68,7 +68,7 @@ public struct RbRational: RbObjectConvertible { /// ```swift /// let rat = RbRational(0.3) /// ``` - public init?(_ value: RbObjectConvertible) { + public init?(_ value: any RbObjectConvertible) { self.init(value.rubyObject) } diff --git a/Sources/RubyGateway/RbSymbol.swift b/Sources/RubyGateway/RbSymbol.swift index da67ff0..7d255bf 100644 --- a/Sources/RubyGateway/RbSymbol.swift +++ b/Sources/RubyGateway/RbSymbol.swift @@ -30,7 +30,7 @@ public struct RbSymbol: RbObjectConvertible, Hashable { /// Always fails - no use for this, just use the `RbObject`. /// :nodoc: public init?(_ value: RbObject) { - return nil + nil } /// A Ruby object for the symbol @@ -48,6 +48,6 @@ public struct RbSymbol: RbObjectConvertible, Hashable { extension RbSymbol: CustomStringConvertible { /// A textual representation of the `RbSymbol` public var description: String { - return "RbSymbol(\(name))" + "RbSymbol(\(name))" } } diff --git a/Sources/RubyGateway/String+RubyGateway.swift b/Sources/RubyGateway/String+RubyGateway.swift index 337daa7..960709c 100644 --- a/Sources/RubyGateway/String+RubyGateway.swift +++ b/Sources/RubyGateway/String+RubyGateway.swift @@ -52,7 +52,7 @@ extension String { /// Does the string look like a Ruby global variable name? var isRubyGlobalVarName: Bool { - return starts(with: "$") + starts(with: "$") } /// Throw if the string does not look like a global variable name. @@ -62,7 +62,7 @@ extension String { /// Does the string look like a Ruby instance variable name? var isRubyInstanceVarName: Bool { - return starts(with: "@") && !isRubyClassVarName + starts(with: "@") && !isRubyClassVarName } /// Throw if the string does not look like an instance var name. @@ -72,7 +72,7 @@ extension String { /// Does the string look like a Ruby class variable name? var isRubyClassVarName: Bool { - return starts(with: "@@") + starts(with: "@@") } /// Throw if the string does not look like a class var name. @@ -82,7 +82,7 @@ extension String { /// Does the string look like a Ruby method name? var isRubyMethodName: Bool { - return !isRubyConstantName && !isRubyGlobalVarName && !isRubyInstanceVarName && !isRubyClassVarName + !isRubyConstantName && !isRubyGlobalVarName && !isRubyInstanceVarName && !isRubyClassVarName } /// Throw if the string does not look like a method name. diff --git a/Tests/RubyGatewayTests/Helpers.swift b/Tests/RubyGatewayTests/Helpers.swift index 40c645c..4c26a12 100644 --- a/Tests/RubyGatewayTests/Helpers.swift +++ b/Tests/RubyGatewayTests/Helpers.swift @@ -50,11 +50,13 @@ struct Helpers { /// that the test runner will treat that as a separate process.) /// Ruby files etc. - private static let fixturesDir: String = { URL(fileURLWithPath: #file).deletingLastPathComponent().path + "/Fixtures" }() + private static let fixturesDir: String = { + URL(fileURLWithPath: #file).deletingLastPathComponent().path + "/Fixtures" + }() /// Get full path to fixture with name static func fixturePath(_ name: String) -> String { - return "\(fixturesDir)/\(name)" + "\(fixturesDir)/\(name)" } /// A weird Swift type that has distinct Swift instances @@ -67,11 +69,11 @@ struct Helpers { } init?(_ value: RbObject) { - return nil + nil } var rubyObject: RbObject { - return RbObject(42) + RbObject(42) } } }