Created
May 15, 2023 17:00
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use reqwest::Client; | |
use serde::{Deserialize, Serialize}; | |
use serde_json; | |
use bytes::Bytes; | |
#[derive(Clone, Debug, Deserialize, Serialize)] | |
struct DropboxDeleteArgs { | |
path: String, | |
} | |
#[tokio::main] | |
async fn main() { | |
let args=DropboxDeleteArgs{ | |
path:"/test.txt".to_string() | |
}; | |
Bytes::from(serde_json::to_string(&args).unwrap().to_string()); | |
Bytes::from_static(serde_json::to_string(&args).unwrap().as_bytes()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我的问题首先来说,我的核心诉求是将 serde_json 序列化后的结果转化成 bytes::Bytes
Bytes::from_static(serde_json::to_string(&args).unwrap().as_bytes())
报错 creates a temporary value which is freed while still in use。我看了下函数签名是bytes: &'static [u8]
,所以我理解是这里将一个所有权转换成 static 这样的东西将会存在计数问题,所以报错,不知道理解正确与否?以及我如果想强行将 as_bytes 这样动态的结果传进去,有什么优雅的方式吗?我查了一下,好像说没有 type safe 的方式,FYI https://users.rust-lang.org/t/convert-string-string-to-static-str/8197/2