How doesn't it work? I suppose your implementation isn't "fair" for the waiting tasks since some task might be waiting forever for an item while others yoink all of the sent items.
The way I would implement this to be a fair channel would be like this
actor Channel<T> {
private var store = [T?] ()
private var suspendeds = [UnsafeContinuation <T?, Never>] ()
func put (_ u: T?) {
if !suspendeds.isEmpty {
suspendeds.removeFirst().resume(returning: u)
return
}
store.append(u)
}
func get () async -> T? {
if store.isEmpty {
return await withUnsafeContinuation {
suspendeds.append($0)
}
}
return store.removeFirst()
}
var suspendedsCount : Int {
suspendeds.count
}
}