@@ -9,24 +9,24 @@ use crate::{error, info};
99const DEFAULT_TIMEOUT : Duration = Duration :: from_millis ( 5000 ) ;
1010
1111/// Policy state machine control
12- pub struct Policy < ' a , S : Kind > {
13- device : & ' a device:: Device ,
12+ pub struct Policy < ' a , S : Kind , const N : usize > {
13+ device : & ' a device:: Device < N > ,
1414 _state : core:: marker:: PhantomData < S > ,
1515}
1616
1717/// Enum to contain any state
18- pub enum AnyState < ' a > {
18+ pub enum AnyState < ' a , const N : usize > {
1919 /// Detached
20- Detached ( Policy < ' a , Detached > ) ,
20+ Detached ( Policy < ' a , Detached , N > ) ,
2121 /// Idle
22- Idle ( Policy < ' a , Idle > ) ,
22+ Idle ( Policy < ' a , Idle , N > ) ,
2323 /// Connected Consumer
24- ConnectedConsumer ( Policy < ' a , ConnectedConsumer > ) ,
24+ ConnectedConsumer ( Policy < ' a , ConnectedConsumer , N > ) ,
2525 /// Connected Provider
26- ConnectedProvider ( Policy < ' a , ConnectedProvider > ) ,
26+ ConnectedProvider ( Policy < ' a , ConnectedProvider , N > ) ,
2727}
2828
29- impl AnyState < ' _ > {
29+ impl < const N : usize > AnyState < ' _ , N > {
3030 /// Return the kind of the contained state
3131 pub fn kind ( & self ) -> StateKind {
3232 match self {
@@ -38,9 +38,9 @@ impl AnyState<'_> {
3838 }
3939}
4040
41- impl < ' a , S : Kind > Policy < ' a , S > {
41+ impl < ' a , S : Kind , const N : usize > Policy < ' a , S , N > {
4242 /// Create a new state machine
43- pub ( crate ) fn new ( device : & ' a device:: Device ) -> Self {
43+ pub ( crate ) fn new ( device : & ' a device:: Device < N > ) -> Self {
4444 Self {
4545 device,
4646 _state : core:: marker:: PhantomData ,
@@ -97,14 +97,14 @@ impl<'a, S: Kind> Policy<'a, S> {
9797}
9898
9999// The policy can do nothing when no device is attached
100- impl Policy < ' _ , Detached > { }
100+ impl < const N : usize > Policy < ' _ , Detached , N > { }
101101
102- impl < ' a > Policy < ' a , Idle > {
102+ impl < ' a , const N : usize > Policy < ' a , Idle , N > {
103103 /// Connect this device as a consumer
104104 pub async fn connect_as_consumer_no_timeout (
105105 self ,
106106 capability : ConsumerPowerCapability ,
107- ) -> Result < Policy < ' a , ConnectedConsumer > , Error > {
107+ ) -> Result < Policy < ' a , ConnectedConsumer , N > , Error > {
108108 info ! ( "Device {} connecting as consumer" , self . device. id( ) . 0 ) ;
109109
110110 self . device
@@ -122,7 +122,7 @@ impl<'a> Policy<'a, Idle> {
122122 pub async fn connect_consumer (
123123 self ,
124124 capability : ConsumerPowerCapability ,
125- ) -> Result < Policy < ' a , ConnectedConsumer > , Error > {
125+ ) -> Result < Policy < ' a , ConnectedConsumer , N > , Error > {
126126 match with_timeout ( DEFAULT_TIMEOUT , self . connect_as_consumer_no_timeout ( capability) ) . await {
127127 Ok ( r) => r,
128128 Err ( TimeoutError ) => Err ( Error :: Timeout ) ,
@@ -133,7 +133,7 @@ impl<'a> Policy<'a, Idle> {
133133 pub async fn connect_provider_no_timeout (
134134 self ,
135135 capability : ProviderPowerCapability ,
136- ) -> Result < Policy < ' a , ConnectedProvider > , Error > {
136+ ) -> Result < Policy < ' a , ConnectedProvider , N > , Error > {
137137 self . connect_as_provider_internal_no_timeout ( capability)
138138 . await
139139 . map ( |_| Policy :: new ( self . device ) )
@@ -143,30 +143,30 @@ impl<'a> Policy<'a, Idle> {
143143 pub async fn connect_provider (
144144 self ,
145145 capability : ProviderPowerCapability ,
146- ) -> Result < Policy < ' a , ConnectedProvider > , Error > {
146+ ) -> Result < Policy < ' a , ConnectedProvider , N > , Error > {
147147 self . connect_provider_internal ( capability)
148148 . await
149149 . map ( |_| Policy :: new ( self . device ) )
150150 }
151151}
152152
153- impl < ' a > Policy < ' a , ConnectedConsumer > {
153+ impl < ' a , const N : usize > Policy < ' a , ConnectedConsumer , N > {
154154 /// Disconnect this device
155- pub async fn disconnect_no_timeout ( self ) -> Result < Policy < ' a , Idle > , Error > {
155+ pub async fn disconnect_no_timeout ( self ) -> Result < Policy < ' a , Idle , N > , Error > {
156156 self . disconnect_internal_no_timeout ( )
157157 . await
158158 . map ( |_| Policy :: new ( self . device ) )
159159 }
160160
161161 /// Disconnect this device
162- pub async fn disconnect ( self ) -> Result < Policy < ' a , Idle > , Error > {
162+ pub async fn disconnect ( self ) -> Result < Policy < ' a , Idle , N > , Error > {
163163 self . disconnect_internal ( ) . await . map ( |_| Policy :: new ( self . device ) )
164164 }
165165}
166166
167- impl < ' a > Policy < ' a , ConnectedProvider > {
167+ impl < ' a , const N : usize > Policy < ' a , ConnectedProvider , N > {
168168 /// Disconnect this device
169- pub async fn disconnect_no_timeout ( self ) -> Result < Policy < ' a , Idle > , Error > {
169+ pub async fn disconnect_no_timeout ( self ) -> Result < Policy < ' a , Idle , N > , Error > {
170170 if let Err ( e) = self . disconnect_internal_no_timeout ( ) . await {
171171 error ! ( "Error disconnecting device {}: {:?}" , self . device. id( ) . 0 , e) ;
172172 return Err ( e) ;
@@ -175,7 +175,7 @@ impl<'a> Policy<'a, ConnectedProvider> {
175175 }
176176
177177 /// Disconnect this device
178- pub async fn disconnect ( self ) -> Result < Policy < ' a , Idle > , Error > {
178+ pub async fn disconnect ( self ) -> Result < Policy < ' a , Idle , N > , Error > {
179179 match with_timeout ( DEFAULT_TIMEOUT , self . disconnect_no_timeout ( ) ) . await {
180180 Ok ( r) => r,
181181 Err ( TimeoutError ) => Err ( Error :: Timeout ) ,
@@ -186,7 +186,7 @@ impl<'a> Policy<'a, ConnectedProvider> {
186186 pub async fn connect_as_consumer_no_timeout (
187187 self ,
188188 capability : ConsumerPowerCapability ,
189- ) -> Result < Policy < ' a , ConnectedConsumer > , Error > {
189+ ) -> Result < Policy < ' a , ConnectedConsumer , N > , Error > {
190190 info ! ( "Device {} connecting as consumer" , self . device. id( ) . 0 ) ;
191191
192192 self . device
@@ -204,7 +204,7 @@ impl<'a> Policy<'a, ConnectedProvider> {
204204 pub async fn connect_consumer (
205205 self ,
206206 capability : ConsumerPowerCapability ,
207- ) -> Result < Policy < ' a , ConnectedConsumer > , Error > {
207+ ) -> Result < Policy < ' a , ConnectedConsumer , N > , Error > {
208208 match with_timeout ( DEFAULT_TIMEOUT , self . connect_as_consumer_no_timeout ( capability) ) . await {
209209 Ok ( r) => r,
210210 Err ( TimeoutError ) => Err ( Error :: Timeout ) ,
@@ -215,7 +215,7 @@ impl<'a> Policy<'a, ConnectedProvider> {
215215 pub async fn connect_provider_no_timeout (
216216 & self ,
217217 capability : ProviderPowerCapability ,
218- ) -> Result < Policy < ' a , ConnectedProvider > , Error > {
218+ ) -> Result < Policy < ' a , ConnectedProvider , N > , Error > {
219219 self . connect_as_provider_internal_no_timeout ( capability)
220220 . await
221221 . map ( |_| Policy :: new ( self . device ) )
@@ -225,7 +225,7 @@ impl<'a> Policy<'a, ConnectedProvider> {
225225 pub async fn connect_provider (
226226 & self ,
227227 capability : ProviderPowerCapability ,
228- ) -> Result < Policy < ' a , ConnectedProvider > , Error > {
228+ ) -> Result < Policy < ' a , ConnectedProvider , N > , Error > {
229229 self . connect_provider_internal ( capability)
230230 . await
231231 . map ( |_| Policy :: new ( self . device ) )
0 commit comments