我想学习如何正确处理Rust中的错误.我读过这本书和这个例子 ; 现在我想知道我应该如何处理这个函数中的错误:
fn get_synch_point(&self) -> Result<pv::synch::MeasPeriods, reqwest::Error> {
let url = self.root.join("/term/pv/synch"); // self.root is url::Url
let url = match url {
Ok(url) => url,
// ** this err here is url::ParseError and can be converted to Error::Kind https://docs.rs/reqwest/0.8.3/src/reqwest/error.rs.html#54-57 **//
Err(err) => {
return Err(Error {
kind: ::std::convert::From::from(err),
url: url.ok(),
})
}
};
Ok(reqwest::get(url)?.json()?) //this return reqwest::Error or convert to pv::sych::MeasPeriods automaticly
}
Run Code Online (Sandbox Code Playgroud)
这段代码不合适; 它会导致编译错误:
error[E0451]: field `kind` of struct `reqwest::Error` is private
--> src/main.rs:34:42
| …Run Code Online (Sandbox Code Playgroud) 我想让我自己的类在任何情况下都与ifstream完全一样,但我可以很容易地得到一个文件的大小.
这是标题:
#include <fstream>
using namespace std;
class ifile: public ifstream {
size_t _file_size = 0;
size_t calculate_file_size();
public:
ifile(): ifstream(), _file_size(0) {}
ifile(const char *filename, ios_base::open_mode mode = ios_base::in):
ifstream(filename, mode)
{
_file_size = cal_file_size();
}
size_t get_file_size();
virtual ~ifile();
};
Run Code Online (Sandbox Code Playgroud)
我发现了许多我不应该从ifstream继承的信息.那我怎么能轻松解决我的问题呢?
calculate_file_size:
size_t ifile::calculate_file_size()
{
auto present_pos = tellg();
seekg(0, ifstream::end);
auto file_size = tellg();
seekg(present_pos);
return file_size;
}
Run Code Online (Sandbox Code Playgroud)
ifstream). get_file_size(ifstream &ifs)呢?我的ifstream obj是静态的,因此它的计算次数很多.这是我的.gitlab-ci.yml文件:
image: docker:18
varibales:
DOCKER_DRIVER: "overlay2"
DOCKER_HOST: tcp://docker:2375
test:
services:
- "docker:18-dind"
script:
- docker info
Run Code Online (Sandbox Code Playgroud)
我运行它:
gitlab-runner exec docker --docker-privileged --docker-pull-policy if-not-present --docker-tlsverify=false test
Run Code Online (Sandbox Code Playgroud)
它有效。
但我想使用当前的 docker 版本。所以我读了这个文档。
gitlab-runner exec docker --docker-privileged --docker-pull-policy if-not-present --docker-tlsverify=false test
Run Code Online (Sandbox Code Playgroud)
我用与上面相同的方式运行它:
gitlab-runner exec docker --docker-privileged --docker-pull-policy if-not-present --docker-tlsverify=false test
Run Code Online (Sandbox Code Playgroud)
不幸的是,这以错误结束:
错误:无法连接到位于 tcp://docker:2375 的 Docker 守护进程。docker 守护进程是否正在运行?
我已经看到这个问题和许多其他问题。但我找不到一个可以gitlab-runner exec docker在每个 UNIX 系统上使用docker:19的最小工作示例docker:19-dind
编辑:
gitlab-runner --version(来自 Manajaro 仓库):
Version: 13.0.0
Git revision: HEAD
Git …Run Code Online (Sandbox Code Playgroud) 我想使用我定义的 ctype.Structure 作为 Python ( b'') 中的字节访问。
不知道是不是正确的方法?我需要向其他设备发送一个标头(它应该是无架构的)。
所以我将它定义为:
class Header(ctypes.Structure):
_pack_ = 2
_fields_ = [
('version', c_uint8),
('additional_options', c_uint8),
('soft_version', c_uint16),
('compilation_time', c_uint8 * 6),
('crc', c_uint16)
]
Run Code Online (Sandbox Code Playgroud)
现在我需要计算CRC。从归档开始version到compilation_time我有一个处理字节的函数。
所以对我来说,只需将 ctypes.Structure 转换为字节(b'')或直接访问内存并更改最后两个字节就可以了。
我试过使用struct但我没有找到pragma选项。
我正在创建一个 azure 管道作业并希望使用all-features名称作为关键参数。它在 yaml 中有效,但看起来我不能在条件下使用它,或者以某种方式 azure 解析错误。有可能让它工作吗?
parameters:
all-features: false
name: cargo_check
jobs:
- job: ${{ parameters.name }}
pool:
vmImage: ubuntu-16.04
variables:
cliflags: ''
steps:
- template: ../steps/install-rust.yml
parameters:
rustup_toolchain: ${{ parameters.rust }}
- script: echo '##vso[task.setvariable variable=cliflags]$(cliflags) --all-features'
enabled: ${{ parameters.all-features }} ###### at this line I am getting error #######
displayName: "Activate all available features"
Run Code Online (Sandbox Code Playgroud)
错误:
/ci/jobs/cargo-check.yml@templates(第 99 行,第 14 列):意外符号:“所有功能”。位于表达式中的位置 12:parameters.all-features。如需更多帮助,请参阅https://go.microsoft.com/fwlink/?linkid=842996
/ci/jobs/cargo-check.yml@templates(第 99 行,第 14 列):意外值 '${{ parameters.all-features }}'
解决方法是将名称从 更改为 …
我想发出定义的信号:
finished = pyqtSignal(dict)
# other place it's connected to function:
def finised(self, dict_result):
Run Code Online (Sandbox Code Playgroud)
我称它为self.finished.emit({"bk": {}})有效.
现在我打电话给它,self.finished.emit({2: {}})它不起作用!!
回溯(最近调用最后一次):文件"/home/sylwek/workspace/t2-pv/Manager.py",第452行,运行self.finished.emit({2:{}})TypeError:TesterManager.finished [ dict] .emit():参数1有意外类型'dict'
这是正常的吗?我可以改变{2: {}},{'2': {}}但我想了解为什么,并确保没有其他惊喜!
我使用PyQt 5.8.2-2和python 3.6.1-1
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication
class Emiterer(QtCore.QThread):
f = QtCore.pyqtSignal(dict)
def __init__(self):
super(Emiterer, self).__init__()
def run(self):
self.f.emit({"2": {}})
# self.f.emit({2: {}}) < == this don't work!
class Main(QtWidgets.QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.e = Emiterer()
self.e.f.connect(self.finised)
self.e.start()
def finised(self, …Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的问题.我可以编译我的代码,arm但不能x86使用gcc v 6.3(和其他也).这是一个导致问题的示例代码.
#include <stdint.h>
#include <cstdio>
#include <unistd.h>
#include <csignal>
volatile bool $run = true;
static void quitHandler(int __attribute__((unused)) signum)
{
$run = false;
}
void setupQuitHandler()
{
struct sigaction action;
action.sa_handler = quitHandler;
action.sa_flags = SA_RESETHAND;
if (sigemptyset(&action.sa_mask) < 0) printf("[SetupQuit] sigemptyset");
if (sigaction(SIGINT, &action, nullptr) < 0) printf("[SetupQuit] sigaction SIGINT");
if (sigaction(SIGQUIT, &action, nullptr) < 0) printf("[SetupQuit] sigaction SIGQUIT");
if (sigaction(SIGTERM, &action, nullptr) < 0) printf("[SetupQuit] sigaction SIGTERM");
if (sigaction(SIGABRT, &action, nullptr) < …Run Code Online (Sandbox Code Playgroud) 我想将ANSI C'格式的字符串转换为std::tm对象.
例
Sun Nov 6 08:49:37 1994; ANSI C的asctime()格式
这里更多的信息在这里.
#include <string>
#include <ctime>
#include <iomanip>
#include <locale>
bool parseAscTimeDate(std::tm& tm, const std::string& str) {
std::istringstream ss(str);
//ss.imbue(std::locale("en_US.UTF8")); //try timezone?
ss >> std::get_time(&tm, "%a %b %d %H:%M:%S %Y");
return !ss.fail();
}
Run Code Online (Sandbox Code Playgroud)
那为什么会失败?
/* ANSI C's asctime format */
std::tm tm;
if (parseAscTimeDate(tm, "Sun Nov 6 08:49:37 1994"))
std::cout << "Ok!" << std::endl;
else
std::cout << "Wrong!" << std::endl;
Run Code Online (Sandbox Code Playgroud)
strptime因为musl libc中存在错误.