news 2026/8/29 7:21:53

【time-rs】月份枚举实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【time-rs】月份枚举实现
// 基本定义#[repr(u8)]pubenumMonth{January=1,February=2,March=3,April=4,May=5,June=6,July=7,August=8,September=9,October=10,November=11,December=12,}

1. 枚举定义和特征

月份枚举1开始编号而不是 0,这符合日常习惯:

#[repr(u8)]// 确保以 u8 形式存储#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]pubenumMonth{January=1,// 不是 0!// ... 其他月份December=12,}

2. 核心方法实现

2.1 创建和转换方法

implMonth{/// 从数字创建月份pub(crate)constfnfrom_number(n:NonZero<u8>)->Result<Self,error::ComponentRange>{matchn.get(){1=>Ok(January),// ... 2-1112=>Ok(December),n=>Err(error::ComponentRange{/* 错误详情 */}),}}/// 获取月份天数(考虑闰年)pubconstfnlength(self,year:i32)->u8{util::days_in_month(self,year)}}

2.2 月份导航方法

implMonth{/// 获取上个月pubconstfnprevious(self)->Self{matchself{January=>December,February=>January,// ... 其他匹配}}/// 获取下个月pubconstfnnext(self)->Self{matchself{January=>February,// ... 其他匹配December=>January,}}/// 获取第 n 个下个月pubconstfnnth_next(self,n:u8)->Self{match(selfasu8-1+n%12)%12{0=>January,// ... 1-1011=>December,_=>unreachable!(),}}}

3. 特性实现

3.1 智能显示(SmartDisplay)

implSmartDisplayforMonth{typeMetadata=MonthMetadata;fnmetadata(&self,_:FormatterOptions)->Metadata<'_,Self>{matchself{January=>Metadata::new(7,self,MonthMetadata),// "January" 长度 7February=>Metadata::new(8,self,MonthMetadata),// "February" 长度 8// ... 其他月份}}fnfmt(&self,f:&mutfmt::Formatter<'_>)->fmt::Result{f.pad(matchself{January=>"January",// ... 其他月份名称})}}

3.2 标准特性实现

// 显示实现委托给 SmartDisplayimplfmt::DisplayforMonth{fnfmt(&self,f:&mutfmt::Formatter<'_>)->fmt::Result{SmartDisplay::fmt(self,f)}}// 从字符串解析implFromStrforMonth{typeErr=error::InvalidVariant;fnfrom_str(s:&str)->Result<Self,Self::Err>{matchs{"January"=>Ok(January),// ... 其他月份名称_=>Err(error::InvalidVariant),}}}

3.3 类型转换

// Month -> u8implFrom<Month>foru8{fnfrom(month:Month)->Self{monthasSelf// 安全:月份值 1-12}}// u8 -> Month(安全转换)implTryFrom<u8>forMonth{typeError=error::ComponentRange;fntry_from(value:u8)->Result<Self,Self::Error>{matchNonZero::new(value){Some(value)=>Self::from_number(value),None=>Err(/* 错误:值为 0 */),}}}

4. 设计特点总结

特点说明
类型安全使用枚举而非整数,防止无效月份值
零成本抽象使用const函数和内联优化
内存优化使用NonZero<u8>#[repr(u8)]
完整功能支持显示、解析、导航、天数计算
实用 API提供日常需要的月份操作

5. 使用示例

// 创建月份letjanuary=Month::January;letfebruary=Month::try_from(2).unwrap();// 显示月份println!("Current month: {}",january);// "January"// 计算天数(考虑闰年)println!("Days in Feb 2020: {}",Month::February.length(2020));// 29// 月份导航letnext_month=january.next();// Februaryletthree_months_later=january.nth_next(3);// April// 类型转换letmonth_num:u8=january.into();// 1letfrom_str:Month="March".parse().unwrap();// Month::March
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/27 9:43:15

Redhat Debian Suse 主流OS 之间的区别

Fedora 介绍&#xff08;官网&#xff09;: https://docs.fedoraproject.org/en-US/project/ Redhat &#xff1a;https://www.redhat.com/zh-cn?ohwww.redhat.com.cn suse 官网 关于我们(中文版) https://www.suse.com/zh-cn/company/about/#suse-history debian 官网介绍: …

作者头像 李华
网站建设 2026/8/29 7:11:10

磁盘分区与文件系统格式相关

MBR 重要数据&#xff1a; 主引导扇区&#xff08;512字节&#xff09; 主引导记录&#xff1a;记录系统引导程序相关数据&#xff08;前446字节&#xff09;分区表&#xff1a;记录磁盘分区开始位置、大小等信息&#xff08;4*1664字节&#xff09;魔数&#xff1a;0x55AA&a…

作者头像 李华
网站建设 2026/8/28 4:47:24

新品发布 | 生产、制造及售后领域强有力的VCI接口M810

MC810是MC产品系列的又一个强大的VCI。凭借紧凑的设计和WiFi、USB及蓝牙作为主机系统的接口&#xff0c;以及CAN(FD)、以太网到车辆&#xff0c;MC810特别适合面向未来的制造和售后服务应用。一、应用场景下图展示了通过车辆通信卡进行诊断测试的系统框图。其中&#xff0c;PC可…

作者头像 李华
网站建设 2026/8/28 15:55:54

LLaMA Factory微调大模型完整指南:从数据准备到API部署

本文详细介绍了使用LLaMA Factory对大语言模型进行指令监督微调(SFT)的完整流程。从数据准备、清洗和格式化开始&#xff0c;讲解了全量微调(Full FT)和参数高效微调(PEFT/LoRA)两种技术方法&#xff0c;最后通过LLaMA Factory API和Ollama API实现模型部署。文章提供了详细的配…

作者头像 李华
网站建设 2026/8/28 19:56:02

谷歌PH-LLM大模型:可穿戴设备数据的健康洞察革命

谷歌推出基于Gemini模型的PH-LLM大模型和智能Agent系统&#xff0c;能够分析可穿戴设备数据并生成个性化健康建议。PH-LLM在睡眠和健身领域表现接近专家水平&#xff0c;而智能Agent可通过迭代推理提供准确健康洞察。这两项技术为开发真正个性化的健康助手奠定基础&#xff0c;…

作者头像 李华
网站建设 2026/8/28 22:52:44

效率提升超24倍!如何实现财务报表OCR识别自动录入?

借助智能财报录入系统&#xff0c;银行将PDF、图片等非结构化财报秒级转为结构化数据。该方案能精准解析印章遮挡及跨页表格&#xff0c;通过自动勾稽校验确保准确性&#xff0c;将作业效率提升超24倍&#xff0c;解决人工录入效率低、易出错的痛点。 为什么银行必须升级到智能…

作者头像 李华