我想让 rustclld用作链接器,而不是ld在特定的板条箱中。所以我.cargo/config在我的项目目录中创建以下内容:
[target.x86_64-unknown-linux-gnu]
linker = "ld.lld"
Run Code Online (Sandbox Code Playgroud)
这会导致链接器错误:
$ cargo build
...
= note: ld.lld: error: unable to find library -ldl
ld.lld: error: unable to find library -lrt
ld.lld: error: unable to find library -lpthread
ld.lld: error: unable to find library -lgcc_s
ld.lld: error: unable to find library -lc
ld.lld: error: unable to find library -lm
ld.lld: error: unable to find library -lrt
ld.lld: error: unable to find library -lpthread
ld.lld: error: unable to find …Run Code Online (Sandbox Code Playgroud) 我很难优化我的SQLAlchemy查询.我的SQL知识非常基础,我无法从SQLAlchemy文档中获取所需的东西.
假设以下非常基本的一对多关系:
class Parent(Base):
__tablename__ = "parents"
id = Column(Integer, primary_key = True)
children = relationship("Child", backref = "parent")
class Child(Base):
__tablename__ = "children"
id = Column(Integer, primary_key = True)
parent_id = Column(Integer, ForeignKey("parents.id"))
naughty = Column(Boolean)
Run Code Online (Sandbox Code Playgroud)
我怎么能:
(Parent, count_of_naughty_children, count_of_all_children)每个父母的元组?花了大量时间谷歌搜索后,我发现如何分别查询这些值:
# The following returns tuples of (Parent, count_of_all_children):
session.query(Parent, func.count(Child.id)).outerjoin(Child, Parent.children).\
group_by(Parent.id)
# The following returns tuples of (Parent, count_of_naughty_children):
al = aliased(Children, session.query(Children).filter_by(naughty = True).\
subquery())
session.query(Parent, func.count(al.id)).outerjoin(al, Parent.children).\
group_by(Parent.id)
Run Code Online (Sandbox Code Playgroud)
我尝试以不同的方式将它们组合在一起,但却无法得到我想要的东西.
我猜这个查询将基于前一个查询,按淘气/全部比率过滤.
任何帮助表示赞赏.
编辑: …
为x86_64-unknown-linux-musl目标编译时,此代码将产生一个.got部分:
fn main() {
println!("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)
fn main() {
println!("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)
根据针对类似C代码的答案,该.got部分是可以安全删除的工件。但是,它对我来说是段错误:
$ cargo build --release --target x86_64-unknown-linux-musl
$ readelf -S hello
There are 30 section headers, starting at offset 0x26dc08:
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
...
[12] .got PROGBITS 0000000000637b58 00037b58
00000000000004a8 0000000000000008 WA 0 0 8
...
Run Code Online (Sandbox Code Playgroud)
看一下反汇编,我发现GOT基本上拥有静态函数地址:
$ objcopy -R.got hello hello_no_got
$ ./hello_no_got
[1] 3131 segmentation fault (core …Run Code Online (Sandbox Code Playgroud) 我有两个类型的变量&T,x并且y我在函数内部交换:
pub fn foo<T: Copy>(mut x: &T) {
let y_owned = *x;
let mut y = &y_owned;
for _ in 0..10 {
do_work(x, y);
std::mem::swap(&mut x, &mut y);
}
}
fn do_work<T>(_x: &T, _y: &T) {}
Run Code Online (Sandbox Code Playgroud)
此代码无法编译,给出以下错误:
error[E0597]: `y_owned` does not live long enough
--> src/lib.rs:3:22
|
3 | let mut y = &y_owned;
| ^^^^^^^ borrowed value does not live long enough
...
8 | }
| - borrowed value only lives until …Run Code Online (Sandbox Code Playgroud) 我正在尝试在夜间Rust 1.38中使用futures-preview = "0.3.0-alpha.16"和进行不稳定的async-await语法runtime = "0.3.0-alpha.6"。感觉真的很酷,但是文档还很稀缺,我被卡住了。
为了超越基本示例,我想创建一个应用程序:
现有的文档和示例使我到目前为止:
#![feature(async_await)]
#![feature(async_closure)]
use futures::{
prelude::*,
select,
future::select_all,
io::{ReadHalf, WriteHalf, Read},
};
use runtime::net::{TcpListener, TcpStream};
use std::io;
async fn read_stream(mut reader: ReadHalf<TcpStream>) -> (ReadHalf<TcpStream>, io::Result<Box<[u8]>>) {
let mut buffer: Vec<u8> = vec![0; 1024];
match reader.read(&mut buffer).await {
Ok(len) => {
buffer.truncate(len);
(reader, Ok(buffer.into_boxed_slice()))
},
Err(err) => (reader, Err(err)),
}
}
#[runtime::main]
async fn main() -> std::io::Result<()> {
let mut listener = TcpListener::bind("127.0.0.1:8080")?;
println!("Listening on {}", …Run Code Online (Sandbox Code Playgroud) 有一个远程 64 位 *nix 服务器可以编译用户提供的代码(应该用 Rust 编写,但我认为它不重要,因为它使用 LLVM)。我不知道它使用哪个编译器/链接器标志,但编译后的 ELF 可执行文件看起来很奇怪 - 它有 4 个 LOAD 段:
$ readelf -e executable
...
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
...
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000004138 0x0000000000004138 R 0x1000
LOAD 0x0000000000005000 0x0000000000005000 0x0000000000005000
0x00000000000305e9 0x00000000000305e9 R E 0x1000
LOAD 0x0000000000036000 0x0000000000036000 0x0000000000036000
0x000000000000d808 0x000000000000d808 R 0x1000
LOAD 0x0000000000043da0 0x0000000000044da0 0x0000000000044da0
0x0000000000002290 0x00000000000024a0 RW 0x1000
...
Run Code Online (Sandbox Code Playgroud)
在我自己的系统上,我查看的所有可执行文件只有 2 个 LOAD 段:
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags …Run Code Online (Sandbox Code Playgroud) 我正在研究 Rust 书,即minigrep 项目。在那里我遇到了以下片段:
fn main() {
let args: Vec<String> = env::args().collect();
let (query, filename) = parse_config(&args);
// --snip--
}
fn parse_config(args: &[String]) -> (&str, &str) {
let query = &args[1];
let filename = &args[2];
(query, filename)
}
Run Code Online (Sandbox Code Playgroud)
对我来说最令人困惑的是args: &[String]。如果我将其替换为args: &Vec<String>,它也可以工作。我的猜测是,这&[String]是一个更通用的类型注释,不仅匹配&Vec<String>,还匹配其他一些类型。那是对的吗?如果是这样, 还可以匹配哪些其他类型[T]?
我正在围绕 ArUco 增强现实库(基于 OpenCV)编写一个薄包装。我正在尝试构建的界面非常简单:
但是,我无法弄清楚如何在 Python 中表示图像以将其传递给 C++。对于 GUI 和相机管理,我将使用 PyQt,所以最初它将是 QImage,但我不能简单地将它传递给 OpenCV(或者我可以?)。一开始,我尝试使用嵌套元组来表示每个像素的行、列和颜色,所以我最终得到了这个示例代码:
using namespace cv;
namespace py = boost::python;
void display(py::tuple pix)
{
/*
Receive image from Python and display it.
*/
Mat img(py::len(pix), py::len(pix[0]), CV_8UC3, Scalar(0, 0, 255));
for (int y = 0; y < py::len(pix); y++)
for (int x = 0; x < py::len(pix[y]); x++)
{
Vec3b rgb;
for (int i = 0; i < 3; i++)
rgb[i] = py::extract<int>(pix[y][x][i]); …Run Code Online (Sandbox Code Playgroud) 我正在编写一个处理 TCP 套接字的低级网络应用程序,我经常需要在其中处理二进制数据流。当一些数据可用时,我将其读入u8数组,然后包装std::io::Cursor<&[u8]>并传递给处理程序。在处理程序中,我经常需要知道是否还有更多数据Cursor。
想象一下该handle函数接收数据,然后使用该函数分块处理它handle_chunk。为简单起见,假设块大小固定为 10 字节;如果数据大小不能被 10 整除,则会出现错误。这个简单的逻辑可以通过以下方式实现:
fn handle(mut data: Cursor<&[u8]>) {
while !data.empty() {
if let Err(err) = handle_chunk(&mut data) {
eprintln!("Error while handling data: {}", err);
}
}
}
fn handle_chunk(data: &mut Cursor<&[u8]>) -> Result<(), String> {
// Returns Err("unexpected EOF".to_string()) if chunk is incomplete
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是,Cursor没有一种empty()方法或任何其他方法能够判断是否有更多数据需要处理。我能想出的可行解决方案是:
fn handle(data: Cursor<&[u8]>) {
let data = data.into_inner();
let len = data.len();
let mut …Run Code Online (Sandbox Code Playgroud) 我的网络应用程序的体系结构可以简化为以下内容:
use std::collections::HashMap;
/// Represents remote user. Usually has fields,
/// but we omit them for the sake of example.
struct User;
impl User {
/// Send data to remote user.
fn send(&mut self, data: &str) {
println!("Sending data to user: \"{}\"", data);
}
}
/// A service that handles user data.
/// Usually has non-trivial internal state, but we omit it here.
struct UserHandler {
users: HashMap<i32, User>, // Maps user id to User objects.
counter: i32 // Represents …Run Code Online (Sandbox Code Playgroud) 我一直在思考为什么 Rust 中的内部可变性在大多数情况下需要运行时检查(例如RefCell)。看起来我找到了一个没有运行时成本的安全替代方案。我已经调用了该类型SafeCell(主要是因为它是一个安全的包装器UnsafeCell),它允许您将任何函数应用于包装的值,而没有引用转义的风险:
struct SafeCell<T> {
inner: UnsafeCell<T>,
}
impl<T> SafeCell<T> {
pub fn new(value: T) -> Self {
Self {
inner: UnsafeCell::new(value),
}
}
pub fn apply<R, F>(&self, fun: F) -> R
where
F: FnOnce(&mut T) -> R,
{
// Reference below has a lifetime of the current scope, so if
// user tries to save it somewhere, borrow checker will catch this.
let reference: &mut T = unsafe { &mut *self.inner.get() }; …Run Code Online (Sandbox Code Playgroud) 我正在开发一个即将完成的小型Pyramid应用程序,因此现在我开始考虑如何部署它。
大多数Pyramid部署教程建议使用nginx作为pserve的反向代理。由于Pyramid应用程序只是wsgi应用程序,因此我也可以将其部署为nginx + wsgi,而无需使用反向代理。我想后者会有些困难,因为我对所有这些东西都是全新的,但是从直观上讲它应该能更快地工作。
所以问题是:nginx + pserve和nginx + wsgi部署方案之间是否有相当大的性能差异?