小编rea*_*eal的帖子

如何使用TryFrom将usize转换为u32?

我想将一个usize类型变量转换为u32Rust中的类型变量.我知道usize变量可能包含一个大于2 ^ 32的值,在这种情况下转换应该失败.我正在尝试使用TryFrom特征来执行转换.

这是一个简单的例子(Nightly Rust,Playground):

#![feature(try_from)]
use std::convert::TryFrom;

fn main() {
    let a: usize = 0x100;
    let res = u32::try_from(a);
    println!("res = {:?}", res);
}
Run Code Online (Sandbox Code Playgroud)

代码无法编译,出现以下编译错误:

error[E0277]: the trait bound `u32: std::convert::From<usize>` is not satisfied
 --> src/main.rs:6:15
  |
6 |     let res = u32::try_from(a);
  |               ^^^^^^^^^^^^^ the trait `std::convert::From<usize>` is not implemented for `u32`
  |
  = help: the following implementations were found:
            <u32 as std::convert::From<std::net::Ipv4Addr>>
            <u32 as std::convert::From<u8>>
            <u32 as …
Run Code Online (Sandbox Code Playgroud)

rust

6
推荐指数
1
解决办法
2016
查看次数

使用 Protobuf 序列化消息的一部分

我正在尝试使用 Protobuf 序列化/反序列化子消息。这样做的原因是签署。我希望能够签署我的部分信息。为了能够做到这一点,我需要以某种方式规范化它。

如果重要的话,我将 protbuf 3.0.0-alpha(使用 proto2 语言)与 Python3.4 一起使用。

示例文件:testp.proto

package my_package;

message my_mess {
  message data {
    optional uint64 x = 1;
    optional uint64 y = 2;
    optional uint64 z = 3;
  }
    optional bytes signature = 4;
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我想对消息的数据部分进行签名。因此我只想序列化my_mess.data,签名,将签名放入my_mess.signature,然后序列化完整的消息my_mess。

编译 testp.proto:

$ protoc -I=. --python_out=. testp.proto 
[libprotobuf WARNING google/protobuf/compiler/parser.cc:471] No syntax specified for the proto file. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
Run Code Online (Sandbox Code Playgroud)

我注意到 mm.data 有方法 …

python serialization digital-signature protocol-buffers

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

py.test混合装置和异步协程

我正在使用py.test为python3代码构建一些测试。该代码使用aiopg(到Postgres的基于Asyncio的接口)访问Postgresql数据库。

我的主要期望:

  • 每个测试用例都应该可以访问新的asyncio事件循环。

  • 运行时间过长的测试将因超时异常而停止。

  • 每个测试用例都应该有权访问数据库连接。

  • 在编写测试用例时,我不想重复自己。

使用py.test固定装置,我可以很接近我想要的东西,但是在每个异步测试用例中,我仍然不得不重复自己一遍。

这是我的代码的样子:

@pytest.fixture(scope='function')
def tloop(request):
    # This fixture is responsible for getting a new event loop
    # for every test, and close it when the test ends.
    ...

def run_timeout(cor,loop,timeout=ASYNC_TEST_TIMEOUT):
    """
    Run a given coroutine with timeout.
    """
    task_with_timeout = asyncio.wait_for(cor,timeout)
    try:
        loop.run_until_complete(task_with_timeout)
    except futures.TimeoutError:
        # Timeout:
        raise ExceptAsyncTestTimeout()


@pytest.fixture(scope='module')
def clean_test_db(request):
    # Empty the test database.
    ...

@pytest.fixture(scope='function')
def udb(request,clean_test_db,tloop):
    # Obtain a connection to the database using aiopg
    # (That's why we …
Run Code Online (Sandbox Code Playgroud)

postgresql fixtures pytest python-decorators python-asyncio

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

推广添加无符号和有符号整数类型

我想有一个允许添加的防锈功能u32(u64,u128)类型的i32(i64,i128)类型,而检查溢出.

我的实施:

/// Add u32 to i32. In case of an overflow, return None.
fn checked_add_i32_u32(a: i32, b: u32) -> Option<i32> {
    let b_half = (b / 2) as i32;
    let b_rem = (b % 2) as i32;

    Some(a.checked_add(b_half)?.checked_add(b_half)?
        .checked_add(b_rem)?)
}

/// Add u64 to i64. In case of an overflow, return None.
fn checked_add_i64_u64(a: i64, b: u64) -> Option<i64> {
    let b_half = (b / 2) as …
Run Code Online (Sandbox Code Playgroud)

integer rust

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

memcmp忽略奇数位置的字符

memcmp从fasm(版本1.71.51)代码调用,我得到了奇怪的结果.

似乎memcmp只比较处于奇数位置的字符.码:

format ELF64

section '.text' executable

    public _start

    extrn memcmp
    extrn exit

_start:
    push    rbp     ; Align the stack to 16 bytes

    mov     rdi, str1
    mov     rsi, str2
    mov     rdx, BUFF_LEN
    call    memcmp

    mov     rdi, rax
    call    exit
    pop     rbp


section '.data' writeable

str1                db '1509487271'
BUFF_LEN = $ - str1
str2                db '1509487273'
Run Code Online (Sandbox Code Playgroud)

我正在运行Ubuntu:

$ uname -a
Linux freedom 4.13.0-43-generic #48~16.04.1-Ubuntu SMP Thu May 17 12:56:46 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

要组装和运行上面的代码,我使用以下命令:

$ …
Run Code Online (Sandbox Code Playgroud)

assembly x86-64 fasm

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