@@ -12,7 +12,9 @@ pub mod rw_lock_tokio;
1212pub mod rw_lock_tokio_mutex_std;
1313pub mod rw_lock_tokio_mutex_tokio;
1414
15- pub trait Repository < T > : Default + ' static {
15+ use std:: fmt:: Debug ;
16+
17+ pub trait Repository < T > : Debug + Default + Sized + ' static {
1618 fn get ( & self , key : & InfoHash ) -> Option < T > ;
1719 fn get_metrics ( & self ) -> TorrentsMetrics ;
1820 fn get_paginated ( & self , pagination : Option < & Pagination > ) -> Vec < ( InfoHash , T ) > ;
@@ -24,7 +26,7 @@ pub trait Repository<T>: Default + 'static {
2426}
2527
2628#[ allow( clippy:: module_name_repetitions) ]
27- pub trait RepositoryAsync < T > : Default + ' static {
29+ pub trait RepositoryAsync < T > : Debug + Default + Sized + ' static {
2830 fn get ( & self , key : & InfoHash ) -> impl std:: future:: Future < Output = Option < T > > + Send ;
2931 fn get_metrics ( & self ) -> impl std:: future:: Future < Output = TorrentsMetrics > + Send ;
3032 fn get_paginated ( & self , pagination : Option < & Pagination > ) -> impl std:: future:: Future < Output = Vec < ( InfoHash , T ) > > + Send ;
@@ -39,12 +41,36 @@ pub trait RepositoryAsync<T>: Default + 'static {
3941 ) -> impl std:: future:: Future < Output = ( bool , SwarmMetadata ) > + Send ;
4042}
4143
42- #[ derive( Default ) ]
44+ #[ derive( Default , Debug ) ]
45+ pub struct RwLockStd < T > {
46+ torrents : std:: sync:: RwLock < std:: collections:: BTreeMap < InfoHash , T > > ,
47+ }
48+
49+ #[ derive( Default , Debug ) ]
4350pub struct RwLockTokio < T > {
4451 torrents : tokio:: sync:: RwLock < std:: collections:: BTreeMap < InfoHash , T > > ,
4552}
4653
47- #[ derive( Default ) ]
48- pub struct RwLockStd < T > {
49- torrents : std:: sync:: RwLock < std:: collections:: BTreeMap < InfoHash , T > > ,
54+ impl < T > RwLockStd < T > {
55+ /// # Panics
56+ ///
57+ /// Panics if unable to get a lock.
58+ pub fn write (
59+ & self ,
60+ ) -> std:: sync:: RwLockWriteGuard < ' _ , std:: collections:: BTreeMap < torrust_tracker_primitives:: info_hash:: InfoHash , T > > {
61+ self . torrents . write ( ) . expect ( "it should get lock" )
62+ }
63+ }
64+
65+ impl < T > RwLockTokio < T > {
66+ pub fn write (
67+ & self ,
68+ ) -> impl std:: future:: Future <
69+ Output = tokio:: sync:: RwLockWriteGuard <
70+ ' _ ,
71+ std:: collections:: BTreeMap < torrust_tracker_primitives:: info_hash:: InfoHash , T > ,
72+ > ,
73+ > {
74+ self . torrents . write ( )
75+ }
5076}
0 commit comments