Type Alias SharedDevice

Source
pub type SharedDevice = Arc<Device>;
Expand description

Type alias for a shared device reference.

This is the recommended way to share a Device across multiple tasks. Since Device intentionally does not implement Clone (to prevent connection ownership ambiguity), wrapping it in Arc is the standard pattern for concurrent access.

§Choosing the Right Device Type

This crate provides several device types for different use cases:

TypeUse CaseAuto-ReconnectThread-Safe
DeviceSingle command, short-livedNoYes (via Arc)
ReconnectingDeviceLong-running appsYesYes
SharedDeviceSharing Device across tasksNoYes
DeviceManagerManaging multiple devicesYesYes

§Decision Guide

§Use Device when:

  • Running a single command (read, history download)
  • Connection lifetime is short and well-defined
  • You’ll handle reconnection yourself
use aranet_core::Device;
let device = Device::connect("Aranet4 12345").await?;
let reading = device.read_current().await?;
device.disconnect().await?;

§Use ReconnectingDevice when:

  • Building a long-running application (daemon, service)
  • You want automatic reconnection on connection loss
  • Continuous monitoring over extended periods
use aranet_core::{AranetDevice, ReconnectingDevice, ReconnectOptions};
let options = ReconnectOptions::default();
let device = ReconnectingDevice::connect("Aranet4 12345", options).await?;
// Will auto-reconnect on connection loss
let reading = device.read_current().await?;

§Use SharedDevice when:

  • Sharing a single Device across multiple async tasks
  • You need concurrent reads but want one connection
use aranet_core::{Device, SharedDevice};
use std::sync::Arc;

let device = Device::connect("Aranet4 12345").await?;
let shared: SharedDevice = Arc::new(device);

let shared_clone = Arc::clone(&shared);
tokio::spawn(async move {
    let reading = shared_clone.read_current().await;
});

§Use DeviceManager when:

  • Managing multiple devices simultaneously
  • Need centralized connection/disconnection handling
  • Building a multi-device monitoring application
use aranet_core::DeviceManager;
let manager = DeviceManager::new();
manager.add_device("AA:BB:CC:DD:EE:FF").await?;
manager.add_device("11:22:33:44:55:66").await?;
// Manager handles connections for all devices

Aliased Type§

pub struct SharedDevice { /* private fields */ }