-
Notifications
You must be signed in to change notification settings - Fork 4
FoundationEssentials #50
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
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6a02751
FoundationEssentials
adam-fowler 8622a6f
import Dispatch
adam-fowler 632482a
Merge branch 'main' into foundation-essentials
adam-fowler d1f332c
Include notice detailing code from swift-foundation
adam-fowler 254d66c
Use runtime beta 2
adam-fowler f41a5d7
Copyright 2025
adam-fowler c361d97
Merge branch 'main' into foundation-essentials
adam-fowler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Hummingbird server framework project | ||
// | ||
// Copyright (c) 2021-2022 the Hummingbird authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
struct OutputBuffer<T>: ~Copyable // ~Escapable | ||
{ | ||
let start: UnsafeMutablePointer<T> | ||
let capacity: Int | ||
var initialized: Int = 0 | ||
|
||
deinit { | ||
// `self` always borrows memory, and it shouldn't have gotten here. | ||
// Failing to use `relinquishBorrowedMemory()` is an error. | ||
if initialized > 0 { | ||
fatalError() | ||
} | ||
} | ||
|
||
// precondition: pointer points to uninitialized memory for count elements | ||
init(initializing: UnsafeMutablePointer<T>, capacity: Int) { | ||
start = initializing | ||
self.capacity = capacity | ||
} | ||
} | ||
|
||
extension OutputBuffer { | ||
mutating func appendElement(_ value: T) { | ||
precondition(initialized < capacity, "Output buffer overflow") | ||
start.advanced(by: initialized).initialize(to: value) | ||
initialized &+= 1 | ||
} | ||
|
||
mutating func deinitializeLastElement() -> T? { | ||
guard initialized > 0 else { return nil } | ||
initialized &-= 1 | ||
return start.advanced(by: initialized).move() | ||
} | ||
} | ||
|
||
extension OutputBuffer { | ||
mutating func deinitialize() { | ||
let b = UnsafeMutableBufferPointer(start: start, count: initialized) | ||
b.deinitialize() | ||
initialized = 0 | ||
} | ||
} | ||
|
||
extension OutputBuffer { | ||
mutating func append<S>( | ||
from elements: S | ||
) -> S.Iterator where S: Sequence, S.Element == T { | ||
var iterator = elements.makeIterator() | ||
append(from: &iterator) | ||
return iterator | ||
} | ||
|
||
mutating func append( | ||
from elements: inout some IteratorProtocol<T> | ||
) { | ||
while initialized < capacity { | ||
guard let element = elements.next() else { break } | ||
start.advanced(by: initialized).initialize(to: element) | ||
initialized &+= 1 | ||
} | ||
} | ||
|
||
mutating func append( | ||
fromContentsOf source: some Collection<T> | ||
) { | ||
let count = source.withContiguousStorageIfAvailable { | ||
guard let sourceAddress = $0.baseAddress, !$0.isEmpty else { | ||
return 0 | ||
} | ||
let available = capacity &- initialized | ||
precondition( | ||
$0.count <= available, | ||
"buffer cannot contain every element from source." | ||
) | ||
let tail = start.advanced(by: initialized) | ||
tail.initialize(from: sourceAddress, count: $0.count) | ||
return $0.count | ||
} | ||
if let count { | ||
initialized &+= count | ||
return | ||
} | ||
|
||
let available = capacity &- initialized | ||
let tail = start.advanced(by: initialized) | ||
let suffix = UnsafeMutableBufferPointer(start: tail, count: available) | ||
var (iterator, copied) = source._copyContents(initializing: suffix) | ||
precondition( | ||
iterator.next() == nil, | ||
"buffer cannot contain every element from source." | ||
) | ||
assert(initialized + copied <= capacity) | ||
initialized &+= copied | ||
} | ||
|
||
mutating func moveAppend( | ||
fromContentsOf source: UnsafeMutableBufferPointer<T> | ||
) { | ||
guard let sourceAddress = source.baseAddress, !source.isEmpty else { | ||
return | ||
} | ||
let available = capacity &- initialized | ||
precondition( | ||
source.count <= available, | ||
"buffer cannot contain every element from source." | ||
) | ||
let tail = start.advanced(by: initialized) | ||
tail.moveInitialize(from: sourceAddress, count: source.count) | ||
initialized &+= source.count | ||
} | ||
|
||
mutating func moveAppend( | ||
fromContentsOf source: Slice<UnsafeMutableBufferPointer<T>> | ||
) { | ||
moveAppend(fromContentsOf: UnsafeMutableBufferPointer(rebasing: source)) | ||
} | ||
} | ||
|
||
extension OutputBuffer<UInt8> /* where T: BitwiseCopyable */ { | ||
|
||
mutating func appendBytes<Value /*: BitwiseCopyable */>( | ||
of value: borrowing Value, | ||
as: Value.Type | ||
) { | ||
precondition(_isPOD(Value.self)) | ||
let (q, r) = MemoryLayout<Value>.stride.quotientAndRemainder( | ||
dividingBy: MemoryLayout<T>.stride | ||
) | ||
precondition( | ||
r == 0, | ||
"Stride of Value must be divisible by stride of Element" | ||
) | ||
precondition( | ||
(capacity &- initialized) >= q, | ||
"buffer cannot contain every byte of value." | ||
) | ||
let p = UnsafeMutableRawPointer(start.advanced(by: initialized)) | ||
p.storeBytes(of: value, as: Value.self) | ||
initialized &+= q | ||
} | ||
} | ||
|
||
extension OutputBuffer { | ||
consuming func relinquishBorrowedMemory() -> UnsafeMutableBufferPointer<T> { | ||
let start = self.start | ||
let initialized = self.initialized | ||
discard self | ||
return .init(start: start, count: initialized) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Licenses out of date