Open zyc801208 opened 3 years ago
panic at line drain, when character is not a single byte char
pub fn try_read(&mut self) -> Option<char> {
// discard eventual errors, EOF will be handled in read_until correctly
let _ = self.read_into_buffer();
if self.buffer.len() > 0 {
self.buffer.drain(..1).last()
} else {
None
}
}
I add this fn to read all bytes in buffer.
pub fn try_read_all(&mut self) -> Option<String> {
...
return Some(self.buffer.drain(..).collect());
...
}
Try to read a utf-8 character, program panic. How to fix this problem?
panicked at 'byte index 1 is not a char boundary; it is inside 'é' (bytes 0..2) of `éª�æ�¶ ...
stack backtrace: 0: backtrace::backtrace::libunwind::trace at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/libunwind.rs:86 1: backtrace::backtrace::trace_unsynchronized at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/mod.rs:66 2: std::sys_common::backtrace::_print_fmt at src/libstd/sys_common/backtrace.rs:78 3:::fmt
at src/libstd/sys_common/backtrace.rs:59
4: core::fmt::write
at src/libcore/fmt/mod.rs:1063
5: std::io::Write::write_fmt
at /rustc/4fb7144ed159f94491249e86d5bbd033b5d60550/src/libstd/io/mod.rs:1426
6: std::io::impls::<impl std::io::Write for alloc::boxed::Box>::write_fmt
at src/libstd/io/impls.rs:156
7: std::sys_common::backtrace::_print
at src/libstd/sys_common/backtrace.rs:62
8: std::sys_common::backtrace::print
at src/libstd/sys_common/backtrace.rs:49
9: std::panicking::default_hook::{{closure}}
at src/libstd/panicking.rs:204
10: std::panicking::default_hook
at src/libstd/panicking.rs:221
11: std::panicking::rust_panic_with_hook
at src/libstd/panicking.rs:470
12: rust_begin_unwind
at src/libstd/panicking.rs:378
13: core::panicking::panic_fmt
at src/libcore/panicking.rs:85
14: core::str::slice_error_fail
at src/libcore/str/mod.rs:0
15: core::str::traits::<impl core::slice::SliceIndex for core::ops::range::Range>::index::{{closure}}
at /rustc/4fb7144ed159f94491249e86d5bbd033b5d60550/src/libcore/str/mod.rs:1919
16: core::option::Option::unwrap_or_else
at /rustc/4fb7144ed159f94491249e86d5bbd033b5d60550/src/libcore/option.rs:428
17: core::str::traits::<impl core::slice::SliceIndex for core::ops::range::Range>::index
at /rustc/4fb7144ed159f94491249e86d5bbd033b5d60550/src/libcore/str/mod.rs:1919
18: core::str::traits::<impl core::ops::index::Index for str>::index
at /rustc/4fb7144ed159f94491249e86d5bbd033b5d60550/src/libcore/str/mod.rs:1780
19: <alloc::string::String as core::ops::index::Index<core::ops::range::Range>>::index
at /rustc/4fb7144ed159f94491249e86d5bbd033b5d60550/src/liballoc/string.rs:1999
20: alloc::string::String::drain
at /rustc/4fb7144ed159f94491249e86d5bbd033b5d60550/src/liballoc/string.rs:1551
21: rexpect::reader::NBReader::try_read
at /home/zhangyongcheng/.cargo/registry/src/mirrors.tuna.tsinghua.edu.cn-df7c3c540f42cdbd/rexpect-0.4.0/src/reader.rs:265
22: rexpect::session::StreamSession::try_read
at /home/zhangyongcheng/.cargo/registry/src/mirrors.tuna.tsinghua.edu.cn-df7c3c540f42cdbd/rexpect-0.4.0/src/session.rs:98