~hime/protochat

86db9559d704726e72f5e4e874007921f7683b86 — drbawb 11 months ago f98197b
(crate) use `Into` trait where possible

removes `as` for infallible casts. a little gross in a few places
where it requires additional `let` bindings because inference
falls down.

remaining casts are guarded w/ `assert!` as a means to declare
my intent that these *should not* fail in practice, even though they
might in some super future, where you have a 65636p display.

("This software doesn't work on my 65K display! D:" - what a nice problem
that'd be to solve. lol.)
M linetest/src/shell/net.rs => linetest/src/shell/net.rs +4 -4
@@ 135,7 135,7 @@ async fn write_packet<W>(wtr: &mut W, packet: Packet) -> anyhow::Result<()>
    where W: AsyncWriteExt + Unpin {
        let packet_buf = packet.encode()?;
        let packet_len = packet_buf.len();
        assert!(packet_len <= u16::MAX as usize);
        assert!(packet_len <= u16::MAX.into());

        let mut packet_sz = [0u8; 2];
        (&mut packet_sz[..]).write_u16::<NetworkEndian>(packet_len as u16)?;


@@ 153,7 153,7 @@ where R: AsyncReadExt + Unpin {
    let packet_sz = (&packet_header_buf[..]).read_u16::<NetworkEndian>()?;

    assert!(packet_sz <= u16::MAX);
    let mut packet_body_buf = vec![0u8; packet_sz as usize];
    let mut packet_body_buf = vec![0u8; packet_sz.into()];
    stream.read_exact(&mut packet_body_buf).await?;

    Ok(Packet::decode(&packet_body_buf)?)


@@ 226,9 226,9 @@ async fn net_worker_task(
    };

    let packet_buf = packet.encode()?;
    if packet_buf.len() > u16::MAX as usize {
    if packet_buf.len() > u16::MAX.into() {
        panic!("BUG: user crafted >64KiB packet, this is not legal.");
    }
    } // TODO: use that fancy `anyhow` error macro?

    let mut packet_sz = [0u8; 2];
    (&mut packet_sz[..]).write_u16::<NetworkEndian>(packet_buf.len() as u16)?;

M linetest/src/shell/ui/buffer.rs => linetest/src/shell/ui/buffer.rs +9 -7
@@ 16,7 16,7 @@ struct Span {

impl Span {
    pub fn new(idx: usize, buf: &str, cols: u16) -> Self {
        let wrapped_str = textwrap::fill(buf, cols as usize);
        let wrapped_str = textwrap::fill(buf, cols.into());

        let lines = wrapped_str
            .lines()


@@ 65,9 65,9 @@ impl Buffer {

        // If there are more lines that could be shown we push the current line pointer
        // down in an effort to fill the window w/ the add'l lines.
        let rows = self.rows as usize;
        let delta_buffer = self.lines.len() - self.current_line_ptr;

        let rows: usize = self.rows.into();
        if rows > delta_buffer {
            self.current_line_ptr -= cmp::min(self.current_line_ptr, rows - delta_buffer);
        }


@@ 114,7 114,7 @@ impl Buffer {
        .rev()
        .skip(self.current_line_ptr)
        .map(|(idx, line)| Span::new(idx, line, cols))
        .take(self.rows as usize);
        .take(self.rows.into());

        // blit lines to the viewport
        let mut rows_drawn = 0;


@@ 122,7 122,7 @@ impl Buffer {
        for span in viewport_spans {
            let mut lines = span.line_iter().rev();

            while rows_drawn <= self.rows as usize {
            while rows_drawn <= self.rows.into() {
                match lines.next() {
                    Some(line) => {
                        rows_drawn += 1;


@@ 140,7 140,7 @@ impl Buffer {
        }
        
        // clear remaining lines
        while rows_drawn < self.rows as usize {
        while rows_drawn < self.rows.into() {
            rows_drawn += 1;
            stdout
                .queue(Clear(ClearType::CurrentLine))?


@@ 258,7 258,7 @@ impl InputBuffer {
    pub fn render(&mut self, stdout: &mut io::Stdout, col: u16, row: u16, width: u16) -> anyhow::Result<()> {
        // calculate some info about our prompt & buffer size
        let prompt = "> ";
        let width = width as usize; // TODO: what if usize < u16?
        let width: usize = width.into();
        let width_with_prompt = width - 1 - prompt.len();

        // adjust viewspan to match current terminal width (less the prompt)


@@ 331,6 331,8 @@ impl InputBuffer {
        let beg = self.start;
        let end = cmp::min(self.buffer.len(), self.end);
        let relative_cursor_pos = (self.pos - self.start) + prompt.len() + 1;
        assert!(relative_cursor_pos <= u16::MAX.into());

        stdout
            .queue(Print(&self.buffer[beg..end]))?
            .queue(cursor::MoveToColumn(relative_cursor_pos as u16))?;


@@ 341,4 343,4 @@ impl InputBuffer {
    fn span_len(&self) -> usize {
        self.end - self.start
    }
}
\ No newline at end of file
}

M linetest/src/shell/ui/term.rs => linetest/src/shell/ui/term.rs +1 -1
@@ 327,4 327,4 @@ impl TermInterface {

        Ok(())
    }
}
\ No newline at end of file
}

M smolboi/src/protocol.rs => smolboi/src/protocol.rs +16 -16
@@ 118,7 118,7 @@ pub mod packet {
            let mut buf = vec![];
            buf.write_u8(0x00)?;

            assert!(name.len() <= u16::MAX as usize);
            assert!(name.len() <= u16::MAX.into());
            buf.write_u16::<NetworkEndian>(name.len() as u16)?;
            buf.write(name.as_bytes())?;



@@ 129,7 129,7 @@ pub mod packet {
            let mut buf = vec![];
            buf.write_u8(0x01)?;

            assert!(name.len() <= u16::MAX as usize);
            assert!(name.len() <= u16::MAX.into());
            buf.write_u16::<NetworkEndian>(name.len() as u16)?;
            buf.write(name.as_bytes())?;



@@ 152,7 152,7 @@ pub mod packet {
            let mut buf = vec![];
            buf.write_u8(0x10)?;

            assert!(room.len() <= u16::MAX as usize);
            assert!(room.len() <= u16::MAX.into());
            buf.write_u16::<NetworkEndian>(room.len() as u16)?;
            buf.write(room.as_bytes())?;



@@ 163,7 163,7 @@ pub mod packet {
            let mut buf = vec![];
            buf.write_u8(0x11)?;

            assert!(room.len() <= u16::MAX as usize);
            assert!(room.len() <= u16::MAX.into());
            buf.write_u16::<NetworkEndian>(room.len() as u16)?;
            buf.write(room.as_bytes())?;



@@ 174,15 174,15 @@ pub mod packet {
            let mut buf = vec![];
            buf.write_u8(0x20)?;

            assert!(room.len() <= u16::MAX as usize);
            assert!(room.len() <= u16::MAX.into());
            buf.write_u16::<NetworkEndian>(room.len() as u16)?;
            buf.write(room.as_bytes())?;

            assert!(from.len() <= u16::MAX as usize);
            assert!(from.len() <= u16::MAX.into());
            buf.write_u16::<NetworkEndian>(from.len() as u16)?;
            buf.write(from.as_bytes())?;

            assert!(body.len() <= u16::MAX as usize);
            assert!(body.len() <= u16::MAX.into());
            buf.write_u16::<NetworkEndian>(body.len() as u16)?;
            buf.write(body.as_bytes())?;



@@ 193,7 193,7 @@ pub mod packet {
            let mut buf = vec![];
            buf.write_u8(0x21)?;

            assert!(body.len() <= u16::MAX as usize);
            assert!(body.len() <= u16::MAX.into());
            buf.write_u16::<NetworkEndian>(body.len() as u16)?;
            buf.write(body.as_bytes())?;



@@ 222,7 222,7 @@ pub mod packet {

        fn decode_registration(buf: &mut Cursor<&[u8]>) -> Result<Packet, Error> {
            let name_len = buf.read_u16::<NetworkEndian>()?;
            let mut name_buf = vec![0u8; name_len as usize];
            let mut name_buf = vec![0u8; name_len.into()];
            buf.read_exact(&mut name_buf)?;
            let name_str = String::from_utf8(name_buf)?;
            Ok(Packet::Register { name: name_str })


@@ 230,7 230,7 @@ pub mod packet {

        fn decode_accept_registration(buf: &mut Cursor<&[u8]>) -> Result<Packet, Error> {
            let name_len = buf.read_u16::<NetworkEndian>()?;
            let mut name_buf = vec![0u8; name_len as usize];
            let mut name_buf = vec![0u8; name_len.into()];
            buf.read_exact(&mut name_buf)?;
            let name_str = String::from_utf8(name_buf)?;
            Ok(Packet::RegistrationAccepted { name: name_str })


@@ 248,7 248,7 @@ pub mod packet {

        fn decode_join_room(buf: &mut Cursor<&[u8]>) -> Result<Packet, Error> {
            let len = buf.read_u16::<NetworkEndian>()?;
            let mut room_buf = vec![0u8; len as usize];
            let mut room_buf = vec![0u8; len.into()];
            buf.read_exact(&mut room_buf)?;
            let room_str = String::from_utf8(room_buf)?;



@@ 257,7 257,7 @@ pub mod packet {

        fn decode_part_room(buf: &mut Cursor<&[u8]>) -> Result<Packet, Error> {
            let len = buf.read_u16::<NetworkEndian>()?;
            let mut room_buf = vec![0u8; len as usize];
            let mut room_buf = vec![0u8; len.into()];
            buf.read_exact(&mut room_buf)?;
            let room_str = String::from_utf8(room_buf)?;



@@ 266,17 266,17 @@ pub mod packet {

        fn decode_message_room(buf: &mut Cursor<&[u8]>) -> Result<Packet, Error> {
            let len = buf.read_u16::<NetworkEndian>()?;
            let mut room_buf = vec![0u8; len as usize];
            let mut room_buf = vec![0u8; len.into()];
            buf.read_exact(&mut room_buf)?;
            let room_str = String::from_utf8(room_buf)?;

            let len = buf.read_u16::<NetworkEndian>()?;
            let mut from_buf = vec![0u8; len as usize];
            let mut from_buf = vec![0u8; len.into()];
            buf.read_exact(&mut from_buf)?;
            let from_str = String::from_utf8(from_buf)?;

            let len = buf.read_u16::<NetworkEndian>()?;
            let mut body_buf = vec![0u8; len as usize];
            let mut body_buf = vec![0u8; len.into()];
            buf.read_exact(&mut body_buf)?;
            let body_str = String::from_utf8(body_buf)?;



@@ 286,7 286,7 @@ pub mod packet {

        fn decode_notice(buf: &mut Cursor<&[u8]>) -> Result<Packet, Error> {
            let len = buf.read_u16::<NetworkEndian>()?;
            let mut body_buf = vec![0u8; len as usize];
            let mut body_buf = vec![0u8; len.into()];
            buf.read_exact(&mut body_buf)?;
            let body_str = String::from_utf8(body_buf)?;


M smolboi/src/util.rs => smolboi/src/util.rs +5 -7
@@ 8,7 8,7 @@ pub async fn write_packet<W: AsyncWriteExt + Unpin> (wtr: &mut W, packet: Packet
    let packet_buf = packet.encode()?;
    let mut packet_sz = [0u8; 2];

    assert!(packet_buf.len() < u16::MAX as usize);
    assert!(packet_buf.len() < u16::MAX.into());
    (&mut packet_sz[..]).write_u16::<NetworkEndian>(packet_buf.len() as u16)?;

    wtr.write(&packet_sz).await?;


@@ 21,7 21,7 @@ pub fn write_packet_sync<W: Write> (wtr: &mut W, packet: Packet) -> anyhow::Resu
    let packet_buf = packet.encode()?;
    let mut packet_sz = [0u8; 2];

    assert!(packet_buf.len() < u16::MAX as usize);
    assert!(packet_buf.len() < u16::MAX.into());
    (&mut packet_sz[..]).write_u16::<NetworkEndian>(packet_buf.len() as u16)?;

    wtr.write(&packet_sz)?;


@@ 38,8 38,7 @@ where R: AsyncReadExt + Unpin {
    let mut cursor = std::io::Cursor::new(packet_header_buf);
    let packet_sz = cursor.read_u16::<NetworkEndian>()?;

    assert!(packet_sz <= u16::MAX);
    let mut packet_body_buf = vec![0u8; packet_sz as usize];
    let mut packet_body_buf = vec![0u8; packet_sz.into()];
    rdr.read_exact(&mut packet_body_buf).await?;

    Ok(Packet::decode(&packet_body_buf)?)


@@ 52,9 51,8 @@ pub fn read_packet_sync<R: Read>(rdr: &mut R) -> anyhow::Result<Packet> {
    let mut cursor = std::io::Cursor::new(packet_header_buf);
    let packet_sz = cursor.read_u16::<NetworkEndian>()?;

    assert!(packet_sz <= u16::MAX);
    let mut packet_body_buf = vec![0u8; packet_sz as usize];
    let mut packet_body_buf = vec![0u8; packet_sz.into()];
    rdr.read_exact(&mut packet_body_buf)?;

    Ok(Packet::decode(&packet_body_buf)?)
}
\ No newline at end of file
}