Skip to content

[SE-0127] stdlib: Remove the ManagedProtoBuffer base class #3690

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 51 additions & 57 deletions stdlib/public/core/ManagedBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,46 @@ public class NonObjectiveCBase {
public init() {}
}

/// A base class of `ManagedBuffer<Header, Element>`, used during
/// A class whose instances contain a property of type `Header` and raw
/// storage for an array of `Element`, whose size is determined at
/// instance creation.
///
/// During instance creation, in particular during
/// `ManagedBuffer.create`'s call to initialize, `ManagedBuffer`'s
/// `header` property is as-yet uninitialized, and therefore
/// `ManagedProtoBuffer` does not offer access to the as-yet
/// uninitialized `header` property of `ManagedBuffer`.
public class ManagedProtoBuffer<Header, Element> : NonObjectiveCBase {
/// Note that the `Element` array is suitably-aligned **raw memory**.
/// You are expected to construct and---if necessary---destroy objects
/// there yourself, using the APIs on `UnsafeMutablePointer<Element>`.
/// Typical usage stores a count and capacity in `Header` and destroys
/// any live elements in the `deinit` of a subclass.
/// - Note: Subclasses must not have any stored properties; any storage
/// needed should be included in `Header`.
public class ManagedBuffer<Header, Element> : NonObjectiveCBase {

/// Create a new instance of the most-derived class, calling
/// `factory` on the partially-constructed object to generate
/// an initial `Header`.
public final class func create(
minimumCapacity: Int,
makingHeaderWith factory: (
ManagedBuffer<Header, Element>) throws -> Header
) rethrows -> ManagedBuffer<Header, Element> {

let p = try ManagedBufferPointer<Header, Element>(
bufferClass: self,
minimumCapacity: minimumCapacity,
makingHeaderWith: { buffer, _ in
try factory(
unsafeDowncast(buffer, to: ManagedBuffer<Header, Element>.self))
})

return unsafeDowncast(p.buffer, to: ManagedBuffer<Header, Element>.self)
}

/// Destroy the stored Header.
deinit {
ManagedBufferPointer(self).withUnsafeMutablePointerToHeader {
_ = $0.deinitialize()
}
}

/// The actual number of elements that can be stored in this object.
///
/// This header may be nontrivial to compute; it is usually a good
Expand Down Expand Up @@ -72,56 +103,12 @@ public class ManagedProtoBuffer<Header, Element> : NonObjectiveCBase {
return try ManagedBufferPointer(self).withUnsafeMutablePointers(body)
}

//===--- internal/private API -------------------------------------------===//

/// Make ordinary initialization unavailable
internal init(_doNotCallMe: ()) {
_sanityCheckFailure("Only initialize these by calling create")
}
}

/// A class whose instances contain a property of type `Header` and raw
/// storage for an array of `Element`, whose size is determined at
/// instance creation.
///
/// Note that the `Element` array is suitably-aligned **raw memory**.
/// You are expected to construct and---if necessary---destroy objects
/// there yourself, using the APIs on `UnsafeMutablePointer<Element>`.
/// Typical usage stores a count and capacity in `Header` and destroys
/// any live elements in the `deinit` of a subclass.
/// - Note: Subclasses must not have any stored properties; any storage
/// needed should be included in `Header`.
public class ManagedBuffer<Header, Element>
: ManagedProtoBuffer<Header, Element> {

/// Create a new instance of the most-derived class, calling
/// `factory` on the partially-constructed object to generate
/// an initial `Header`.
public final class func create(
minimumCapacity: Int,
makingHeaderWith factory: (
ManagedProtoBuffer<Header, Element>) throws -> Header
) rethrows -> ManagedBuffer<Header, Element> {

let p = try ManagedBufferPointer<Header, Element>(
bufferClass: self,
minimumCapacity: minimumCapacity,
makingHeaderWith: { buffer, _ in
try factory(
unsafeDowncast(buffer, to: ManagedProtoBuffer<Header, Element>.self))
})

return unsafeDowncast(p.buffer, to: ManagedBuffer<Header, Element>.self)
}

/// Destroy the stored Header.
deinit {
ManagedBufferPointer(self).withUnsafeMutablePointerToHeader {
_ = $0.deinitialize()
}
}

/// The stored `Header` instance.
///
/// During instance creation, in particular during
/// `ManagedBuffer.create`'s call to initialize, `ManagedBuffer`'s
/// `header` property is as-yet uninitialized, and therefore
/// reading the `header` property during `ManagedBuffer.create` is undefined.
public final var header: Header {
addressWithNativeOwner {
return (
Expand All @@ -136,6 +123,13 @@ public class ManagedBuffer<Header, Element>
Builtin.castToNativeObject(self))
}
}

//===--- internal/private API -------------------------------------------===//

/// Make ordinary initialization unavailable
internal init(_doNotCallMe: ()) {
_sanityCheckFailure("Only initialize these by calling create")
}
}

/// Contains a buffer object, and provides access to an instance of
Expand Down Expand Up @@ -364,7 +358,7 @@ public struct ManagedBufferPointer<Header, Element> : Equatable {
///
/// - Note: It is an error to use the `header` property of the resulting
/// instance unless it has been initialized.
internal init(_ buffer: ManagedProtoBuffer<Header, Element>) {
internal init(_ buffer: ManagedBuffer<Header, Element>) {
_nativeBuffer = Builtin.castToNativeObject(buffer)
}

Expand Down
8 changes: 0 additions & 8 deletions test/1_stdlib/ManagedBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ func createSubclassOfNonObjectiveCBase() {
// Check that the generic parameters are called 'Header' and 'Element'.
protocol TestProtocol1 {}

extension ManagedProtoBuffer
where Header : TestProtocol1, Element : TestProtocol1 {

var _valueAndElementAreTestProtocol1: Bool {
fatalError("not implemented")
}
}

extension ManagedBuffer
where Header : TestProtocol1, Element : TestProtocol1 {

Expand Down