Trying to fix rust compat

This commit is contained in:
Igor Katson 2024-11-20 16:13:37 +00:00
parent 69a638fb81
commit e5c11e778a
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 14 additions and 15 deletions

View file

@ -3,29 +3,28 @@ use governor::DefaultDirectRateLimiter as RateLimiter;
use governor::Quota; use governor::Quota;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use std::num::NonZero;
use std::num::NonZeroU32; use std::num::NonZeroU32;
use std::sync::Arc; use std::sync::Arc;
#[derive(Default, Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)] #[derive(Default, Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
pub struct LimitsConfig { pub struct LimitsConfig {
pub upload_bps: Option<NonZero<u32>>, pub upload_bps: Option<NonZeroU32>,
pub download_bps: Option<NonZero<u32>>, pub download_bps: Option<NonZeroU32>,
} }
struct Limit(ArcSwapOption<RateLimiter>); struct Limit(ArcSwapOption<RateLimiter>);
impl Limit { impl Limit {
fn new_inner(bps: Option<NonZero<u32>>) -> Option<Arc<RateLimiter>> { fn new_inner(bps: Option<NonZeroU32>) -> Option<Arc<RateLimiter>> {
let bps = bps?; let bps = bps?;
Some(Arc::new(RateLimiter::direct(Quota::per_second(bps)))) Some(Arc::new(RateLimiter::direct(Quota::per_second(bps))))
} }
fn new(bps: Option<NonZero<u32>>) -> Self { fn new(bps: Option<NonZeroU32>) -> Self {
Self(ArcSwapOption::new(Self::new_inner(bps))) Self(ArcSwapOption::new(Self::new_inner(bps)))
} }
async fn acquire(&self, size: NonZero<u32>) -> anyhow::Result<()> { async fn acquire(&self, size: NonZeroU32) -> anyhow::Result<()> {
let lim = self.0.load().clone(); let lim = self.0.load().clone();
if let Some(rl) = lim.as_ref() { if let Some(rl) = lim.as_ref() {
rl.until_n_ready(size).await?; rl.until_n_ready(size).await?;
@ -33,7 +32,7 @@ impl Limit {
Ok(()) Ok(())
} }
fn set(&self, limit: Option<NonZero<u32>>) { fn set(&self, limit: Option<NonZeroU32>) {
let new = Self::new_inner(limit); let new = Self::new_inner(limit);
self.0.swap(new); self.0.swap(new);
} }
@ -52,15 +51,15 @@ impl Limits {
} }
} }
pub async fn prepare_for_upload(&self, len: NonZero<u32>) -> anyhow::Result<()> { pub async fn prepare_for_upload(&self, len: NonZeroU32) -> anyhow::Result<()> {
self.up.acquire(len).await self.up.acquire(len).await
} }
pub async fn prepare_for_download(&self, len: NonZero<u32>) -> anyhow::Result<()> { pub async fn prepare_for_download(&self, len: NonZeroU32) -> anyhow::Result<()> {
self.down.acquire(len).await self.down.acquire(len).await
} }
pub fn set_upload_bps(&self, bps: Option<NonZero<u32>>) { pub fn set_upload_bps(&self, bps: Option<NonZeroU32>) {
self.up.set(bps); self.up.set(bps);
} }

View file

@ -46,7 +46,7 @@ pub mod stats;
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
net::SocketAddr, net::SocketAddr,
num::NonZero, num::NonZeroU32,
sync::{ sync::{
atomic::{AtomicBool, AtomicU64, Ordering}, atomic::{AtomicBool, AtomicU64, Ordering},
Arc, Arc,
@ -418,12 +418,12 @@ impl TorrentStateLive {
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
while let Some((tx, ci)) = rx.recv().await { while let Some((tx, ci)) = rx.recv().await {
self.ratelimits self.ratelimits
.prepare_for_upload(NonZero::new(ci.size).unwrap()) .prepare_for_upload(NonZeroU32::new(ci.size).unwrap())
.await?; .await?;
if let Some(session) = self.torrent.session.upgrade() { if let Some(session) = self.torrent.session.upgrade() {
session session
.ratelimits .ratelimits
.prepare_for_upload(NonZero::new(ci.size).unwrap()) .prepare_for_upload(NonZeroU32::new(ci.size).unwrap())
.await?; .await?;
} }
let _ = tx.send(WriterRequest::ReadChunkRequest(ci)); let _ = tx.send(WriterRequest::ReadChunkRequest(ci));
@ -1449,13 +1449,13 @@ impl PeerHandler {
self.state self.state
.ratelimits .ratelimits
.prepare_for_download(NonZero::new(request.length).unwrap()) .prepare_for_download(NonZeroU32::new(request.length).unwrap())
.await?; .await?;
if let Some(session) = self.state.torrent().session.upgrade() { if let Some(session) = self.state.torrent().session.upgrade() {
session session
.ratelimits .ratelimits
.prepare_for_download(NonZero::new(request.length).unwrap()) .prepare_for_download(NonZeroU32::new(request.length).unwrap())
.await?; .await?;
} }