这是一个智能指针:std::shared_ptr<char> p(new char[size])
表示填充原始二进制文件内容的数组.在整个数组从文件复制到RAM之后(并且仅在之后),我可以解析它,在此期间我检索一些头信息(几个第一个dwords).然后是实际数据.
在没有提供更多上下文的情况下,将所提到的共享指针设置为实际数据开头的新地址非常方便.该地址仍处于已分配的内存中.但如何设置而不失去它?
一个问题是(是/否):是否可以设置p
偏移主流指针,而无需调用数据删除?
当我使用new []关键字(或new-operator)时,它是否连续分配内存?
int* arr = new int[10];
Run Code Online (Sandbox Code Playgroud)
我的意思是,有没有保证,arr [0]和arr [1]紧密放置,我可以使用指针增量迭代arr?如果是这样,这种行为是否保存结构和类而不是int?
tungstenite
我正在使用此示例连接到 websocket 服务器并从那里使用write.send
let connect_addr = env::args()
.nth(1)
.unwrap_or_else(|| panic!("this program requires at least one argument"));
let url = url::Url::parse(&connect_addr).unwrap();
let (stdin_tx, stdin_rx) = futures_channel::mpsc::unbounded();
tokio::spawn(read_stdin(stdin_tx));
let (ws_stream, _) = connect_async(url).await.expect("Failed to connect");
println!("WebSocket handshake has been successfully completed");
let (write, read) = ws_stream.split();
## THIS DOESNT WORK ###
write.send(Message::Text(format!("{}", "HELLO!")))
.await
.expect("Failed to send message");
Run Code Online (Sandbox Code Playgroud)
我无法让写入工作。我得到:
error[E0599]: no method named `send` found for struct `futures_util::stream::stream::split::SplitSink<tokio_tungstenite::WebSocketStream<tokio_tungstenite::stream::Stream<tokio::net::tcp::stream::TcpStream, tokio_native_tls::TlsStream<tokio::net::tcp::stream::TcpStream>>>, tungstenite::protocol::message::Message>` in the current scope
--> src/main.rs:28:15
| …
Run Code Online (Sandbox Code Playgroud) 因此Result<T, E>
有一个非常简洁的方法map_err
,允许以功能性的方式处理错误:
use std::io::Result;
use std::error::Error;
use std::string::ToString;
use std::io;
fn init() -> Result<u32> { Ok(42) }
fn do_work(_data: u32) -> Result<()> { Err(io::Error::new(io::ErrorKind::Other, "IO Error!")) }
fn handle_error<E: Error + ToString>(error: E, message: &str) -> E {
eprintln!("{}: {}", message, error.to_string());
error
}
fn main() {
let _ = init()
.map_err(|e| handle_error(e, "Init error"))
.and_then(do_work)
.map_err(|e| handle_error(e, "Work error")); // "Work error: IO error"
}
Run Code Online (Sandbox Code Playgroud)
如果有相同的功能风格来处理,那就太酷了Option<T>::None
:
use std::io::Result;
use std::error::Error;
use std::string::ToString;
use …
Run Code Online (Sandbox Code Playgroud) 如何在 Golang 中执行批量更新查询?例如,我想在一个查询中执行这些查询。
update product set product_name='AAAA' where product_id='1'
update product set product_name='BBB' where product_id='2'
update product set product_name='CCC' where product_id='3'
update product set product_name='DDDD' where product_id='4'
Run Code Online (Sandbox Code Playgroud) 假设CPU正在运行汇编指令,例如,FOO
将在几个时钟(例如10)中执行
中断请求刚好在执行过程中FOO
,处理器需要中断.它是否等待命令正确执行,或FOO
中止并将重新启动?考虑到不同类型的中断优先级,它的行为是否有所不同?
我想调用模板函数,即使std::enable_if_t
没有解析为true
. 它将允许我在其他上下文中重用我现有的模板之一。
最小的例子:
#include <iostream>
#include <type_traits>
template<typename T,
typename = typename std::enable_if<std::is_same<T, int>::value>::type>
auto foo(T t) {
std::cout << t << std::endl;
return t;
}
template<typename T>
auto bar(T t) {
foo(t);
}
int main() {
int i = 42;
foo(i); // ok already
// foo("42"); // shouldn't
bar("42"); // should
}
Run Code Online (Sandbox Code Playgroud)
I tried to solve it bydeclaring a bool
in template argument list of foo
and specify it when calling foo
in bar
, …
我有一个系统,它将连接多个 API 下游。我想用
let mut system = System::new()
Run Code Online (Sandbox Code Playgroud)
引入配置并进行验证,然后用于
system.init()
初始化下游的所有连接。连接所有下游后,我想通过多种方法对下游进行CRUD。
这里是游乐场
struct Conn {
connection: String,
}
impl Conn {
fn fetch(&mut self) -> &str {
self.connection.push_str("data");
&self.connection
}
}
struct System {
downstream: Option<Conn>,
}
impl System {
fn new() -> Self {
System { downstream: None }
}
fn init(&mut self) {
self.downstream = Some(Conn {
connection: String::from("db connection"),
})
}
fn method_a(self) -> String {
let mut conn = self.downstream.unwrap();
String::from(conn.fetch())
}
fn method_b(self) -> …
Run Code Online (Sandbox Code Playgroud) c++ ×3
rust ×3
assembly ×1
c++17 ×1
database ×1
go ×1
interrupt ×1
new-operator ×1
rust-tokio ×1
shared-ptr ×1
sql ×1
templates ×1
x86 ×1