moq-transport 0.14.2

Media over QUIC
Documentation
// SPDX-FileCopyrightText: 2024-2026 Cloudflare Inc., Luke Curley, Mike English and contributors
// SPDX-FileCopyrightText: 2023-2024 Luke Curley and contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::collections::VecDeque;

use super::Watch;

// TODO replace with mpsc or similar
pub struct Queue<T> {
	state: Watch<VecDeque<T>>,
}

impl<T> Clone for Queue<T> {
	fn clone(&self) -> Self {
		Self {
			state: self.state.clone(),
		}
	}
}

impl<T> Default for Queue<T> {
	fn default() -> Self {
		Self {
			state: Default::default(),
		}
	}
}

impl<T> Queue<T> {
	pub fn push(&self, item: T) {
		self.state.lock_mut().push_back(item);
	}

	pub async fn pop(&self) -> T {
		loop {
			let notify = {
				let queue = self.state.lock();
				if !queue.is_empty() {
					return queue.into_mut().pop_front().unwrap();
				}
				queue.changed()
			};

			notify.await
		}
	}
}