Rust类型系统增强库ic-types的使用,ic-types为Rust项目提供互联网计算机协议的类型定义和工具函数

Rust类型系统增强库ic-types的使用

已弃用

这个库已不再维护。其原始内容已被转移到其他项目中:

  • Principal类型已转移到candid项目
  • HashTree类型已转移到agent-rs项目

关于IC Types

这个库包含处理互联网计算机(Internet Computer)公共规范的类型定义和工具函数,以及HTTP客户端相关的功能。它最初是为了测试和开发目的而单独创建的。

安装

在项目目录中运行以下Cargo命令:

cargo add ic-types

或者在Cargo.toml中添加以下行:

ic-types = "0.7.0"

示例代码

以下是内容中提供的示例代码:

// 注意:ic-types已弃用,此代码仅为演示结构
use ic_types::Principal; // 现在应从candid库导入

fn main() {
    // 创建Principal示例
    let principal = Principal::from_text("rrkah-fqaaa-aaaaa-aaaaq-cai").unwrap();
    
    println!("Principal ID: {}", principal);
    
    // 其他IC相关类型操作...
}

完整示例代码

基于原有示例,扩展一个更完整的demo:

// 注意:ic-types已弃用,实际应该使用candid和agent-rs
// 这里仅为展示ic-types原有功能的示例结构

use ic_types::{Principal, HashTree}; // 现在应从candid和agent-rs分别导入

fn main() {
    // 1. Principal示例
    let principal = Principal::from_text("rrkah-fqaaa-aaaaa-aaaaq-cai").unwrap();
    println!("Principal ID: {}", principal);
    
    // 2. 打印Principal的原始字节
    let principal_bytes = principal.as_slice();
    println!("Principal bytes: {:?}", principal_bytes);
    
    // 3. HashTree示例 (伪代码,实际应使用agent-rs)
    let tree = HashTree::Leaf(vec![1, 2, 3]);
    println!("HashTree: {:?}", tree);
    
    // 4. 检查Principal是否是匿名
    let is_anonymous = principal == Principal::anonymous();
    println!("Is anonymous: {}", is_anonymous);
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_principal() {
        let principal = Principal::from_text("ryjl3-tyaaa-aaaaa-aaaba-cai").unwrap();
        assert_eq!(principal.as_slice().len(), 10);
    }
}

测试

常规测试可以通过cargo test运行。

贡献

请遵循CONTRIBUTING.md文档中的指南进行贡献。

许可证

该项目使用Apache-2.0许可证。


1 回复

Rust类型系统增强库ic-types的使用指南

介绍

ic-types是一个为Rust项目提供互联网计算机(Internet Computer)协议相关类型定义和工具函数的库。它包含了与DFINITY的互联网计算机区块链交互所需的核心类型定义,简化了开发者在Rust中与IC交互的复杂度。

主要功能

  1. 提供互联网计算机协议的核心类型定义
  2. 包含常用的工具函数
  3. 增强Rust类型系统以更好地匹配IC的规范
  4. 简化与IC交互的序列化/反序列化过程

安装

在Cargo.toml中添加依赖:

[dependencies]
ic-types = "0.5.0"  # 请使用最新版本

完整示例代码

use ic_types::{
    Principal, 
    CanisterId,
    Time,
    messages::{Blob, HttpCallContent, HttpRequestEnvelope}
};
use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Serialize, Deserialize};

fn main() {
    // 1. Principal类型示例
    let principal = Principal::from_text("rrkah-fqaaa-aaaaa-aaaaq-cai").unwrap();
    println!("Principal: {}", principal.to_text());
    
    // 2. Canister ID示例
    let canister_id = CanisterId::from_u64(12345);
    println!("Canister ID: {}", canister_id);
    
    // 3. 时间类型示例
    let system_time = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    let ic_time = Time::from_nanos_since_unix_epoch(system_time);
    println!("IC time: {}", ic_time);
    
    // 4. 消息相关类型示例
    let sender = Principal::anonymous();
    let content = HttpCallContent::Call {
        arg: Blob(vec![1, 2, 3, 4]),
        method_name: "greet".to_string(),
        sender,
        canister_id: CanisterId::from_u64(12345),
        ingress_expiry: 0,
        nonce: None,
    };
    
    let envelope = HttpRequestEnvelope::<HttpCallContent> {
        content,
        sender_pubkey: None,
        sender_sig: None,
    };
    
    // 5. 自定义类型序列化示例
    #[derive(Serialize, Deserialize)]
    struct MyCanisterMessage {
        caller: Principal,
        data: Blob,
        timestamp: u64,
    }
    
    let msg = MyCanisterMessage {
        caller: Principal::anonymous(),
        data: Blob(vec![1, 2, 3]),
        timestamp: 123456789,
    };
    
    let json = serde_json::to_string(&msg).unwrap();
    println!("Serialized message: {}", json);
    
    // 6. IC特定编码处理示例
    let encoded = "aaaaa-aa";  // IC管理canister的特殊编码
    let principal = Principal::from_text(encoded).unwrap();
    assert!(principal.is_management_canister());
    println!("Management canister principal: {}", principal);
}

注意事项

  1. 确保使用与你的IC节点兼容的ic-types版本
  2. 处理Principal时要注意错误情况,如无效的文本格式
  3. 时间相关操作要考虑IC的特定时间处理方式
  4. 序列化/反序列化时使用兼容的格式(通常为CBOR)

ic-types库为Rust开发者提供了与互联网计算机交互的类型安全抽象,大大简化了IC应用开发中的底层细节处理。

回到顶部