Skip to content

Commit 717dbc5

Browse files
authored
Merge pull request #155 from rneswold/pr-issue154
Create driver traits
2 parents f022393 + 86e63a8 commit 717dbc5

27 files changed

Lines changed: 1184 additions & 1020 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ default-members = ["drmemd"]
88
resolver = "2"
99

1010
[workspace.dependencies]
11-
async-trait = { version = "0.1", default-features = false }
1211
chrono = { version = "0.4", default-features = false }
1312
futures = { version = "0.3", default-features = false }
1413
toml = { version = "0.8", default-features = false }

drivers/drmem-drv-ntp/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "drmem-drv-ntp"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
authors = ["Rich Neswold <rich.neswold@gmail.com>"]
55
edition = "2021"
66
homepage = "https://github.com/DrMemCS/drmem"
@@ -30,4 +30,4 @@ tracing-futures.default-features = false
3030
tracing-subscriber.workspace = true
3131
tracing-subscriber.default-features = false
3232

33-
drmem-api = { path = "../../drmem-api", version = "0.5" }
33+
drmem-api = { path = "../../drmem-api", version = "0.6" }

drivers/drmem-drv-ntp/src/lib.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use drmem_api::{
33
driver::{self, DriverConfig},
44
Error, Result,
55
};
6+
use std::convert::Infallible;
67
use std::future::Future;
78
use std::sync::Arc;
8-
use std::{convert::Infallible, pin::Pin};
99
use std::{
1010
net::{SocketAddr, SocketAddrV4},
1111
str,
@@ -110,13 +110,6 @@ pub struct Instance {
110110
seq: u16,
111111
}
112112

113-
pub struct Devices {
114-
d_state: driver::ReadOnlyDevice<bool>,
115-
d_source: driver::ReadOnlyDevice<String>,
116-
d_offset: driver::ReadOnlyDevice<f64>,
117-
d_delay: driver::ReadOnlyDevice<f64>,
118-
}
119-
120113
impl Instance {
121114
pub const NAME: &'static str = "ntp";
122115

@@ -364,14 +357,19 @@ impl Instance {
364357
}
365358
}
366359

367-
impl driver::API for Instance {
368-
type DeviceSet = Devices;
360+
pub struct Devices {
361+
d_state: driver::ReadOnlyDevice<bool>,
362+
d_source: driver::ReadOnlyDevice<String>,
363+
d_offset: driver::ReadOnlyDevice<f64>,
364+
d_delay: driver::ReadOnlyDevice<f64>,
365+
}
369366

370-
fn register_devices(
371-
core: driver::RequestChan,
367+
impl driver::Registrator for Devices {
368+
fn register_devices<'a>(
369+
core: &'a mut driver::RequestChan,
372370
_: &DriverConfig,
373371
max_history: Option<usize>,
374-
) -> Pin<Box<dyn Future<Output = Result<Self::DeviceSet>> + Send>> {
372+
) -> impl Future<Output = Result<Self>> + Send + 'a {
375373
// It's safe to use `.unwrap()` for these names because, in a
376374
// fully-tested, released version of this driver, we would
377375
// have seen and fixed any panics.
@@ -403,13 +401,17 @@ impl driver::API for Instance {
403401
})
404402
})
405403
}
404+
}
405+
406+
impl driver::API for Instance {
407+
type HardwareType = Devices;
406408

407409
fn create_instance(
408410
cfg: &DriverConfig,
409-
) -> Pin<Box<dyn Future<Output = Result<Box<Self>>> + Send>> {
411+
) -> impl Future<Output = Result<Box<Self>>> + Send {
410412
let addr = Instance::get_cfg_address(cfg);
411413

412-
let fut = async move {
414+
async move {
413415
// Validate the configuration.
414416

415417
let addr = addr?;
@@ -423,16 +425,14 @@ impl driver::API for Instance {
423425
}
424426
}
425427
Err(Error::OperationError("couldn't create socket".to_owned()))
426-
};
427-
428-
Box::pin(fut)
428+
}
429429
}
430430

431431
fn run<'a>(
432432
&'a mut self,
433-
devices: Arc<Mutex<Devices>>,
434-
) -> Pin<Box<dyn Future<Output = Infallible> + Send + 'a>> {
435-
let fut = async move {
433+
devices: Arc<Mutex<Self::HardwareType>>,
434+
) -> impl Future<Output = Infallible> + Send + 'a {
435+
async move {
436436
// Record the peer's address in the "cfg" field of the
437437
// span.
438438

@@ -504,9 +504,7 @@ impl driver::API for Instance {
504504
devices.d_state.report_update(false).await;
505505
}
506506
}
507-
};
508-
509-
Box::pin(fut)
507+
}
510508
}
511509
}
512510

