首页 > 其他分享 >Rust Empty Type

Rust Empty Type

时间:2024-09-24 14:57:31浏览次数:3  
标签:Type void FromBehaviour Empty type event Rust Void


Rust Empty Type

(Jin Qing’s Column, Sep., 2024)

Crate void defines an empty type enum Void {}.

Empty type is an enum with no variants. Empty type can not be instantiated.
See: https://doc.rust-lang.org/nomicon/exotic-sizes.html

Void is used in statically impossible cases (type-level unreachability).
For instance, a return type of Result<T, Void> indicates that it always returns Ok.

Trait void::ResultVoidErrExt can be used to unwrap the result, statically indicating the unreachability.

impl<T> ResultVoidExt<T> for Result<T, Void> {
    /// Get the value out of an always-ok Result.
    ///
    /// Never panics, since it is statically known to be Ok.
    #[inline]
    fn void_unwrap(self) -> T {
        match self {
            Ok(val) => val,
            Err(e) => unreachable(e)
        }
    }
}

rust-libp2p\swarm\src\dummy.rs uses Void to indicate that FromBehaviour event is impossible.

impl crate::handler::ConnectionHandler for ConnectionHandler {
    type FromBehaviour = Void;
    ...

    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
        void::unreachable(event)
    }
...
}


标签:Type,void,FromBehaviour,Empty,type,event,Rust,Void
From: https://blog.51cto.com/u_16162321/12099795

相关文章

  • Rust Pin
    RustPin(JinQing’sColumn,Sep.,2024)From:https://doc.rust-lang.org/std/pin/index.htmlRustcanpindatalocationinmemory,thatmeansitsaddresscannotbechanged.Pinneddataguaranteesthememorylocationisalwaysvalid.Safecodecannotmovepin......
  • 华为 Flexus 云服务器部署 RustDesk Server,打造自己的远程桌面服务器
    ......
  • 使用FILETYPE\SITE和INTITLE指令:/英文双引号/安装字体
    一、·使用filetype指令可以查询特定格式的文件,比如docltxtlppt\pdf.授索格式为:关键词+空格+filetype:+文件格式,比如:初等数论filetype:doc,搜索结果均为与初等数论有关的doc文档。·使用site指令可以搜索指定网站的内容,搜索格式为:关键调+空格+site:+网站。比如:U盘site:jd.co......
  • 使用 typed-rest-client 进行 REST API 调用
    typed-rest-client是一个用于Node.js的库,它提供了一种类型安全的方式来与RESTfulAPI进行交互。其主要功能包括:安装typed-rest-client要使用typed-rest-client,首先需要安装它,可以通过npm来安装:$npminstalltyped-rest-client使用typed-rest-client这里假......
  • el-input type='index' 输入非负数或保留一位小数的数字
    /^(0|[1-9]\d*)$/匹配非负整数^表示字符串的开始。0:匹配单个零。[1-9]\d*:匹配以1到9开头的数字,后面可以跟任意数量的数字(包括零)。$表示字符串的结束。/[^0-9]/g匹配任何非数字字符^在方括号内表示取反,意味着匹配不在指定范围内的字符0-9表示所有数字字符(从0到9)g是一......
  • post请求的body数据类型和content-type的关系
    背景:登陆接口的类型是post,request接口的content-type是multipart/form-data;boundary=----WebKitFormBoundaryxeYAwSy6FSo4kow9response接口的content-type是application/json;charset=utf-8接口的请求体在编写python脚本时post接口的请求头content-type定义了类型multipar......
  • 【TS】TypeScript内置条件类型-ReturnType
    ReturnType在TypeScript中,ReturnType是一个内置的条件类型(ConditionalType),它用于获取一个函数返回值的类型。这个工具类型非常有用,特别是当你需要引用某个函数的返回类型,但又不想直接写出那个具体的类型时。ReturnType的基本语法如下:typeReturnType<Textends(...args:an......
  • 【TS】TypeScript基础入门篇以及实践案例
    Array和Tuple//最简单的方法是使用「类型+方括号」来表示数组:letarrOfNumbers:number[]=[1,2,3,4]//数组的项中不允许出现其他的类型://数组的一些方法的参数也会根据数组在定义时约定的类型进行限制:arrOfNumbers.push(3)arrOfNumbers.push('abc')//报错//元......
  • 2024/09/22:TypeScript 学习笔记二
    1、类型注解在TypeScript中,可以使用类型注解来明确标识类型。如:constgreeting:string='helloworld' 2、类型检查静态类型检查:在程序编译时进行——(两种静态类型检查模式:非严格类型检查【默认方式】;严格类型检查)动态类型检查:在程序运行时进行3、TypeScri......
  • WPF Unable to cast object of type 'System.Windows.Controls.SelectedItemCollectio
    SelectedItemsconverttoIListasbelowfailed;IList<Book>collection2=(IList<Book>)obj; System.InvalidCastExceptionHResult=0x80004002Message=Unabletocastobjectoftype'System.Windows.Controls.SelectedItemCollection'......