Skip to content

[stdlib] Revise unsafe pointers documentation #6275

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 5 commits into from
Dec 22, 2016

Conversation

natecook1000
Copy link
Member

This revises and expands upon documentation for the standard library's
unsafe pointer types. This includes typed and raw pointers and buffers,
the MemoryLayout type, and some other top-level functions.

This revises and expands upon documentation for the standard library's
unsafe pointer types. This includes typed and raw pointers and buffers,
the MemoryLayout type, and some other top-level functions.
@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

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

A few thoughts from a quick read over while busing. I like the way you have really expanded the documentation.

My comments are mainly related to some places where there is a "jump in thought" that could be made explicit or clearer. I understood what you were trying to say, but I had to make an inference. IMO technical docs should minimize such inference as much as possible in the favor of absolute clarity.

+1!

///
/// - To convert an integer value from one type to another, use an initializer
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, this is a bit of a jump. I think you need a connective that communicates that you are provided a list of "other means" for common operations when people instinctively reach for unsafeBitCast.

There is no reason not to be more explicit... thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good note, thanks!

/// The result does not include any dynamically allocated or remote
/// storage. In particular, `MemoryLayout.size(ofValue: x)`, when `x` is an
/// instance of a class `C`, is the same regardless of how many stored
/// properties `C` has.
Copy link
Contributor

Choose a reason for hiding this comment

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

To me this is a bit weird. It is skirting around a class being a pointer. Why not just call that out in a subsequent sentence? Perhaps use the term "out of line storage"? Just a thought.

///
/// - Complexity: O(1).
/// - Returns: An iterator over the elements of this buffer.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really want to get rid of the Complexity marker here?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's the expected complexity for makeIterator()—we only document when it deviates.

Copy link
Contributor

Choose a reason for hiding this comment

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

SGTM!

@airspeedswift airspeedswift self-requested a review December 15, 2016 19:48
@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

@natecook1000
Copy link
Member Author

@swift-ci Please smoke test Linux platform

/// `Pointee` type. To access the stride, use
/// `MemoryLayout<Pointee>.stride`.
///
/// - SeeAlso: `MemoryLayout`
Copy link
Member Author

Choose a reason for hiding this comment

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

@atrick Do we need to say something about pointer alignment here? If lhs and rhs aren't properly aligned the result won't accurately describe the distance between them. (The same also applies to distance(to:).) Maybe something like:

If the two pointers are not properly aligned for the Pointee type, the result of lhs - rhs may not accurately measure the distance between them. To find the distance in bytes between two pointers, convert them to UnsafeRawPointer instances before subtracting.

Copy link
Contributor

Choose a reason for hiding this comment

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

The requirement seems redundant since any instance of these typed pointers already need to be aligned. Although I don't see any debug alignment checks, so it doesn't hurt to reiterate this requirement. Can you word it in a way that doesn't sound specific to this operator: e.g. "The two pointers are required to be properly aligned for their Pointee type. This ensures that the result accurately measures the distance..."

Copy link
Member

@airspeedswift airspeedswift left a comment

Choose a reason for hiding this comment

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

ltgm

// to avoid a hang in Foundation, which has the following setup:
// struct A { struct B { let x: UnsafeMutableBufferPointer<...> } let b: B }
/// You can use an `${Self}` instance in low level operations to eliminate
/// uniqueness checks and release mode bounds checks. Bounds checks are always
Copy link
Member

Choose a reason for hiding this comment

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

"release mode bounds checks" -> "bounds checks in release mode" maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!

@natecook1000
Copy link
Member Author

@swift-ci Please smoke test and merge

@natecook1000 natecook1000 merged commit 0a0c77e into swiftlang:master Dec 22, 2016
@natecook1000 natecook1000 deleted the nc-revise-pointers branch December 22, 2016 21:20
@natecook1000
Copy link
Member Author

Both tests passed, but the bot had an error during its call to merge: https://ci.swift.org/job/swift-PR-smoke-merge/1454/consoleText

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.

5 participants