Skip to content

Commit

Permalink
Swift code modernization, adopt explicit any
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfairh committed Apr 16, 2024
1 parent 4fac65a commit 9c1c3fb
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 142 deletions.
6 changes: 3 additions & 3 deletions Sources/RubyGateway/CRubyMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Sources/RubyGateway/RbBlockCall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<RbBlockContext>.fromOpaque(raw).takeUnretainedValue()
Unmanaged<RbBlockContext>.fromOpaque(raw).takeUnretainedValue()
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/RubyGateway/RbClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private protocol RbBoundClassProtocol {
}

private struct RbBoundClass<T: AnyObject> : RbBoundClassProtocol {
var initializer: () -> T
let initializer: () -> T

func createInstance() -> UnsafeMutableRawPointer {
let instance = initializer()
Expand Down
2 changes: 1 addition & 1 deletion Sources/RubyGateway/RbComplex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
18 changes: 9 additions & 9 deletions Sources/RubyGateway/RbConversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
Expand All @@ -517,7 +517,7 @@ private func decodeRange<T>(_ object: RbObject, halfOpen: Bool) -> (T, T)? where

/// Helper to create a Ruby Range from Swift types.
private func makeRange<T>(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...
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
17 changes: 8 additions & 9 deletions Sources/RubyGateway/RbError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand 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)"
}
}
}
Expand All @@ -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
}
}
Expand Down
82 changes: 41 additions & 41 deletions Sources/RubyGateway/RbFailableAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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<String, RbObjectConvertible?> = [:]) -> RbObject? {
return try? access.call(method, args: args, kwArgs: kwArgs)
args: [(any RbObjectConvertible)?] = [],
kwArgs: KeyValuePairs<String, (any RbObjectConvertible)?> = [:]) -> RbObject? {
try? access.call(method, args: args, kwArgs: kwArgs)
}

/// Call a method of a Ruby object passing Swift code as a block.
Expand All @@ -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<String, RbObjectConvertible?> = [:],
args: [(any RbObjectConvertible)?] = [],
kwArgs: KeyValuePairs<String, (any RbObjectConvertible)?> = [:],
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.
Expand All @@ -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<String, RbObjectConvertible?> = [:],
block: RbObjectConvertible) -> RbObject? {
return try? access.call(method, args: args, kwArgs: kwArgs, block: block)
args: [(any RbObjectConvertible)?] = [],
kwArgs: KeyValuePairs<String, (any RbObjectConvertible)?> = [:],
block: any RbObjectConvertible) -> RbObject? {
try? access.call(method, args: args, kwArgs: kwArgs, block: block)
}

/// Call a method of a Ruby object using a symbol.
Expand All @@ -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<String, RbObjectConvertible?> = [:]) -> RbObject? {
return try? access.call(symbol: symbol, args: args, kwArgs: kwArgs)
public func call(symbol: any RbObjectConvertible,
args: [(any RbObjectConvertible)?] = [],
kwArgs: KeyValuePairs<String, (any RbObjectConvertible)?> = [:]) -> 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.
Expand All @@ -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<String, RbObjectConvertible?> = [:],
public func call(symbol: any RbObjectConvertible,
args: [(any RbObjectConvertible)?] = [],
kwArgs: KeyValuePairs<String, (any RbObjectConvertible)?> = [:],
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.
Expand All @@ -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<String, RbObjectConvertible?> = [:],
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<String, (any RbObjectConvertible)?> = [:],
block: any RbObjectConvertible) -> RbObject? {
try? access.call(symbol: symbol, args: args, kwArgs: kwArgs, block: block)
}
}

Expand All @@ -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.
Expand All @@ -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)
}
}

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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)
}
}

Expand All @@ -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.
Expand All @@ -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)
}
}

Expand All @@ -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.
Expand All @@ -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)
}
}

Expand All @@ -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.
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions Sources/RubyGateway/RbGateway.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 9c1c3fb

Please sign in to comment.