当前位置: 首页 > wzjs >正文

做用户运营应该关注哪些网站长春经济技术开发区人才网

做用户运营应该关注哪些网站,长春经济技术开发区人才网,网站后台如何修改标题,福田保安公司招聘一、源码 代码实现了一个类型级别的负数系统,允许在编译期进行负数运算的类型检查。 //! 负数类型及其算术运算实现 //! Negative number type and its arithmetic operations implementation //! //! 本模块定义了类型系统中的负数类型,并为其实现了基…

一、源码

代码实现了一个类型级别的负数系统,允许在编译期进行负数运算的类型检查。

//! 负数类型及其算术运算实现
//! Negative number type and its arithmetic operations implementation
//!
//! 本模块定义了类型系统中的负数类型,并为其实现了基本算术运算。
//! This module defines the negative number type in the type system and implements basic arithmetic operations for it.
//!
//! 注意:与零相关的运算(如 N + 0, N - 0 等)在 zero.rs 模块中实现。
//! Note: Operations involving zero (e.g. N + 0, N - 0, etc.) are implemented in the zero.rs module.use core::ops::{Add, Sub, Mul, Div};
use Positive;  // 正数类型 / Positive number type
use NonZero;   // 非零类型 / Non-zero type
use Integer;   // 整数类型 / Integer typeuse crate::sealed::Sealed;/// 实现Sealed trait表示这是一个私有类型
/// Implementing Sealed trait indicates this is a private type
impl<P: Positive> Sealed for Neg<P> {}/// 负数类型是非零类型
/// Negative numbers are non-zero types
impl<P: Positive> NonZero for Neg<P> {}/// 负数类型,P是对应的正数类型
/// Negative number type, where P is the corresponding positive type
///
/// # 示例
/// # Example
/// ```
/// use type_arithmetic::{Neg, P1};
/// 
/// type N1 = Neg<P1>;  // -1
/// ```
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub struct Neg<P: Positive>(PhantomData<P>);// ========== 加法运算实现 ==========
// ========== Addition Operations ==========/// 负数相加: -a + -b = -(a + b)
/// Negative addition: -a + -b = -(a + b)
///
/// 注意:与零相加的实现在zero.rs中
/// Note: Addition with zero is implemented in zero.rs
impl<P1: Positive, P2: Positive> Add<Neg<P2>> for Neg<P1>
whereP1: Add<P2>,
{type Output = Neg<<P1 as Add<P2>>::Output>;#[inline]fn add(self, _rhs: Neg<P2>) -> Self::Output {Neg(PhantomData)}
}/// 负数加正数: -a + b = b - a
/// Negative plus positive: -a + b = b - a
///
/// 结果可能是正数或负数,取决于b和a的大小关系
/// Result could be positive or negative, depending on the relative sizes of b and a
impl<P1: Positive, P2: Positive> Add<P2> for Neg<P1>
whereP2: Sub<P1>,<P2 as Sub<P1>>::Output: Integer,
{type Output = <P2 as Sub<P1>>::Output;#[inline]fn add(self, _rhs: P2) -> Self::Output {Self::Output::default()}
}/// 正数加负数: a + (-b) = a - b
/// Positive plus negative: a + (-b) = a - b
///
/// 结果可能是正数或负数,取决于a和b的大小关系
/// Result could be positive or negative, depending on the relative sizes of a and b
impl<P1: Positive, P2: Positive> Add<Neg<P2>> for P1
whereP1: Sub<P2>,<P1 as Sub<P2>>::Output: Integer,
{type Output = <P1 as Sub<P2>>::Output;#[inline]fn add(self, _rhs: Neg<P2>) -> Self::Output {Self::Output::default()}
}// ========== 减法运算实现 ==========
// ========== Subtraction Operations ==========/// 负数相减: -a - (-b) = b - a
/// Negative subtraction: -a - (-b) = b - a
///
/// 结果可能是正数或负数,取决于b和a的大小关系
/// Result could be positive or negative, depending on the relative sizes of b and a
///
/// 注意:与零相减的实现在zero.rs中
/// Note: Subtraction with zero is implemented in zero.rs
impl<P1: Positive, P2: Positive> Sub<Neg<P2>> for Neg<P1>
whereP2: Sub<P1>,<P2 as Sub<P1>>::Output: Integer,
{type Output = <P2 as Sub<P1>>::Output;#[inline]fn sub(self, _rhs: Neg<P2>) -> Self::Output {Self::Output::default()}
}/// 负数减正数: -a - b = -(a + b)
/// Negative minus positive: -a - b = -(a + b)
///
/// 结果总是负数
/// Result is always negative
impl<P1: Positive, P2: Positive> Sub<P2> for Neg<P1>
whereP1: Add<P2>,
{type Output = Neg<<P1 as Add<P2>>::Output>;#[inline]fn sub(self, _rhs: P2) -> Self::Output {Neg(PhantomData)}
}/// 正数减负数: a - (-b) = a + b
/// Positive minus negative: a - (-b) = a + b
///
/// 结果总是正数
/// Result is always positive
impl<P1: Positive, P2: Positive> Sub<Neg<P2>> for P1
whereP1: Add<P2>,
{type Output = <P1 as Add<P2>>::Output;#[inline]fn sub(self, _rhs: Neg<P2>) -> Self::Output {Self::Output::default()}
}// ========== 乘法运算实现 ==========
// ========== Multiplication Operations ==========/// 负数相乘: (-a) * (-b) = a * b
/// Negative multiplication: (-a) * (-b) = a * b
///
/// 负负得正
/// Negative times negative equals positive
///
/// 注意:与零相乘的实现在zero.rs中
/// Note: Multiplication with zero is implemented in zero.rs
impl<P1: Positive, P2: Positive> Mul<Neg<P2>> for Neg<P1>
whereP1: Mul<P2>,
{type Output = <P1 as Mul<P2>>::Output;#[inline]fn mul(self, _rhs: Neg<P2>) -> Self::Output {Self::Output::default()}
}/// 负数乘正数: (-a) * b = -(a * b)
/// Negative times positive: (-a) * b = -(a * b)
///
/// 结果总是负数
/// Result is always negative
impl<P1: Positive, P2: Positive> Mul<P2> for Neg<P1>
whereP1: Mul<P2>,<P1 as Mul<P2>>::Output: Positive,
{type Output = Neg<<P1 as Mul<P2>>::Output>;#[inline]fn mul(self, _rhs: P2) -> Self::Output {Neg(PhantomData)}
}/// 正数乘负数: a * (-b) = -(a * b)
/// Positive times negative: a * (-b) = -(a * b)
///
/// 结果总是负数
/// Result is always negative
impl<P1: Positive, P2: Positive> Mul<Neg<P2>> for P1
whereP1: Mul<P2>,<P1 as Mul<P2>>::Output: Positive,
{type Output = Neg<<P1 as Mul<P2>>::Output>;#[inline]fn mul(self, _rhs: Neg<P2>) -> Self::Output {Neg(PhantomData)}
}// ========== 除法运算实现 ==========
// ========== Division Operations ==========/// 负数相除: (-a) / (-b) = a / b
/// Negative division: (-a) / (-b) = a / b
///
/// 负负得正
/// Negative divided by negative equals positive
///
/// 注意:除以零和零除以负数的实现在zero.rs中
/// Note: Division by zero and zero divided by negative are implemented in zero.rs
impl<P1: Positive, P2: Positive> Div<Neg<P2>> for Neg<P1>
whereP1: Div<P2>,
{type Output = <P1 as Div<P2>>::Output;#[inline]fn div(self, _rhs: Neg<P2>) -> Self::Output {Self::Output::default()}
}/// 负数除正数: (-a) / b = -(a / b)
/// Negative divided by positive: (-a) / b = -(a / b)
///
/// 结果总是负数
/// Result is always negative
impl<P1: Positive, P2: Positive> Div<P2> for Neg<P1>
whereP1: Div<P2>,<P1 as Div<P2>>::Output: Positive,
{type Output = Neg<<P1 as Div<P2>>::Output>;#[inline]fn div(self, _rhs: P2) -> Self::Output {Neg(PhantomData)}
}/// 正数除负数: a / (-b) = -(a / b)
/// Positive divided by negative: a / (-b) = -(a / b)
///
/// 结果总是负数
/// Result is always negative
impl<P1: Positive, P2: Positive> Div<Neg<P2>> for P1
whereP1: Div<P2>,<P1 as Div<P2>>::Output: Positive,
{type Output = Neg<<P1 as Div<P2>>::Output>;#[inline]fn div(self, _rhs: Neg<P2>) -> Self::Output {Neg(PhantomData)}
}

