@@ -163,3 +163,54 @@ pub trait Transactional {
163
163
operations : & mut [ Operation < ' a > ] ,
164
164
) -> Result < ( ) , Self :: Error > ;
165
165
}
166
+
167
+ /// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
168
+ /// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
169
+ pub mod transactional {
170
+ use super :: { Operation , Read , Transactional , Write , WriteRead } ;
171
+
172
+ /// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
173
+ /// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
174
+ pub trait Default < E > { }
175
+
176
+ impl < E , S > Write for S
177
+ where
178
+ S : self :: Default < E > + Transactional < Error = E > ,
179
+ {
180
+ type Error = E ;
181
+
182
+ fn try_write ( & mut self , address : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
183
+ self . try_exec ( address, & mut [ Operation :: Write ( bytes) ] )
184
+ }
185
+ }
186
+
187
+ impl < E , S > Read for S
188
+ where
189
+ S : self :: Default < E > + Transactional < Error = E > ,
190
+ {
191
+ type Error = E ;
192
+
193
+ fn try_read ( & mut self , address : u8 , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
194
+ self . try_exec ( address, & mut [ Operation :: Read ( buffer) ] )
195
+ }
196
+ }
197
+
198
+ impl < E , S > WriteRead for S
199
+ where
200
+ S : self :: Default < E > + Transactional < Error = E > ,
201
+ {
202
+ type Error = E ;
203
+
204
+ fn try_write_read (
205
+ & mut self ,
206
+ address : u8 ,
207
+ bytes : & [ u8 ] ,
208
+ buffer : & mut [ u8 ] ,
209
+ ) -> Result < ( ) , Self :: Error > {
210
+ self . try_exec (
211
+ address,
212
+ & mut [ Operation :: Write ( bytes) , Operation :: Read ( buffer) ] ,
213
+ )
214
+ }
215
+ }
216
+ }
0 commit comments