0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

toml-0.5.11 までの toml::from_slice toml::to_vec を toml-0.6 以降(0.7, 0.8 含む) へ更新する実装例

Posted at
// toml-0.5.11 まで

// serialize
let paylaod = toml::to_vec(&data)?; // data: DataStruct -> vec[u8]

// deserialize
let data: DataStruct = toml::from_slice(payload)?; // vec[u8] -> data: DataStruct
// toml-0.6 以降

// serialize
let payload_str = toml::ser::to_string(&data)?; // data: DataStruct -> String
let payload = payload_str.into_bytes(); // String -> vec[u8]

// deserialize
let paylaod_str = std::str::from_utf8(payload).ok()?; // vec[u8] -> &str
let data: DataStruct = toml::de::from_str(payload_str)?; // &str -> data: DataStruct

以上の対応が serde_json, toml, rmp_serde など複数のシリアライザーを関数で共通化して対応している実装や payload をバイト列に抽象化して扱う実装において tomldependencies をアップデートする際に必要となる。

また、そのようなデシリアライズ関数の実装で payload の引数を次のように:

pub fn my_deserialize<T>(payload: &T)
 where T: Deserialize<'a>

定義していた場合は次のように:

pub fn my_deserialize<T>(payload: &T)
 where T: DeserializeOwned

変更する必要も生じる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?