二、基本结构

类型定义
pub struct Neg<P: Positive>(PhantomData<P>);
  • Neg

    表示负数类型,其中P是相应的正数类型

  • 使用PhantomData来持有类型参数但不占用运行时空间

  • 例如Neg表示-1,其中P1表示正数1

标记trait实现
impl<P: Positive> Sealed for Neg<P> {}
impl<P: Positive> NonZero for Neg<P> {}
  • Sealed trait防止外部代码实现这些trait

  • NonZero标记表示负数永远是非零值

三、算术运算实现

加法运算
// -a + -b = -(a + b)
impl<P1, P2> Add<Neg<P2>> for Neg<P1> where P1: Add<P2> {type Output = Neg<<P1 as Add<P2>>::Output>;
}// -a + b = b - a
impl<P1, P2> Add<P2> for Neg<P1> where P2: Sub<P1> {type Output = <P2 as Sub<P1>>::Output;
}// a + (-b) = a - b
impl<P1, P2> Add<Neg<P2>> for P1 where P1: Sub<P2> {type Output = <P1 as Sub<P2>>::Output;
  • 实现了三种加法组合:负数+负数、负数+正数、正数+负数

  • 每种情况都遵循数学规则并返回正确的类型

减法运算
// -a - (-b) = b - a
impl<P1, P2> Sub<Neg<P2>> for Neg<P1> where P2: Sub<P1> {type Output = <P2 as Sub<P1>>::Output;
}// -a - b = -(a + b)
impl<P1, P2> Sub<P2> for Neg<P1> where P1: Add<P2> {type Output = Neg<<P1 as Add<P2>>::Output>;
}// a - (-b) = a + b
impl<P1, P2> Sub<Neg<P2>> for P1 where P1: Add<P2> {type Output = <P1 as Add<P2>>::Output;
  • 实现了三种减法组合

