Skip to content

Commit 21911d1

Browse files
committed
Make Publisher uses dyn Subscription<X> trait as its observers' type. Improve codes by fixing clippy lint hints
1 parent 51c7458 commit 21911d1

File tree

9 files changed

+116
-102
lines changed

9 files changed

+116
-102
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fp_rust"
3-
version = "0.1.35"
3+
version = "0.1.36"
44
license = "MIT"
55
authors = ["JunYi JohnTeee Lee <[email protected]>"]
66
include = ["src/**/*.rs", "Cargo.toml"]

clippy.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cargo +nightly clippy

src/common.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ This is a convenience function that gets the `mut ref` of an element of the `Vec
7575
7676
*/
7777
pub fn get_mut<'a, T>(v: &'a mut Vec<T>, index: usize) -> Option<&'a mut T> {
78-
let mut i = 0;
79-
for elem in v {
78+
for (i, elem) in v.into_iter().enumerate() {
8079
if index == i {
8180
return Some(elem);
8281
}
83-
i += 1;
8482
}
8583

8684
None
@@ -145,7 +143,7 @@ for general purposes crossing over many modules of fpRust.
145143
This is an implementation of Observer Pattern of GoF, and inspired by Rx Subscription.
146144
147145
*/
148-
pub trait Subscription<X>: Send + Sync + 'static + PartialEq {
146+
pub trait Subscription<X>: Send + Sync + 'static + UniqueId<String> {
149147
/**
150148
The callback when `Subscription` received the broadcasted value.
151149
@@ -203,10 +201,10 @@ impl<T: Send + Sync + 'static> SubscriptionFunc<T> {
203201
.duration_since(UNIX_EPOCH)
204202
.expect("Time went backwards");
205203

206-
return SubscriptionFunc {
204+
SubscriptionFunc {
207205
id: format!("{:?}", since_the_epoch),
208206
receiver: RawReceiver::new(func),
209-
};
207+
}
210208
}
211209
}
212210

@@ -257,10 +255,10 @@ impl<T> RawReceiver<T> {
257255
258256
*/
259257
pub fn new(func: impl FnMut(Arc<T>) + Send + Sync + 'static) -> RawReceiver<T> {
260-
return RawReceiver {
258+
RawReceiver {
261259
func: Arc::new(Mutex::new(func)),
262260
_t: PhantomData,
263-
};
261+
}
264262
}
265263

266264
/**
@@ -304,9 +302,9 @@ impl RawFunc {
304302
where
305303
T: FnMut() + Send + Sync + 'static,
306304
{
307-
return RawFunc {
305+
RawFunc {
308306
func: Arc::new(Mutex::new(func)),
309-
};
307+
}
310308
}
311309

312310
/**

src/cor.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl<RETURN: Send + Sync + 'static, RECEIVE: Send + Sync + 'static> Cor<RETURN,
311311
// me MutexGuard lifetime block
312312
{
313313
let _me = this.clone();
314-
let mut me = _me.lock().unwrap();
314+
let me = _me.lock().unwrap();
315315
if !me.is_alive() {
316316
return None;
317317
}
@@ -347,7 +347,7 @@ impl<RETURN: Send + Sync + 'static, RECEIVE: Send + Sync + 'static> Cor<RETURN,
347347
}
348348
}
349349

350-
return None;
350+
None
351351
}
352352

353353
/**
@@ -365,7 +365,7 @@ impl<RETURN: Send + Sync + 'static, RECEIVE: Send + Sync + 'static> Cor<RETURN,
365365
366366
*/
367367
pub fn yield_none(this: Arc<Mutex<Cor<RETURN, RECEIVE>>>) -> Option<RECEIVE> {
368-
return Cor::yield_ref(this, None);
368+
Cor::yield_ref(this, None)
369369
}
370370

371371
/**
@@ -391,7 +391,7 @@ impl<RETURN: Send + Sync + 'static, RECEIVE: Send + Sync + 'static> Cor<RETURN,
391391
// me MutexGuard lifetime block
392392
{
393393
let _me = this.clone();
394-
let mut me = _me.lock().unwrap();
394+
let me = _me.lock().unwrap();
395395
if !me.is_alive() {
396396
return None;
397397
}
@@ -415,7 +415,7 @@ impl<RETURN: Send + Sync + 'static, RECEIVE: Send + Sync + 'static> Cor<RETURN,
415415
None => {}
416416
}
417417

418-
return None;
418+
None
419419
}
420420

421421
/**
@@ -499,23 +499,23 @@ impl<RETURN: Send + Sync + 'static, RECEIVE: Send + Sync + 'static> Cor<RETURN,
499499
Return `true` when it did started (no matter it has stopped or not)
500500
501501
*/
502-
pub fn is_started(&mut self) -> bool {
502+
pub fn is_started(&self) -> bool {
503503
let _started_alive = self.started_alive.clone();
504504
let started_alive = _started_alive.lock().unwrap();
505505
let &(ref started, _) = &*started_alive;
506-
return started.load(Ordering::SeqCst);
506+
started.load(Ordering::SeqCst)
507507
}
508508

509509
/**
510510
Is this `Cor` alive?
511511
Return `true` when it has started and not stopped yet.
512512
513513
*/
514-
pub fn is_alive(&mut self) -> bool {
514+
pub fn is_alive(&self) -> bool {
515515
let _started_alive = self.started_alive.clone();
516516
let started_alive = _started_alive.lock().unwrap();
517517
let &(_, ref alive) = &*started_alive;
518-
return alive.load(Ordering::SeqCst);
518+
alive.load(Ordering::SeqCst)
519519
}
520520

521521
/**
@@ -565,7 +565,7 @@ impl<RETURN: Send + Sync + 'static, RECEIVE: Send + Sync + 'static> Cor<RETURN,
565565
// do: (effect)();
566566
let _result = _op_ch_sender.lock().unwrap().send(CorOp {
567567
// cor: cor,
568-
result_ch_sender: result_ch_sender,
568+
result_ch_sender,
569569
val: given_as_request,
570570
});
571571
}

src/handler.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,23 @@ pub struct HandlerThread {
8181
handle: Arc<Mutex<Option<thread::JoinHandle<()>>>>,
8282
}
8383

84-
impl HandlerThread {
85-
pub fn new() -> HandlerThread {
86-
return HandlerThread {
84+
impl Default for HandlerThread {
85+
fn default() -> Self {
86+
HandlerThread {
8787
started_alive: Arc::new(Mutex::new((AtomicBool::new(false), AtomicBool::new(false)))),
8888
inner: Arc::new(HandlerThreadInner::new()),
8989

9090
handle: Arc::new(Mutex::new(None)),
91-
};
91+
}
92+
}
93+
}
94+
95+
impl HandlerThread {
96+
pub fn new() -> HandlerThread {
97+
Default::default()
9298
}
9399
pub fn new_with_mutex() -> Arc<Mutex<HandlerThread>> {
94-
return Arc::new(Mutex::new(HandlerThread::new()));
100+
Arc::new(Mutex::new(HandlerThread::new()))
95101
}
96102
}
97103

@@ -100,14 +106,14 @@ impl Handler for HandlerThread {
100106
let _started_alive = self.started_alive.clone();
101107
let started_alive = _started_alive.lock().unwrap();
102108
let &(ref started, _) = &*started_alive;
103-
return started.load(Ordering::SeqCst);
109+
started.load(Ordering::SeqCst)
104110
}
105111

106112
fn is_alive(&mut self) -> bool {
107113
let _started_alive = self.started_alive.clone();
108114
let started_alive = _started_alive.lock().unwrap();
109115
let &(_, ref alive) = &*started_alive;
110-
return alive.load(Ordering::SeqCst);
116+
alive.load(Ordering::SeqCst)
111117
}
112118

113119
fn start(&mut self) {
@@ -185,21 +191,21 @@ struct HandlerThreadInner {
185191

186192
impl HandlerThreadInner {
187193
pub fn new() -> HandlerThreadInner {
188-
return HandlerThreadInner {
194+
HandlerThreadInner {
189195
started: Arc::new(AtomicBool::new(false)),
190196
alive: Arc::new(AtomicBool::new(false)),
191197
q: Arc::new(<fpSync::BlockingQueue<RawFunc>>::new()),
192-
};
198+
}
193199
}
194200
}
195201

196202
impl Handler for HandlerThreadInner {
197203
fn is_started(&mut self) -> bool {
198-
return self.started.load(Ordering::SeqCst);
204+
self.started.load(Ordering::SeqCst)
199205
}
200206

201207
fn is_alive(&mut self) -> bool {
202-
return self.alive.load(Ordering::SeqCst);
208+
self.alive.load(Ordering::SeqCst)
203209
}
204210

205211
fn start(&mut self) {

src/maybe.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,43 @@ pub struct Maybe<T> {
2727
impl<T: Clone + 'static> Maybe<T> {
2828
pub fn option(&self) -> Option<T> {
2929
let r = &*self.r.lock().unwrap();
30-
return r.clone();
30+
r.clone()
3131
}
3232
pub fn unwrap(&self) -> T {
3333
let r = &*self.r.lock().unwrap();
34-
return r.clone().unwrap();
34+
r.clone().unwrap()
3535
}
3636
pub fn or(&self, val: T) -> T {
3737
let r = &*self.r.lock().unwrap();
38-
return r.clone().unwrap_or(val);
38+
r.clone().unwrap_or(val)
3939
}
4040
}
4141

4242
impl<T: 'static> Maybe<T> {
4343
pub fn just(r: Option<T>) -> Maybe<T> {
44-
return Maybe {
44+
Maybe {
4545
r: Arc::new(Mutex::new(r)),
46-
};
46+
}
4747
}
4848
pub fn of(r: Option<T>) -> Maybe<T> {
49-
return Maybe::just(r);
49+
Maybe::just(r)
5050
}
5151
pub fn val(r: T) -> Maybe<T> {
52-
return Maybe::just(Some(r));
52+
Maybe::just(Some(r))
5353
}
5454

5555
pub fn present(&self) -> bool {
5656
let r = &*self.r.lock().unwrap();
5757
match r {
58-
Some(_x) => return true,
59-
None => return false,
58+
Some(_x) => true,
59+
None => false,
6060
}
6161
}
6262
pub fn null(&self) -> bool {
6363
let r = &*self.r.lock().unwrap();
6464
match r {
65-
Some(_x) => return false,
66-
None => return true,
65+
Some(_x) => false,
66+
None => true,
6767
}
6868
}
6969
pub fn let_do<F>(&self, func: F)
@@ -82,42 +82,42 @@ impl<T: 'static> Maybe<T> {
8282
F: FnOnce(&Option<T>) -> Maybe<G>,
8383
{
8484
let r = &*self.r.lock().unwrap();
85-
return func(&r);
85+
func(&r)
8686
}
8787
pub fn map<F, G>(&self, func: F) -> Maybe<G>
8888
where
8989
F: FnOnce(&Option<T>) -> Option<G>,
9090
G: 'static,
9191
{
9292
let r = &*self.r.lock().unwrap();
93-
return Maybe::just(func(&r));
93+
Maybe::just(func(&r))
9494
}
9595
pub fn bind<F, G>(&self, func: F) -> Maybe<G>
9696
where
9797
F: FnOnce(&Option<T>) -> Option<G>,
9898
G: 'static,
9999
{
100-
return self.map(func);
100+
self.map(func)
101101
}
102102
pub fn then<F, G>(&self, func: F) -> Maybe<G>
103103
where
104104
F: FnOnce(&Option<T>) -> Option<G>,
105105
G: 'static,
106106
{
107-
return self.map(func);
107+
self.map(func)
108108
}
109109
pub fn chain<F, G>(&self, func: F) -> Maybe<G>
110110
where
111111
F: FnOnce(&Option<T>) -> Maybe<G>,
112112
{
113-
return self.fmap(func);
113+
self.fmap(func)
114114
}
115-
pub fn ap<F, G>(&self, maybe_func: Maybe<F>) -> Maybe<G>
115+
pub fn ap<F, G>(&self, maybe_func: &Maybe<F>) -> Maybe<G>
116116
where
117117
F: FnOnce(&Option<T>) -> Option<G> + Clone + 'static,
118118
G: 'static,
119119
{
120-
return maybe_func.chain(|f| self.map(f.clone().unwrap()));
120+
maybe_func.chain(|f| self.map(f.clone().unwrap()))
121121
}
122122
}
123123

@@ -166,7 +166,7 @@ fn test_maybe_flatmap() {
166166
assert_eq!(
167167
true,
168168
Maybe::val(1)
169-
.ap(Maybe::val(|x: &Option<i16>| if x.unwrap() > 0 {
169+
.ap(&Maybe::val(|x: &Option<i16>| if x.unwrap() > 0 {
170170
return Some(true);
171171
} else {
172172
return Some(false);

0 commit comments

Comments
 (0)