drivers/drmem-drv-sump/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "drmem-drv-sump"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
authors = ["Rich Neswold <rich.neswold@gmail.com>"]
55
edition = "2021"
66
homepage = "https://github.com/DrMemCS/drmem"
@@ -33,4 +33,4 @@ tracing-futures.default-features = false
3333
tracing-subscriber.workspace = true
3434
tracing-subscriber.default-features = false
3535

36-
drmem-api = { path = "../../drmem-api", version = "0.5" }
36+
drmem-api = { path = "../../drmem-api", version = "0.6" }

drivers/drmem-drv-sump/src/lib.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use drmem_api::{
33
driver::{self, DriverConfig},
44
Error, Result,
55
};
6+
use std::convert::Infallible;
67
use std::future::Future;
78
use std::net::SocketAddrV4;
89
use std::sync::Arc;
9-
use std::{convert::Infallible, pin::Pin};
1010
use tokio::{
1111
io::{self, AsyncReadExt},
1212
net::{
@@ -150,14 +150,6 @@ pub struct Instance {
150150
_tx: OwnedWriteHalf,
151151
}
152152

153-
pub struct Devices {
154-
d_service: driver::ReadOnlyDevice<bool>,
155-
d_state: driver::ReadOnlyDevice<bool>,
156-
d_duty: driver::ReadOnlyDevice<f64>,
157-
d_inflow: driver::ReadOnlyDevice<f64>,
158-
d_duration: driver::ReadOnlyDevice<f64>,
159-
}
160-
161153
impl Instance {
162154
pub const NAME: &'static str = "sump-gpio";
163155

@@ -282,14 +274,20 @@ impl Instance {
282274
}
283275
}
284276

285-
impl driver::API for Instance {
286-
type DeviceSet = Devices;
277+
pub struct Devices {
278+
d_service: driver::ReadOnlyDevice<bool>,
279+
d_state: driver::ReadOnlyDevice<bool>,
280+
d_duty: driver::ReadOnlyDevice<f64>,
281+
d_inflow: driver::ReadOnlyDevice<f64>,
282+
d_duration: driver::ReadOnlyDevice<f64>,
283+
}
287284

288-
fn register_devices(
289-
core: driver::RequestChan,
285+
impl driver::Registrator for Devices {
286+
fn register_devices<'a>(
287+
core: &'a mut driver::RequestChan,
290288
_: &DriverConfig,
291289
max_history: Option<usize>,
292-
) -> Pin<Box<dyn Future<Output = Result<Self::DeviceSet>> + Send>> {
290+
) -> impl Future<Output = Result<Self>> + Send + 'a {
293291
let service_name = "service".parse::<device::Base>().unwrap();
294292
let state_name = "state".parse::<device::Base>().unwrap();
295293
let duty_name = "duty".parse::<device::Base>().unwrap();
@@ -322,14 +320,18 @@ impl driver::API for Instance {
322320
})
323321
})
324322
}
323+
}
324+
325+
impl driver::API for Instance {
326+
type HardwareType = Devices;
325327

326328
fn create_instance(
327329
cfg: &DriverConfig,
328-
) -> Pin<Box<dyn Future<Output = Result<Box<Self>>> + Send>> {
330+
) -> impl Future<Output = Result<Box<Self>>> + Send {
329331
let addr = Instance::get_cfg_address(cfg);
330332
let gpm = Instance::get_cfg_gpm(cfg);
331333

332-
let fut = async move {
334+
async move {
333335
// Validate the configuration.
334336

335337
let addr = addr?;
@@ -348,16 +350,14 @@ impl driver::API for Instance {
348350
rx,
349351
_tx,
350352
}))
351-
};
352-
353-
Box::pin(fut)
353+
}
354354
}
355355

356356
fn run<'a>(
357357
&'a mut self,
358-
devices: Arc<Mutex<Devices>>,
359-
) -> Pin<Box<dyn Future<Output = Infallible> + Send + 'a>> {
360-
let fut = async move {
358+
devices: Arc<Mutex<Self::HardwareType>>,
359+
) -> impl Future<Output = Infallible> + Send + 'a {
360+
async move {
361361
// Record the peer's address in the "cfg" field of the
362362
// span.
363363

@@ -415,9 +415,7 @@ impl driver::API for Instance {
415415
}
416416
}
417417
}
418-
};
419-
420-
Box::pin(fut)
418+
}
421419
}
422420
}
423421

drivers/drmem-drv-tplink/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "drmem-drv-tplink"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
authors = ["Rich Neswold <rich.neswold@gmail.com>"]
55
edition = "2021"
66
homepage = "https://github.com/DrMemCS/drmem"
@@ -45,4 +45,4 @@ serde_json.workspace = true
4545
serde_json.default-features = false
4646
serde_json.features = ["std"]
4747

48-
drmem-api = { path = "../../drmem-api", version = "0.5" }
48+
drmem-api = { path = "../../drmem-api", version = "0.6" }

0 commit comments

Comments
 (0)