pi-async-task-queue 0.1.5

任务队列接口及默认实现
Documentation
// 当前文件落实配对声明中的局部操作。

#[allow(unused_variables)]
fn queue_new_inner<const ALLOW_UNBOUND: bool>(owner: &TaskThread) -> TaskQueue<ALLOW_UNBOUND> {
    TaskQueue::<ALLOW_UNBOUND> { owner: owner.id(), entries: LinkedList::new(QueueAdapter { links: LinkOps, pointers: DefaultPointerOps::new() }), count: 0, closed: false, local: PhantomData }
}

#[allow(unused_variables)]
fn queue_owner_inner<const ALLOW_UNBOUND: bool>(this: &TaskQueue<ALLOW_UNBOUND>) -> TaskOwnerId {
    this.owner
}

#[allow(unused_variables)]
fn queue_len_inner<const ALLOW_UNBOUND: bool>(this: &TaskQueue<ALLOW_UNBOUND>) -> usize {
    this.count
}

#[allow(unused_variables)]
fn queue_is_empty_inner<const ALLOW_UNBOUND: bool>(this: &TaskQueue<ALLOW_UNBOUND>) -> bool {
    this.count == 0
}

fn queue_new_with_unbound_inner(owner: &TaskThread) -> TaskQueue<true> {
    TaskQueue {
        owner: owner.id(),
        entries: LinkedList::new(QueueAdapter { links: LinkOps, pointers: DefaultPointerOps::new() }),
        count: 0,
        closed: false,
        local: PhantomData,
    }
}

fn queue_push_count_inner<const ALLOW_UNBOUND: bool, const FRONT: bool>(
    queue: &TaskQueue<ALLOW_UNBOUND>,
    task: &Runnable,
) -> std::result::Result<usize, QueuePushErrorKind> {
    // 未绑定的原值只能按约定尾插,接纳(admission)留给取出后负责推进的一方。
    match task.snapshot().owner {
        Some(owner) if owner == queue.owner => {}
        None if ALLOW_UNBOUND && !FRONT => {}
        _ => return Err(QueuePushErrorKind::WrongOwner),
    }
    queue.count.checked_add(1).ok_or(QueuePushErrorKind::CountOverflow)
}

fn queue_append_count_inner<const ALLOW_UNBOUND: bool>(
    queue: &TaskQueue<ALLOW_UNBOUND>,
    batch: &TaskBatch,
) -> std::result::Result<usize, QueuePushErrorKind> {
    let count = queue.count.checked_add(batch.tasks.len()).ok_or(QueuePushErrorKind::CountOverflow)?;
    if batch.tasks.iter().any(|task| task.snapshot().owner != Some(queue.owner)) {
        return Err(QueuePushErrorKind::WrongOwner);
    }
    Ok(count)
}

unsafe fn queue_link_inner<const ALLOW_UNBOUND: bool, const FRONT: bool>(
    queue: &mut TaskQueue<ALLOW_UNBOUND>,
    node: NonNull<QueueEntry>,
) {
    // SAFETY: 调用者独占队列,原保管槽持有本节点,未入链的链接可交给原生容器。
    let value = unsafe { UnsafeRef::from_raw(node.as_ptr()) };
    if FRONT {
        queue.entries.push_front(value);
    } else {
        queue.entries.push_back(value);
    }
}

fn queue_unlink_inner<const ALLOW_UNBOUND: bool, const FRONT: bool>(
    queue: &mut TaskQueue<ALLOW_UNBOUND>,
) -> Option<NonNull<QueueEntry>> {
    let node = if FRONT { queue.entries.pop_front() } else { queue.entries.pop_back() }?;
    queue.count = queue.count.checked_sub(1).unwrap_or_else(|| std::process::abort());
    Some(NonNull::from(&*node))
}

#[allow(unused_variables)]
unsafe fn adapter_value_inner(
    this: &QueueAdapter,
    link: NonNull<LinkedListLink>,
) -> *const QueueEntry {
    // SAFETY: 此链接(link)由同适配器(Adapter)的正向转换取得。
    unsafe { QueueEntry::node_from_link(link) }.as_ptr()
}
#[allow(unused_variables)]
unsafe fn adapter_link_inner(
    this: &QueueAdapter,
    value: *const QueueEntry,
) -> NonNull<LinkedListLink> {
    // SAFETY: 原生适配器(Adapter)提供有效且保活的节点(node),本转换不改变持有。
    unsafe { QueueEntry::link_from_node(NonNull::new(value.cast_mut()).unwrap_or_else(|| std::process::abort())) }
}
#[allow(unused_variables)]
fn adapter_ops_inner(this: &QueueAdapter) -> &LinkOps {
    &this.links
}
#[allow(unused_variables)]
fn adapter_ops_mut_inner(this: &mut QueueAdapter) -> &mut LinkOps {
    &mut this.links
}
#[allow(unused_variables)]
fn adapter_pointer_ops_inner(this: &QueueAdapter) -> &DefaultPointerOps<UnsafeRef<QueueEntry>> {
    &this.pointers
}