Skip to content

NSNumber: update API for SE-0170 #1204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 16, 2017

Conversation

ianpartridge
Copy link
Contributor

@ianpartridge ianpartridge commented Sep 7, 2017

Int(truncating: NSNumber) is missing in swift-corelibs-foundation. This function was added as part of SE-0170.

This PR implements the init(truncating:) and init?(exactly:) APIs from SE-0170, and also deprecates init(_:) appropriately.

It seems we also lacked implementations of init(_:) for many of the primitive number types (e.g. Int8) to begin with so this PR also adds those.

@ianpartridge
Copy link
Contributor Author

@swift-ci please test

self = value
}

public func _bridgeToObjectiveC() -> NSNumber {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: should this (and other _bridgeToObjectiveC() functions) be marked @_semantics("convertToObjectiveC")?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For swift corelibs it is fine to omit, iirc that is just a hint not a strict enforcement

@discardableResult
static public func _conditionallyBridgeFromObjectiveC(_ source: _ObjectType, result: inout Int?) -> Bool {
result = source.intValue
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int?) -> Bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the @discardableResult annotation as this is not present in the overlay. Is that ok?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what that does for compiler methods

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it generates a warning, I couldn't see why ignoring that would ever be a good idea though.

self = number.boolValue
}

public init?(exactly number: NSNumber) {
Copy link
Contributor Author

@ianpartridge ianpartridge Sep 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd welcome a close review of this function @phausler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@@ -119,7 +119,7 @@ open class InputStream: Stream {

// reads up to length bytes into the supplied buffer, which must be at least of size len. Returns the actual number of bytes read.
open func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int {
return CFReadStreamRead(_stream, buffer, CFIndex(len._bridgeToObjectiveC()))
return CFReadStreamRead(_stream, buffer, CFIndex(truncating: len._bridgeToObjectiveC()))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is produced by the Xcode fixit. Could it just be return CFReadStreamRead(_stream, buffer, len) though?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If len is an Int and CFIndex is defined as public typealias CFIndex = Int why is there any bridging at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I wondered!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea let's drop the extra round trip bridge here

Copy link
Contributor Author

@ianpartridge ianpartridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feedback requested.

@@ -119,7 +119,7 @@ open class InputStream: Stream {

// reads up to length bytes into the supplied buffer, which must be at least of size len. Returns the actual number of bytes read.
open func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int {
return CFReadStreamRead(_stream, buffer, CFIndex(len._bridgeToObjectiveC()))
return CFReadStreamRead(_stream, buffer, CFIndex(truncating: len._bridgeToObjectiveC()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea let's drop the extra round trip bridge here

self = number.boolValue
}

public init?(exactly number: NSNumber) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@discardableResult
static public func _conditionallyBridgeFromObjectiveC(_ source: _ObjectType, result: inout Int?) -> Bool {
result = source.intValue
public static func _conditionallyBridgeFromObjectiveC(_ x: NSNumber, result: inout Int?) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what that does for compiler methods

self = value
}

public func _bridgeToObjectiveC() -> NSNumber {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For swift corelibs it is fine to omit, iirc that is just a hint not a strict enforcement

@@ -32,158 +32,523 @@ internal let kCFNumberSInt128Type = CFNumberType(rawValue: 17)!
internal let kCFNumberSInt128Type: CFNumberType = 17
#endif

extension Int8 : _ObjectTypeBridgeable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a faithful copy of the overlay, how much effort do you think will be required to make this shared tween the overlay and SCLF?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The protocol is called _ObjectiveCBridgeable in the overlay - giving it the same name here would be a start. The files will never be identical because SCLF has the class NSNumber definition which isn't in the overlay. But I think we can get them closer.

@ianpartridge
Copy link
Contributor Author

This should be merged after #1213.

@ianpartridge
Copy link
Contributor Author

@swift-ci please test

@spevans
Copy link
Contributor

spevans commented Sep 16, 2017

@swift-ci please test and merge

@swift-ci swift-ci merged commit 5198af1 into swiftlang:master Sep 16, 2017
@ianpartridge ianpartridge deleted the nsnumber-se-0170 branch September 18, 2017 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants