Skip to content

Commit 3ca0074

Browse files
committed
fix lint.
1 parent d91012a commit 3ca0074

28 files changed

+190
-192
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ description = "rsocket-rust is an implementation of the RSocket protocol in Rust
1212
[dependencies]
1313
log = "0.4.8"
1414
bytes = "0.4.12"
15-
futures = "0.1.28"
15+
futures = "0.1.29"
1616
tokio = "0.1.22"
1717

1818
[dev-dependencies]

src/core/callers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Future for RequestCaller {
2222
type Error = RSocketError;
2323

2424
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
25-
self.rx.poll().map_err(|e| RSocketError::from(e))
25+
self.rx.poll().map_err(RSocketError::from)
2626
}
2727
}
2828

src/core/socket.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ use std::sync::{Arc, Mutex, RwLock};
2020
use tokio::net::TcpStream;
2121
use tokio::runtime::Runtime;
2222

23+
type AcceptorGenerator = Arc<fn(SetupPayload, Box<dyn RSocket>) -> Box<dyn RSocket>>;
24+
2325
pub enum Acceptor {
2426
Direct(Box<dyn RSocket>),
25-
Generate(Arc<fn(SetupPayload, Box<dyn RSocket>) -> Box<dyn RSocket>>),
27+
Generate(AcceptorGenerator),
2628
Empty(),
2729
}
2830

@@ -67,11 +69,11 @@ impl Runner {
6769
Acceptor::Generate(x) => (Responder::new(), Acceptor::Generate(x)),
6870
};
6971
Runner {
70-
tx: tx,
71-
handlers: handlers,
72-
acceptor: acceptor,
73-
responder: responder,
74-
socket: socket,
72+
tx,
73+
handlers,
74+
acceptor,
75+
responder,
76+
socket,
7577
}
7678
}
7779

@@ -172,8 +174,8 @@ impl Runner {
172174
}
173175

