From 4ac811c38f00fa2188844aceaaba3d54a86a4ea2 Mon Sep 17 00:00:00 2001 From: hpp2334 Date: Thu, 28 Nov 2024 01:14:29 +0800 Subject: [PATCH] refactor: use bitcode --- rust-libs/Cargo.lock | 69 ++++++++++++------- rust-libs/ease-client-backend/Cargo.toml | 2 +- .../src/services/server/mod.rs | 12 ++-- rust-libs/ease-client-shared/Cargo.toml | 3 +- .../ease-client-shared/src/backends/lyric.rs | 6 +- .../src/backends/message.rs | 12 ++-- .../ease-client-shared/src/backends/music.rs | 24 +++++-- .../src/backends/music_duration.rs | 2 +- .../ease-client-shared/src/backends/player.rs | 10 +-- .../src/backends/playlist.rs | 14 ++-- .../src/backends/storage.rs | 61 ++++++++++++++-- .../ease-client-shared/src/utils/define_id.rs | 14 +++- 12 files changed, 160 insertions(+), 69 deletions(-) diff --git a/rust-libs/Cargo.lock b/rust-libs/Cargo.lock index ea97aac8..b0ec5366 100644 --- a/rust-libs/Cargo.lock +++ b/rust-libs/Cargo.lock @@ -105,6 +105,12 @@ version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e1496f8fb1fbf272686b8d37f523dab3e4a7443300055e74cdaa449f3114356" +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + [[package]] name = "askama" version = "0.12.1" @@ -236,6 +242,30 @@ dependencies = [ "serde", ] +[[package]] +name = "bitcode" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee1bce7608560cd4bf0296a4262d0dbf13e6bcec5ff2105724c8ab88cc7fc784" +dependencies = [ + "arrayvec", + "bitcode_derive", + "bytemuck", + "glam", + "serde", +] + +[[package]] +name = "bitcode_derive" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a539389a13af092cd345a2b47ae7dec12deb306d660b2223d25cd3419b253ebe" +dependencies = [ + "proc-macro2 1.0.86", + "quote 1.0.37", + "syn 2.0.77", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -263,6 +293,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "bytemuck" +version = "1.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b37c88a63ffd85d15b406896cc343916d7cf57838a847b3a6f2ca5d39a5695a" + [[package]] name = "byteorder" version = "1.5.0" @@ -516,6 +552,7 @@ name = "ease-client-backend" version = "0.2.0" dependencies = [ "base64 0.22.1", + "bitcode", "bytes", "ease-client-shared", "ease-database", @@ -529,7 +566,6 @@ dependencies = [ "num-traits", "once_cell", "redb", - "rmp-serde", "serde", "serde_bytes", "serde_json", @@ -544,12 +580,11 @@ dependencies = [ name = "ease-client-shared" version = "0.1.0" dependencies = [ + "bitcode", "ease-database", "num-derive", "num-traits", - "obfstr", "once_cell", - "rmp-serde", "serde", "serde_bytes", "serde_json", @@ -821,6 +856,12 @@ version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64" +[[package]] +name = "glam" +version = "0.29.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc46dd3ec48fdd8e693a98d2b8bafae273a2d54c1de02a2a7e3d57d501f39677" + [[package]] name = "glob" version = "0.3.1" @@ -1647,28 +1688,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "rmp" -version = "0.8.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "228ed7c16fa39782c3b3468e974aec2795e9089153cd08ee2e9aefb3613334c4" -dependencies = [ - "byteorder", - "num-traits", - "paste", -] - -[[package]] -name = "rmp-serde" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52e599a477cf9840e92f2cde9a7189e67b42c57532749bf90aea6ec10facd4db" -dependencies = [ - "byteorder", - "rmp", - "serde", -] - [[package]] name = "rusqlite" version = "0.30.0" diff --git a/rust-libs/ease-client-backend/Cargo.toml b/rust-libs/ease-client-backend/Cargo.toml index 7cac4926..a7ef2092 100644 --- a/rust-libs/ease-client-backend/Cargo.toml +++ b/rust-libs/ease-client-backend/Cargo.toml @@ -30,4 +30,4 @@ base64 = "0.22.1" uniffi = "=0.28.3" redb = "2.2.0" lru = "0.12.5" -rmp-serde = "1.3.0" +bitcode = "0.6.3" diff --git a/rust-libs/ease-client-backend/src/services/server/mod.rs b/rust-libs/ease-client-backend/src/services/server/mod.rs index 655ee807..66f262e5 100644 --- a/rust-libs/ease-client-backend/src/services/server/mod.rs +++ b/rust-libs/ease-client-backend/src/services/server/mod.rs @@ -11,14 +11,13 @@ use ease_remote_storage::StreamFile; use futures::StreamExt; use lru::LruCache; use misty_async::Task; -use serde::{Deserialize, Serialize}; use serve::{ get_stream_file_by_loc, get_stream_file_by_music_id, get_stream_file_cover_by_music_id, }; use crate::{ctx::BackendContext, error::BResult}; -#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, uniffi::Enum)] +#[derive(Debug, Default, bitcode::Encode, bitcode::Decode, PartialEq, Eq, uniffi::Enum)] pub enum AssetLoadStatus { #[default] Pending, @@ -50,14 +49,13 @@ struct AssetChunks { source: AssetChunksSource, } -#[derive(Debug, Serialize, Deserialize, uniffi::Enum)] +#[derive(Debug, bitcode::Encode, bitcode::Decode, uniffi::Enum)] pub enum AssetChunkData { Status(AssetLoadStatus), - #[serde(with = "serde_bytes")] Buffer(Vec), } -#[derive(Debug, Serialize, Deserialize, uniffi::Enum)] +#[derive(Debug, bitcode::Encode, bitcode::Decode, uniffi::Enum)] pub enum AssetChunkRead { NotOpen, None, @@ -103,7 +101,7 @@ impl AssetChunksSource { let data = table.get(index)?; if let Some(data) = data { - let data = rmp_serde::from_read(std::io::Cursor::new(data.value())).unwrap(); + let data = bitcode::decode(&data.value()).unwrap(); Ok(Some(data)) } else { Ok(None) @@ -115,7 +113,7 @@ impl AssetChunksSource { let table_definition = Self::def(&self.key); { let mut table = w_txn.open_table(table_definition)?; - let data = rmp_serde::to_vec(&chunk).unwrap(); + let data = bitcode::encode(&chunk); table.insert(index, data)?; } w_txn.commit()?; diff --git a/rust-libs/ease-client-shared/Cargo.toml b/rust-libs/ease-client-shared/Cargo.toml index 5c59c8e7..fb1d5a52 100644 --- a/rust-libs/ease-client-shared/Cargo.toml +++ b/rust-libs/ease-client-shared/Cargo.toml @@ -16,7 +16,6 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" thiserror = "1.0.57" serde_bytes = "0.11.14" -rmp-serde = "1.3.0" urlencoding = "2.1.3" once_cell = "1.18.0" -obfstr = "0.4.4" +bitcode = "0.6.3" diff --git a/rust-libs/ease-client-shared/src/backends/lyric.rs b/rust-libs/ease-client-shared/src/backends/lyric.rs index 146813f7..06082fab 100644 --- a/rust-libs/ease-client-shared/src/backends/lyric.rs +++ b/rust-libs/ease-client-shared/src/backends/lyric.rs @@ -2,7 +2,9 @@ use std::time::Duration; use serde::{Deserialize, Serialize}; -#[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, Clone)] +#[derive( + Debug, PartialEq, Eq, Default, Serialize, Deserialize, bitcode::Encode, bitcode::Decode, Clone, +)] pub struct LrcMetadata { pub artist: String, pub album: String, @@ -13,7 +15,7 @@ pub struct LrcMetadata { pub offset: String, } -#[derive(Debug, Default, Serialize, Deserialize, Clone)] +#[derive(Debug, Default, Serialize, Deserialize, bitcode::Encode, bitcode::Decode, Clone)] pub struct Lyrics { pub metdata: LrcMetadata, pub lines: Vec<(Duration, String)>, diff --git a/rust-libs/ease-client-shared/src/backends/message.rs b/rust-libs/ease-client-shared/src/backends/message.rs index f846d69b..0b6c4242 100644 --- a/rust-libs/ease-client-shared/src/backends/message.rs +++ b/rust-libs/ease-client-shared/src/backends/message.rs @@ -6,8 +6,8 @@ use super::generated::Code; pub trait IMessage { const CODE: Code; - type Argument: Debug + Serialize + DeserializeOwned + Send + Sync + 'static; - type Return: Debug + Serialize + DeserializeOwned + Send + Sync + 'static; + type Argument: Debug + bitcode::Encode + bitcode::DecodeOwned + Send + Sync + 'static; + type Return: Debug + bitcode::Encode + bitcode::DecodeOwned + Send + Sync + 'static; } #[derive(Deserialize, Serialize)] @@ -30,16 +30,16 @@ macro_rules! define_message { pub fn decode_message_payload(arg: Vec) -> T where - T: Serialize + DeserializeOwned, + T: bitcode::Encode + bitcode::DecodeOwned, { - let ret = rmp_serde::from_slice(arg.as_slice()).unwrap(); + let ret = bitcode::decode(arg.as_slice()).unwrap(); ret } pub fn encode_message_payload(arg: T) -> Vec where - T: Serialize + DeserializeOwned, + T: bitcode::Encode + bitcode::DecodeOwned, { - let ret = rmp_serde::to_vec(&arg).unwrap(); + let ret = bitcode::encode(&arg); ret } diff --git a/rust-libs/ease-client-shared/src/backends/music.rs b/rust-libs/ease-client-shared/src/backends/music.rs index 34dc391f..3b8ea1ac 100644 --- a/rust-libs/ease-client-shared/src/backends/music.rs +++ b/rust-libs/ease-client-shared/src/backends/music.rs @@ -12,20 +12,32 @@ use super::{ define_id!(MusicId); -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode, Clone)] pub struct MusicMeta { pub id: MusicId, pub title: String, pub duration: Option, } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, bitcode::Encode, bitcode::Decode)] pub struct MusicAbstract { pub meta: MusicMeta, pub cover: Option, } -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, uniffi::Enum)] +#[derive( + Debug, + Default, + Clone, + Copy, + PartialEq, + Eq, + Serialize, + Deserialize, + bitcode::Encode, + bitcode::Decode, + uniffi::Enum, +)] pub enum LyricLoadState { Loading, #[default] @@ -34,14 +46,14 @@ pub enum LyricLoadState { Loaded, } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode, Clone)] pub struct MusicLyric { pub loc: StorageEntryLoc, pub data: Lyrics, pub loaded_state: LyricLoadState, } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode, Clone)] pub struct Music { pub meta: MusicMeta, pub loc: StorageEntryLoc, @@ -79,7 +91,7 @@ impl MusicAbstract { } } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode)] pub struct ArgUpdateMusicLyric { pub id: MusicId, pub lyric_loc: Option, diff --git a/rust-libs/ease-client-shared/src/backends/music_duration.rs b/rust-libs/ease-client-shared/src/backends/music_duration.rs index 223d7ff1..50470311 100644 --- a/rust-libs/ease-client-shared/src/backends/music_duration.rs +++ b/rust-libs/ease-client-shared/src/backends/music_duration.rs @@ -1,6 +1,6 @@ use std::{ops::Deref, time::Duration}; -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, bitcode::Encode, bitcode::Decode)] pub struct MusicDuration(Duration); impl Deref for MusicDuration { diff --git a/rust-libs/ease-client-shared/src/backends/player.rs b/rust-libs/ease-client-shared/src/backends/player.rs index b876c3cb..132e06e2 100644 --- a/rust-libs/ease-client-shared/src/backends/player.rs +++ b/rust-libs/ease-client-shared/src/backends/player.rs @@ -10,13 +10,13 @@ use super::{ storage::DataSourceKey, }; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode)] pub struct ArgPlayMusic { pub id: MusicId, pub playlist_id: PlaylistId, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, bitcode::Encode, bitcode::Decode)] pub struct PlayerCurrentPlaying { pub abstr: MusicAbstract, pub playlist_id: PlaylistId, @@ -29,7 +29,7 @@ pub struct PlayerCurrentPlaying { pub next_cover: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Enum)] +#[derive(Debug, Clone, Serialize, Deserialize, bitcode::Encode, bitcode::Decode, uniffi::Enum)] pub enum PlayerDelegateEvent { Complete, Loading, @@ -63,6 +63,8 @@ pub enum PlayerDelegateEvent { Deserialize, FromPrimitive, ToPrimitive, + bitcode::Encode, + bitcode::Decode, uniffi::Enum, )] pub enum PlayMode { @@ -84,7 +86,7 @@ pub enum ConnectorPlayerAction { Error { value: String }, } -#[derive(Debug, Serialize, Deserialize, uniffi::Record)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode, uniffi::Record)] pub struct PlayerDurations { pub current: Duration, pub buffer: Duration, diff --git a/rust-libs/ease-client-shared/src/backends/playlist.rs b/rust-libs/ease-client-shared/src/backends/playlist.rs index db9e81f7..dcc4d04a 100644 --- a/rust-libs/ease-client-shared/src/backends/playlist.rs +++ b/rust-libs/ease-client-shared/src/backends/playlist.rs @@ -12,7 +12,7 @@ use super::{ define_id!(PlaylistId); -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode, Clone)] pub struct PlaylistMeta { pub id: PlaylistId, pub title: String, @@ -21,14 +21,14 @@ pub struct PlaylistMeta { pub created_time: Duration, } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode, Clone)] pub struct PlaylistAbstract { pub meta: PlaylistMeta, pub music_count: usize, pub duration: Option, } -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode, Clone)] pub struct Playlist { pub abstr: PlaylistAbstract, pub musics: Vec, @@ -80,26 +80,26 @@ impl Playlist { } } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode)] pub struct ArgUpdatePlaylist { pub id: PlaylistId, pub title: String, pub cover: Option, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode)] pub struct ArgCreatePlaylist { pub title: String, pub cover: Option, pub entries: Vec<(StorageEntry, String)>, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode)] pub struct ArgAddMusicsToPlaylist { pub id: PlaylistId, pub entries: Vec<(StorageEntry, String)>, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, bitcode::Encode, bitcode::Decode)] pub struct ArgRemoveMusicFromPlaylist { pub playlist_id: PlaylistId, pub music_id: MusicId, diff --git a/rust-libs/ease-client-shared/src/backends/storage.rs b/rust-libs/ease-client-shared/src/backends/storage.rs index 262f97ae..8d6625b2 100644 --- a/rust-libs/ease-client-shared/src/backends/storage.rs +++ b/rust-libs/ease-client-shared/src/backends/storage.rs @@ -7,13 +7,35 @@ use super::{music::MusicId, playlist::PlaylistId}; define_id!(StorageId); -#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq, uniffi::Record)] +#[derive( + Debug, + Serialize, + Deserialize, + Clone, + Hash, + PartialEq, + Eq, + bitcode::Encode, + bitcode::Decode, + uniffi::Record, +)] pub struct StorageEntryLoc { pub path: String, pub storage_id: StorageId, } -#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq, uniffi::Enum)] +#[derive( + Debug, + Clone, + Serialize, + Deserialize, + Hash, + PartialEq, + Eq, + bitcode::Encode, + bitcode::Decode, + uniffi::Enum, +)] pub enum DataSourceKey { Music { id: MusicId }, Cover { id: MusicId }, @@ -32,6 +54,8 @@ pub enum DataSourceKey { Eq, Default, Hash, + bitcode::Encode, + bitcode::Decode, uniffi::Enum, )] pub enum StorageType { @@ -41,7 +65,7 @@ pub enum StorageType { OneDrive, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, bitcode::Encode, bitcode::Decode)] pub struct StorageEntry { pub storage_id: StorageId, pub name: String, @@ -50,7 +74,18 @@ pub struct StorageEntry { pub is_dir: bool, } -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default, uniffi::Record)] +#[derive( + Debug, + Clone, + PartialEq, + Eq, + Serialize, + Deserialize, + Default, + bitcode::Encode, + bitcode::Decode, + uniffi::Record, +)] pub struct ArgUpsertStorage { pub id: Option, pub addr: String, @@ -61,7 +96,19 @@ pub struct ArgUpsertStorage { pub typ: StorageType, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, uniffi::Enum)] +#[derive( + Debug, + Clone, + Copy, + PartialEq, + Eq, + Serialize, + Deserialize, + Default, + bitcode::Encode, + bitcode::Decode, + uniffi::Enum, +)] pub enum StorageConnectionTestResult { #[default] None, @@ -81,7 +128,7 @@ pub enum StorageEntryType { Other, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, bitcode::Encode, bitcode::Decode)] pub struct Storage { pub id: StorageId, pub addr: String, @@ -129,7 +176,7 @@ impl StorageEntry { } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, bitcode::Encode, bitcode::Decode)] pub enum ListStorageEntryChildrenResp { Ok(Vec), AuthenticationFailed, diff --git a/rust-libs/ease-client-shared/src/utils/define_id.rs b/rust-libs/ease-client-shared/src/utils/define_id.rs index f073b6c2..0b4783f7 100644 --- a/rust-libs/ease-client-shared/src/utils/define_id.rs +++ b/rust-libs/ease-client-shared/src/utils/define_id.rs @@ -1,7 +1,19 @@ #[macro_export] macro_rules! define_id { ($s:ident) => { - #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Copy, uniffi::Record)] + #[derive( + Debug, + Clone, + Hash, + PartialEq, + Eq, + PartialOrd, + Ord, + Copy, + bitcode::Encode, + bitcode::Decode, + uniffi::Record, + )] pub struct $s { value: i64, }