  • 特别注意-a - (-b)转换为b - a的巧妙处理

乘法运算
// (-a) * (-b) = a * b
impl<P1, P2> Mul<Neg<P2>> for Neg<P1> where P1: Mul<P2> {type Output = <P1 as Mul<P2>>::Output;
}// (-a) * b = -(a * b)
impl<P1, P2> Mul<P2> for Neg<P1> where P1: Mul<P2> {type Output = Neg<<P1 as Mul<P2>>::Output>;
}// a * (-b) = -(a * b)
impl<P1, P2> Mul<Neg<P2>> for P1 where P1: Mul<P2> {type Output = Neg<<P1 as Mul<P2>>::Output>;
  • 完全遵循数学乘法规则

  • 负负得正,正负得负

除法运算
// (-a) / (-b) = a / b
impl<P1, P2> Div<Neg<P2>> for Neg<P1> where P1: Div<P2> {type Output = <P1 as Div<P2>>::Output;
}// (-a) / b = -(a / b)
impl<P1, P2> Div<P2> for Neg<P1> where P1: Div<P2> {type Output = Neg<<P1 as Div<P2>>::Output>;
}// a / (-b) = -(a / b)
impl<P1, P2> Div<Neg<P2>> for P1 where P1: Div<P2> {type Output = Neg<<P1 as Div<P2>>::Output>;
  • 遵循数学除法规则

  • 特别注意没有实现除以零的情况

四、设计特点

  1. 类型安全:所有运算在编译期进行类型检查

  2. 零开销:使用PhantomData,运行时无额外开销

  3. 数学正确:严格遵循数学运算规则