174176
#[inline]
175-
fn to_future(self, rx: mpsc::Receiver<Frame>) -> impl Future<Item = (), Error = ()> + Send {
176-
let task = rx.for_each(move |f| {
177+
fn gen_task(self, rx: mpsc::Receiver<Frame>) -> impl Future<Item = (), Error = ()> + Send {
178+
rx.for_each(move |f| {
177179
let sid = f.get_stream_id();
178180
let flag = f.get_flag();
179181
debug!("incoming frame#{}", sid);
@@ -203,7 +205,6 @@ impl Runner {
203205
match handler {
204206
Handler::Request(sender) => {
205207
tx1 = Some(sender);
206-
()
207208
}
208209
Handler::Stream(sender) => {
209210
if flag & frame::FLAG_NEXT != 0 {
@@ -212,7 +213,6 @@ impl Runner {
212213
if flag & frame::FLAG_COMPLETE == 0 {
213214
senders.insert(sid, Handler::Stream(sender));
214215
}
215-
()
216216
}
217217
};
218218

@@ -247,9 +247,7 @@ impl Runner {
247247
_ => unimplemented!(),
248248
};
249249
Ok(())
250-
});
251-
252-
task
250+
})
253251
}
254252
}
255253

@@ -280,11 +278,11 @@ impl DuplexSocket {
280278

281279
let sk = DuplexSocket {
282280
tx: tx.clone(),
283-
handlers: handlers,
281+
handlers,
284282
seq: StreamID::from(first_stream_id),
285283
};
286284

287-
let task = Runner::new(tx, handlers2, responder, sk.clone()).to_future(rx);
285+
let task = Runner::new(tx, handlers2, responder, sk.clone()).gen_task(rx);
288286
let fu = lazy(move || {
289287
tokio::spawn(task0);
290288
task
@@ -339,10 +337,7 @@ impl RSocket for DuplexSocket {
339337
}
340338
let sending = bu.build();
341339
let tx = self.tx.clone();
342-
let fu = tx
343-
.send(sending)
344-
.map(|_| ())
345-
.map_err(|e| RSocketError::from(e));
340+
let fu = tx.send(sending).map(|_| ()).map_err(RSocketError::from);
346341
Box::new(fu)
347342
}
348343

@@ -358,10 +353,7 @@ impl RSocket for DuplexSocket {
358353
}
359354
let sending = bu.build();
360355
let tx = self.tx.clone();
361-
let fu = tx
362-
.send(sending)
363-
.map(|_| ())
364-
.map_err(|e| RSocketError::from(e));
356+
let fu = tx.send(sending).map(|_| ()).map_err(RSocketError::from);
365357
Box::new(fu)
366358
}
367359

@@ -428,7 +420,7 @@ impl DuplexSocketBuilder {
428420
self
429421
}
430422

431-
pub fn from_socket(
423+
pub fn with_socket(
432424
self,
433425
socket: TcpStream,
434426
) -> (DuplexSocket, impl Future<Item = (), Error = ()>) {

src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl fmt::Display for RSocketError {
4242

4343
impl From<ErrorKind> for RSocketError {
4444
fn from(kind: ErrorKind) -> RSocketError {
45-
RSocketError { kind: kind }
45+
RSocketError { kind }
4646
}
4747
}
4848

src/frame/cancel.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
use super::{Body, Frame};
22

3-
#[derive(Debug)]
3+
#[derive(Debug, PartialEq)]
44
pub struct Cancel {}
55

6+
pub struct CancelBuilder {
7+
stream_id: u32,
8+
flag: u16,
9+
}
10+
11+
impl CancelBuilder {
12+
pub fn build(self) -> Frame {
13+
Frame::new(self.stream_id, Body::Cancel(), self.flag)
14+
}
15+
}
16+
617
impl Cancel {
7-
pub fn new(stream_id: u32, flag: u16) -> Frame {
8-
Frame::new(stream_id, Body::Cancel(), flag)
18+
pub fn builder(stream_id: u32, flag: u16) -> CancelBuilder {
19+
CancelBuilder { stream_id, flag }
920
}
1021
}

src/frame/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{Body, Frame, Writeable};
44
use crate::result::RSocketResult;
55
use bytes::{BigEndian, BufMut, ByteOrder, Bytes, BytesMut};
66

7-
#[derive(Debug)]
7+
#[derive(Debug, PartialEq)]
88
pub struct Error {
99
code: u32,
1010
data: Option<Bytes>,
@@ -77,9 +77,9 @@ impl Writeable for Error {
7777
}
7878
}
7979

80-
fn len(&self) -> u32 {
80+
fn len(&self) -> usize {
8181
4 + match &self.data {
82-
Some(v) => v.len() as u32,
82+
Some(v) => v.len(),
8383
None => 0,
8484
}
8585
}

src/frame/keepalive.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{Body, Frame, Writeable};
44
use crate::result::RSocketResult;
55
use bytes::{BigEndian, BufMut, ByteOrder, Bytes, BytesMut};
66

7-
#[derive(Debug)]
7+
#[derive(Debug, PartialEq)]
88
pub struct Keepalive {
99
last_received_position: u64,
1010
data: Option<Bytes>,
@@ -69,10 +69,9 @@ impl Keepalive {
6969
&self.data
7070
}
7171

72-
pub fn split(self) -> (Option<Bytes>,Option<Bytes>){
73-
(self.data,None)
72+
pub fn split(self) -> (Option<Bytes>, Option<Bytes>) {
73+
(self.data, None)
7474
}
75-
7675
}
7776

7877
impl Writeable for Keepalive {
@@ -84,9 +83,9 @@ impl Writeable for Keepalive {
8483
}
8584
}
8685

87-
fn len(&self) -> u32 {
86+
fn len(&self) -> usize {
8887
8 + match &self.data {
89-
Some(v) => v.len() as u32,
88+
Some(v) => v.len(),
9089
None => 0,
9190
}
9291
}

src/frame/lease.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{Body, Frame, Writeable, FLAG_METADATA};
44
use crate::result::RSocketResult;
55
use bytes::{BigEndian, BufMut, ByteOrder, Bytes, BytesMut};
66

7-
#[derive(Debug, Clone)]
7+
#[derive(Debug, PartialEq)]
88
pub struct Lease {
99
ttl: u32,
1010
number_of_requests: u32,
@@ -47,7 +47,7 @@ impl LeaseBuilder {
4747
}
4848

4949
pub fn build(self) -> Frame {
50-
Frame::new(self.stream_id, Body::Lease(self.value.clone()), self.flag)
50+
Frame::new(self.stream_id, Body::Lease(self.value), self.flag)
5151
}
5252
}
5353

@@ -96,9 +96,9 @@ impl Writeable for Lease {
9696
}
9797
}
9898

99-
fn len(&self) -> u32 {
99+
fn len(&self) -> usize {
100100
8 + match &self.metadata {
101-
Some(v) => v.len() as u32,
101+
Some(v) => v.len(),
102102
None => 0,
103103
}
104104
}

src/frame/metadata_push.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{Body, Frame, Writeable};
44
use crate::result::RSocketResult;
55
use bytes::{BufMut, Bytes, BytesMut};
66

7-
#[derive(Debug, Clone)]
7+
#[derive(Debug, PartialEq)]
88
pub struct MetadataPush {
99
metadata: Option<Bytes>,
1010
}
@@ -48,10 +48,9 @@ impl MetadataPush {
4848
&self.metadata
4949
}
5050

51-
pub fn split(self) -> (Option<Bytes>,Option<Bytes>){
52-
(None,self.metadata)
51+
pub fn split(self) -> (Option<Bytes>, Option<Bytes>) {
52+
(None, self.metadata)
5353
}
54-
5554
}
5655

5756
impl Writeable for MetadataPush {
@@ -62,9 +61,9 @@ impl Writeable for MetadataPush {
6261
}
6362
}
6463

65-
fn len(&self) -> u32 {
64+
fn len(&self) -> usize {
6665
match &self.metadata {
67-
Some(v) => v.len() as u32,
66+
Some(v) => v.len(),
6867
None => 0,
6968
}
7069
}

src/frame/mod.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,30 @@ pub const TYPE_METADATA_PUSH: u16 = 0x0C;
6262
pub const TYPE_RESUME: u16 = 0x0D;
6363
pub const TYPE_RESUME_OK: u16 = 0x0E;
6464

65-
pub const ERR_INVALID_SETUP: u32 = 0x00000001;
66-
pub const ERR_UNSUPPORTED_SETUP: u32 = 0x00000002;
67-
pub const ERR_REJECT_SETUP: u32 = 0x00000003;
68-
pub const ERR_REJECT_RESUME: u32 = 0x00000004;
69-
pub const ERR_CONN_FAILED: u32 = 0x00000101;
70-
pub const ERR_CONN_CLOSED: u32 = 0x00000102;
71-
pub const ERR_APPLICATION: u32 = 0x00000201;
72-
pub const ERR_REJECTED: u32 = 0x00000202;
73-
pub const ERR_CANCELED: u32 = 0x00000203;
74-
pub const ERR_INVALID: u32 = 0x00000204;
75-
76-
pub const REQUEST_MAX: u32 = 2147483647;
77-
const LEN_HEADER: u32 = 6;
65+
pub const ERR_INVALID_SETUP: u32 = 0x0000_0001;
66+
pub const ERR_UNSUPPORTED_SETUP: u32 = 0x0000_0002;
67+
pub const ERR_REJECT_SETUP: u32 = 0x0000_0003;
68+
pub const ERR_REJECT_RESUME: u32 = 0x0000_0004;
69+
pub const ERR_CONN_FAILED: u32 = 0x0000_0101;
70+
pub const ERR_CONN_CLOSED: u32 = 0x0000_0102;
71+
pub const ERR_APPLICATION: u32 = 0x0000_0201;
72+
pub const ERR_REJECTED: u32 = 0x0000_0202;
73+
pub const ERR_CANCELED: u32 = 0x0000_0203;
74+
pub const ERR_INVALID: u32 = 0x0000_0204;
75+
76+
pub const REQUEST_MAX: u32 = 0x7FFF_FFFF; // 2147483647
77+
78+
const LEN_HEADER: usize = 6;
7879

7980
pub trait Writeable {
8081
fn write_to(&self, bf: &mut BytesMut);
81-
fn len(&self) -> u32;
82+
fn len(&self) -> usize;
83+
fn is_empty(&self) -> bool {
84+
self.len() == 0
85+
}
8286
}
8387

84-
#[derive(Debug)]
88+
#[derive(Debug, PartialEq)]
8589
pub enum Body {
8690
Setup(Setup),
8791
Lease(Lease),
@@ -99,7 +103,7 @@ pub enum Body {
99103
ResumeOK(ResumeOK),
100104
}
101105

102-
#[derive(Debug)]
106+
#[derive(Debug, PartialEq)]
103107
pub struct Frame {
104108
stream_id: u32,
105109
body: Body,
@@ -128,7 +132,7 @@ impl Writeable for Frame {
128132
}
129133
}
130134

131-
fn len(&self) -> u32 {
135+
fn len(&self) -> usize {
132136
// header len
133137
LEN_HEADER
134138
+ match &self.body {
@@ -167,20 +171,20 @@ impl Frame {
167171
b.advance(2);
168172
let (flag, kind) = (n & 0x03FF, (n & 0xFC00) >> 10);
169173
let body = match kind {
170-
TYPE_SETUP => Setup::decode(flag, b).map(|it| Body::Setup(it)),
171-
TYPE_REQUEST_RESPONSE => RequestResponse::decode(flag, b).map(|it| Body::RequestResponse(it)),
172-
TYPE_REQUEST_STREAM => RequestStream::decode(flag, b).map(|it| Body::RequestStream(it)),
173-
TYPE_REQUEST_CHANNEL => RequestChannel::decode(flag, b).map(|it| Body::RequestChannel(it)),
174-
TYPE_REQUEST_FNF => RequestFNF::decode(flag, b).map(|it| Body::RequestFNF(it)),
175-
TYPE_REQUEST_N => RequestN::decode(flag, b).map(|it| Body::RequestN(it)),
176-
TYPE_METADATA_PUSH => MetadataPush::decode(flag, b).map(|it| Body::MetadataPush(it)),
177-
TYPE_KEEPALIVE => Keepalive::decode(flag, b).map(|it| Body::Keepalive(it)),
178-
TYPE_PAYLOAD => Payload::decode(flag, b).map(|it| Body::Payload(it)),
179-
TYPE_LEASE => Lease::decode(flag, b).map(|it| Body::Lease(it)),
174+
TYPE_SETUP => Setup::decode(flag, b).map(Body::Setup),
175+
TYPE_REQUEST_RESPONSE => RequestResponse::decode(flag, b).map(Body::RequestResponse),
176+
TYPE_REQUEST_STREAM => RequestStream::decode(flag, b).map(Body::RequestStream),
177+
TYPE_REQUEST_CHANNEL => RequestChannel::decode(flag, b).map(Body::RequestChannel),
178+
TYPE_REQUEST_FNF => RequestFNF::decode(flag, b).map(Body::RequestFNF),
179+
TYPE_REQUEST_N => RequestN::decode(flag, b).map(Body::RequestN),
180+
TYPE_METADATA_PUSH => MetadataPush::decode(flag, b).map(Body::MetadataPush),
181+
TYPE_KEEPALIVE => Keepalive::decode(flag, b).map(Body::Keepalive),
182+
TYPE_PAYLOAD => Payload::decode(flag, b).map(Body::Payload),
183+
TYPE_LEASE => Lease::decode(flag, b).map(Body::Lease),
180184
TYPE_CANCEL => Ok(Body::Cancel()),
181-
TYPE_ERROR => Error::decode(flag, b).map(|it| Body::Error(it)),
182-
TYPE_RESUME_OK => ResumeOK::decode(flag, b).map(|it| Body::ResumeOK(it)),
183-
TYPE_RESUME => Resume::decode(flag, b).map(|it| Body::Resume(it)),
185+
TYPE_ERROR => Error::decode(flag, b).map(Body::Error),
186+
TYPE_RESUME_OK => ResumeOK::decode(flag, b).map(Body::ResumeOK),
187+
TYPE_RESUME => Resume::decode(flag, b).map(Body::Resume),
184188
_ => Err(RSocketError::from("illegal frame type")),
185189
};
186190
body.map(|it| Frame::new(sid, it, flag))
@@ -212,7 +216,7 @@ impl Frame {
212216
}
213217

214218
fn to_frame_type(body: &Body) -> u16 {
215-
return match body {
219+
match body {
216220
Body::Setup(_) => TYPE_SETUP,
217221
Body::Lease(_) => TYPE_LEASE,
218222
Body::Keepalive(_) => TYPE_KEEPALIVE,
@@ -227,5 +231,5 @@ fn to_frame_type(body: &Body) -> u16 {
227231
Body::MetadataPush(_) => TYPE_METADATA_PUSH,
228232
Body::Resume(_) => TYPE_RESUME,
229233
Body::ResumeOK(_) => TYPE_RESUME_OK,
230-
};
234+
}
231235
}

0 commit comments

Comments
 (0)