小编S.R*_*S.R的帖子

Rust正确的错误处理(从一种错误类型自动转换为带问号的另一种错误类型)

我想学习如何正确处理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)

rust

15
推荐指数
3
解决办法
4814
查看次数

将自己的功能添加到ifstream

我想让我自己的类在任何情况下都与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)
  1. 很高兴看到正确的例子(如果我可以继承ifstream).
  2. 原因是计算一次并多次读数.
  3. 为什么不get_file_size(ifstream &ifs)呢?我的ifstream obj是静态的,因此它的计算次数很多.

c++ fstream std c++11

5
推荐指数
1
解决办法
229
查看次数

gitlab-runner exec with dind (docker in docker) 最小工作示例

这是我的.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)

docker gitlab-ci gitlab-ci-runner docker-in-docker

5
推荐指数
0
解决办法
684
查看次数

如何将 ctypes 转换为字节

我想使用我定义的 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。从归档开始versioncompilation_time我有一个处理字节的函数。

所以对我来说,只需将 ctypes.Structure 转换为字节(b'')或直接访问内存并更改最后两个字节就可以了。

我试过使用struct但我没有找到pragma选项。

python ctypes python-3.x

4
推荐指数
1
解决办法
4324
查看次数

使用 dash(-) 作为参数中键名的一部分(Azure 管道)

我正在创建一个 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 }}'

解决方法是将名称从 更改为 …

continuous-integration azure azure-devops azure-pipelines

3
推荐指数
1
解决办法
1626
查看次数

PyQt用dict发出信号

我想发出定义的信号:

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)

python qt pyqt python-3.x pyqt5

2
推荐指数
1
解决办法
3522
查看次数

无法编译x86的代码,但可以为arm

我有一个奇怪的问题.我可以编译我的代码,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)

c++ gcc compiler-errors g++ cross-compiling

0
推荐指数
1
解决办法
102
查看次数

使用std :: get_time转换ANSI C的asctime()格式

我想将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)

附加信息

  1. 我无法使用strptime因为musl libc中存在错误.
  2. 我试过设置一些时区但是: …

c++ datetime c++11

0
推荐指数
1
解决办法
274
查看次数