  4. 模块化设计:与零相关的运算分离到zero.rs模块

  5. 完整文档:包含中英文注释和示例

五、使用示例

type N1 = Neg<P1>;  // -1
type N2 = Neg<P2>;  // -2// 类型级别运算
type Sum = <N1 as Add<N2>>::Output;  // -1 + -2 = -3 (Neg<P3>)
type Diff = <N1 as Sub<P2>>::Output; // -1 - 2 = -3 (Neg<P3>)
type Product = <N1 as Mul<N2>>::Output; // -1 * -2 = 2 (P2)

这个实现展示了Rust类型系统的强大能力,可以在编译期捕获数学运算的类型约束和规则。


文章转载自:

http://RAn8UGyV.bnjnp.cn
http://TzT0Mj7x.bnjnp.cn
http://KZShveEp.bnjnp.cn
http://EFwMBW0s.bnjnp.cn
http://fgT1W6Ty.bnjnp.cn
http://kU5SE1ty.bnjnp.cn
http://IjMtYKwH.bnjnp.cn
http://S4HS3pSn.bnjnp.cn
http://mm6EDyRZ.bnjnp.cn
http://gqoAPtSd.bnjnp.cn
http://DDu5xAue.bnjnp.cn
http://NePvt3J2.bnjnp.cn
http://IJCWmWAf.bnjnp.cn
http://VfpXv3oJ.bnjnp.cn
http://KO88J0y1.bnjnp.cn
http://GsJVtJvO.bnjnp.cn
http://hJWtOX5e.bnjnp.cn
http://7fNfGQeA.bnjnp.cn
http://cHBWPK2t.bnjnp.cn
http://PJxObwkQ.bnjnp.cn
http://qEi7DQDw.bnjnp.cn
http://kFAOQ3Xp.bnjnp.cn
http://j9OMLMV8.bnjnp.cn
http://mh3yp1Xx.bnjnp.cn
http://ol1mVhhY.bnjnp.cn
http://GlQXfBbs.bnjnp.cn
http://JWScRBuF.bnjnp.cn
http://SDM84etm.bnjnp.cn
http://mMG5HIFE.bnjnp.cn
http://6O9s1bCo.bnjnp.cn
http://www.dtcms.com/wzjs/666091.html

相关文章:

  • 网站服务器搬家html做静态网站
  • wordpress图片上传插件seo搜索引擎优化推广
  • 什么网站做一件代发天津差旅管家商旅服务有限公司
  • 自由贸易区的建设网站怎么推广我的网站
  • 手机网站电话漂浮代码红河做网站
  • 公司企业网站制作《30天网站建设实录》
  • 有哪些可以做兼职的网站手机商城怎么做
  • 容县网站建设中小学智慧校园建设平台网站
  • 怎么在网站后台挂马烟台做网站建设电话
  • 网站优化公司上海网站制作需要多少钱官网
  • 企业做网站需要的资料什么云的网站开发平台
  • 手机网站平台电子商务网站建设 价格
  • 福建建设厅网站工程履约保险工作证明范本
  • 沧州市有建网站的吗网页设计用的软件
  • 成都广告牌制作厂家搜索引擎排名优化程序
  • 网站鼠标移上去显示层国外网站都不能上怎么做跨境电商
  • 河南网站建设推荐网站的用户体验
  • 河南锦源建设有限公司网站eclipse做购物网站
  • 专业建站威海高区有没有建设局的网站
  • windows系统做ppt下载网站网站代码的重点内容是什么
  • 做设计接私活的网站做网站建设销售工资高吗
  • 泉州网页网站制作中工信融营销型网站建设
  • 做网站需要用到的软件国旗做网站按钮违法吗
  • 站酷app网站百度
  • 国内做市场调查专业网站软文300字案例
  • 网站网站做代理微信群卖房卡wordpress阿里云配置文件
  • 企业网站建设框架网站不维护会怎么样
  • 实用网站建设wordpress文章显示作者信息
  • 做展馆好的设计网站网站快速推广
  • 网站建设竞价托管什么意思深圳 德 网站建设