Skip to content

Commit 729617b

Browse files
committed
proto: Internally use SendStream::new
Makes it so that SendStream is always constructed with SendStream::new. This is in preparation for subsequent commit which will make constructor non-trivial. Previously, `new` was `#[cfg(fuzzing)] pub`. Now, the `#[cfg(fuzzing)]` is removed and `pub` is changed to `pub(super)`. Fuzz code is modified to call a new `#[cfg(fuzzing)] pub fn new_for_fuzzing` delegate method.
1 parent 0699545 commit 729617b

File tree

4 files changed

+33
-93
lines changed

4 files changed

+33
-93
lines changed

fuzz/fuzz_targets/streams.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ fuzz_target!(|input: (StreamParams, Vec<Operation>)| {
5050
Streams::new(&mut state, &conn_state).accept(dir);
5151
}
5252
Operation::Finish(id) => {
53-
let _ = SendStream::new(id, &mut state, &mut pending, &conn_state).finish();
53+
let _ =
54+
SendStream::new_for_fuzzing(id, &mut state, &mut pending, &conn_state).finish();
5455
}
5556
Operation::ReceivedStopSending(sid, err_code) => {
5657
Streams::new(&mut state, &conn_state)
@@ -63,8 +64,8 @@ fuzz_target!(|input: (StreamParams, Vec<Operation>)| {
6364
.received_reset(rs);
6465
}
6566
Operation::Reset(id) => {
66-
let _ =
67-
SendStream::new(id, &mut state, &mut pending, &conn_state).reset(0u32.into());
67+
let _ = SendStream::new_for_fuzzing(id, &mut state, &mut pending, &conn_state)
68+
.reset(0u32.into());
6869
}
6970
}
7071
}

quinn-proto/src/connection/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,12 @@ impl Connection {
426426
#[must_use]
427427
pub fn send_stream(&mut self, id: StreamId) -> SendStream<'_> {
428428
assert!(id.dir() == Dir::Bi || id.initiator() == self.side.side());
429-
SendStream {
429+
SendStream::new(
430430
id,
431-
state: &mut self.streams,
432-
pending: &mut self.spaces[SpaceId::Data].pending,
433-
conn_state: &self.state,
434-
}
431+
&mut self.streams,
432+
&mut self.spaces[SpaceId::Data].pending,
433+
&self.state,
434+
)
435435
}
436436

437437
/// Returns packets to transmit

quinn-proto/src/connection/streams/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,16 @@ pub struct SendStream<'a> {
203203
#[allow(clippy::needless_lifetimes)] // Needed for cfg(fuzzing)
204204
impl<'a> SendStream<'a> {
205205
#[cfg(fuzzing)]
206-
pub fn new(
206+
pub fn new_for_fuzzing(
207+
id: StreamId,
208+
state: &'a mut StreamsState,
209+
pending: &'a mut Retransmits,
210+
conn_state: &'a super::State,
211+
) -> Self {
212+
Self::new(id, state, pending, conn_state)
213+
}
214+
215+
pub(super) fn new(
207216
id: StreamId,
208217
state: &'a mut StreamsState,
209218
pending: &'a mut Retransmits,

quinn-proto/src/connection/streams/state.rs

Lines changed: 14 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,12 +1290,7 @@ mod tests {
12901290
.open(Dir::Uni)
12911291
.unwrap();
12921292

1293-
let mut stream = SendStream {
1294-
id,
1295-
state: &mut server,
1296-
pending: &mut pending,
1297-
conn_state: &state,
1298-
};
1293+
let mut stream = SendStream::new(id, &mut server, &mut pending, &state);
12991294

13001295
let error_code = 0u32.into();
13011296
stream.state.received_stop_sending(id, error_code);
@@ -1353,29 +1348,14 @@ mod tests {
13531348
let id_mid = streams.open(Dir::Bi).unwrap();
13541349
let id_low = streams.open(Dir::Bi).unwrap();
13551350

1356-
let mut mid = SendStream {
1357-
id: id_mid,
1358-
state: &mut server,
1359-
pending: &mut pending,
1360-
conn_state: &state,
1361-
};
1351+
let mut mid = SendStream::new(id_mid, &mut server, &mut pending, &state);
13621352
mid.write(b"mid").unwrap();
13631353

1364-
let mut low = SendStream {
1365-
id: id_low,
1366-
state: &mut server,
1367-
pending: &mut pending,
1368-
conn_state: &state,
1369-
};
1354+
let mut low = SendStream::new(id_low, &mut server, &mut pending, &state);
13701355
low.set_priority(-1).unwrap();
13711356
low.write(b"low").unwrap();
13721357

1373-
let mut high = SendStream {
1374-
id: id_high,
1375-
state: &mut server,
1376-
pending: &mut pending,
1377-
conn_state: &state,
1378-
};
1358+
let mut high = SendStream::new(id_high, &mut server, &mut pending, &state);
13791359
high.set_priority(1).unwrap();
13801360
high.write(b"high").unwrap();
13811361

@@ -1408,34 +1388,19 @@ mod tests {
14081388
let id_high = streams.open(Dir::Bi).unwrap();
14091389
let id_mid = streams.open(Dir::Bi).unwrap();
14101390

1411-
let mut mid = SendStream {
1412-
id: id_mid,
1413-
state: &mut server,
1414-
pending: &mut pending,
1415-
conn_state: &state,
1416-
};
1391+
let mut mid = SendStream::new(id_mid, &mut server, &mut pending, &state);
14171392
assert_eq!(mid.write(b"mid").unwrap(), 3);
14181393
assert_eq!(server.pending.len(), 1);
14191394

1420-
let mut high = SendStream {
1421-
id: id_high,
1422-
state: &mut server,
1423-
pending: &mut pending,
1424-
conn_state: &state,
1425-
};
1395+
let mut high = SendStream::new(id_high, &mut server, &mut pending, &state);
14261396
high.set_priority(1).unwrap();
14271397
assert_eq!(high.write(&[0; 200]).unwrap(), 200);
14281398
assert_eq!(server.pending.len(), 2);
14291399

14301400
// Requeue the high priority stream to lowest priority. The initial send
14311401
// still uses high priority since it's queued that way. After that it will
14321402
// switch to low priority
1433-
let mut high = SendStream {
1434-
id: id_high,
1435-
state: &mut server,
1436-
pending: &mut pending,
1437-
conn_state: &state,
1438-
};
1403+
let mut high = SendStream::new(id_high, &mut server, &mut pending, &state);
14391404
high.set_priority(-1).unwrap();
14401405

14411406
let mut buf = Vec::with_capacity(1000);
@@ -1478,28 +1443,13 @@ mod tests {
14781443
let id_b = streams.open(Dir::Bi).unwrap();
14791444
let id_c = streams.open(Dir::Bi).unwrap();
14801445

1481-
let mut stream_a = SendStream {
1482-
id: id_a,
1483-
state: &mut server,
1484-
pending: &mut pending,
1485-
conn_state: &state,
1486-
};
1446+
let mut stream_a = SendStream::new(id_a, &mut server, &mut pending, &state);
14871447
stream_a.write(&[b'a'; 100]).unwrap();
14881448

1489-
let mut stream_b = SendStream {
1490-
id: id_b,
1491-
state: &mut server,
1492-
pending: &mut pending,
1493-
conn_state: &state,
1494-
};
1449+
let mut stream_b = SendStream::new(id_b, &mut server, &mut pending, &state);
14951450
stream_b.write(&[b'b'; 100]).unwrap();
14961451

1497-
let mut stream_c = SendStream {
1498-
id: id_c,
1499-
state: &mut server,
1500-
pending: &mut pending,
1501-
conn_state: &state,
1502-
};
1452+
let mut stream_c = SendStream::new(id_c, &mut server, &mut pending, &state);
15031453
stream_c.write(&[b'c'; 100]).unwrap();
15041454

15051455
let mut metas = vec![];
@@ -1558,20 +1508,10 @@ mod tests {
15581508
let id_b = streams.open(Dir::Bi).unwrap();
15591509
let id_c = streams.open(Dir::Bi).unwrap();
15601510

1561-
let mut stream_a = SendStream {
1562-
id: id_a,
1563-
state: &mut server,
1564-
pending: &mut pending,
1565-
conn_state: &state,
1566-
};
1511+
let mut stream_a = SendStream::new(id_a, &mut server, &mut pending, &state);
15671512
stream_a.write(&[b'a'; 100]).unwrap();
15681513

1569-
let mut stream_b = SendStream {
1570-
id: id_b,
1571-
state: &mut server,
1572-
pending: &mut pending,
1573-
conn_state: &state,
1574-
};
1514+
let mut stream_b = SendStream::new(id_b, &mut server, &mut pending, &state);
15751515
stream_b.write(&[b'b'; 100]).unwrap();
15761516

15771517
let mut metas = vec![];
@@ -1584,12 +1524,7 @@ mod tests {
15841524
metas.extend(meta);
15851525

15861526
// Queue stream_c which has higher priority
1587-
let mut stream_c = SendStream {
1588-
id: id_c,
1589-
state: &mut server,
1590-
pending: &mut pending,
1591-
conn_state: &state,
1592-
};
1527+
let mut stream_c = SendStream::new(id_c, &mut server, &mut pending, &state);
15931528
stream_c.set_priority(1).unwrap();
15941529
stream_c.write(&[b'b'; 100]).unwrap();
15951530

@@ -1658,12 +1593,7 @@ mod tests {
16581593
};
16591594

16601595
let id = streams.open(Dir::Uni).unwrap();
1661-
let mut stream = SendStream {
1662-
id,
1663-
state: &mut server,
1664-
pending: &mut pending,
1665-
conn_state: &state,
1666-
};
1596+
let mut stream = SendStream::new(id, &mut server, &mut pending, &state);
16671597
stream.write(b"hello").unwrap();
16681598
stream.reset(0u32.into()).unwrap();
16691599

0 commit comments

Comments
 (0)