跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Rust] 请教一个 Rust 问题,提前祝大家中秋节快乐

发表于

最近接触 rust ,心血来潮想写一个 rdb 解析工具,目前 aux 部分解析完成,但是每次解析出来的值都会覆盖掉之前的值,导致最后结果为空,以下是代码,跪求各位大佬解答 orz:

impl ParserFactory {
    pub fn parse(rdb_context: &[u8]) -> Result<RDBInfo> {
        let mut cursor = Cursor::new(rdb_context);
        let base_info = BaseInfo::parse(&mut cursor)?;
        let rdb_version:usize = base_info.rdb_version.parse().expect("Not a valid number");
        let mut aux_info = None;
        let mut db_info=  None;



        // aux 只有 rdb 版本大于等于 7 才引入
        if rdb_version < 7 {
            aux_info = None
        };

        loop {
            // 读取标志位
            let mut flag_byte = [0;1];
            if cursor.read_exact(&mut flag_byte).is_err(){
                break
            }
            match flag_byte[0] {
                FA => {
                    aux_info = Some(AuxInfo::parse(&mut cursor)?);
                }
                FE => {
                    db_info = Some(DbInfo::parse(&mut cursor)?);
                }

                FF => {
                    println!("parse done.");
                    break
                }
                _ =>{
                    continue;
                }
            }
        }

        // 组合 RDBInfo
        let rdb_info = RDBInfo {
            base_info,
            aux_info: aux_info.unwrap_or_else(|| AuxInfo {
                redis_server_version: String::new(),
                used_mem:0,
            }),
        };

        Ok(rdb_info)
    }

}
impl Parser for AuxInfo {
    fn parse(cursor: &mut Cursor<&[u8]>) -> Result<AuxInfo> {
        let mut aux_info = AuxInfo {
            redis_server_version: String::new(),
            used_mem: 0,
        };
        let aux_name = parser_aux_name(cursor)?;

        match aux_name.as_str() {
            "redis-ver" => {
                aux_info.redis_server_version = parser_aux_value(cursor)?;
            }
            "used-mem" => {
                aux_info.used_mem = parser_aux_value(cursor)?.parse::<usize>().expect("Failed to parse used-mem");
            }

            _ => {

            }
        }
        Ok(aux_info)
    }
}

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.