1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![warn(missing_docs)]
extern crate slog;
extern crate crossbeam;
use slog::*;
use std::sync::Arc;
use crossbeam::sync::ArcCell;
pub struct AtomicSwitchCtrl<O=(), E=slog::Never>(Arc<ArcCell<Box<SendSyncRefUnwindSafeDrain<Ok=O,Err=E>>>>);
pub struct AtomicSwitch<O=(), E=slog::Never>(Arc<ArcCell<Box<SendSyncRefUnwindSafeDrain<Ok=O,Err=E>>>>);
impl<O, E> AtomicSwitch<O, E> {
pub fn new<D: SendSyncRefUnwindSafeDrain<Ok = O, Err = E> + 'static>(drain: D) -> Self {
AtomicSwitch::new_from_arc(Arc::new(ArcCell::new(Arc::new(Box::new(drain) as Box<SendSyncRefUnwindSafeDrain<Ok=O,Err=E>>))))
}
pub fn new_from_arc(d: Arc<ArcCell<Box<SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>>>) -> Self {
AtomicSwitch(d)
}
pub fn ctrl(&self) -> AtomicSwitchCtrl<O, E> {
AtomicSwitchCtrl(self.0.clone())
}
}
impl<O, E> AtomicSwitchCtrl<O, E> {
pub fn get(&self) -> Arc<Box<SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>> {
self.0.get()
}
pub fn set<D: SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>(&self, drain: D) {
let _ = self.0.set(Arc::new(Box::new(drain)));
}
pub fn swap(&self,
drain: Arc<Box<SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>>)
-> Arc<Box<SendSyncRefUnwindSafeDrain<Ok = O, Err = E>>> {
self.0.set(drain)
}
pub fn drain(&self) -> AtomicSwitch<O, E> {
AtomicSwitch(self.0.clone())
}
}
impl<O, E> Drain for AtomicSwitch<O, E> {
type Ok = O;
type Err = E;
fn log(&self, info: &Record, kv: &OwnedKVList) -> std::result::Result<O, E> {
self.0.get().log(info, kv)
}
}