Skip to content

Commit

Permalink
Support Rust's non-zero integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
ubolonton committed Mar 15, 2024
1 parent b4fa6f6 commit 3ca7709
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]
- Upgraded to Rust 2021 edition.
- Added support for Rust's non-zero integer types.

## [0.18.0] - 2021-09-26
- Eliminated the build-time dependency on LLVM by putting the raw Rust bindings in source, instead of generating them at build time. This would also make builds faster.
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ rustc_version = "0.2.3"
default = []
utf-8-validation = []
lossy-integer-conversion = []
nonzero-integer-conversion = []
# This feature is intended as a workaround when building on new untested platforms. If the crate
# cannot be built without this feature, please report the issue at
# https://github.com/ubolonton/emacs-module-rs/issues/new.
Expand Down
6 changes: 6 additions & 0 deletions guide/src/type-conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ To disable this behavior, use the `lossy-integer-conversion` feature:
features = ["lossy-integer-conversion"]
```

Support for Rust's `NonZero` integer types is disabled by default. To enable it, use the `nonzero-integer-conversion` feature:
```toml
[dependencies.emacs]
features = ["nonzero-integer-conversion"]
```

## Strings

By default, no utf-8 validation is done when converting Lisp strings into Rust strings, because the string data returned by Emacs is guaranteed to be valid utf-8 sequence. If you think you've otherwise encountered an Emacs bug, utf-8 validation can be enabled through a feature:
Expand Down
62 changes: 62 additions & 0 deletions src/types/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ int_from_lisp!(u32);
int_from_lisp!(u64);
int_from_lisp!(usize);

// -------------------------------------------------------------------------------------------------

macro_rules! nonzero_int_from_lisp {
($name:ident($primitive:ident)) => {
impl FromLisp<'_> for std::num::$name {
#[cfg(not(feature = "lossy-integer-conversion"))]
fn from_lisp(value: Value<'_>) -> Result<std::num::$name> {
let i: i64 = value.into_rust()?;
let i: $primitive = i.try_into()?;
Ok(i.try_into()?)
}

#[cfg(feature = "lossy-integer-conversion")]
fn from_lisp(value: Value<'_>) -> Result<std::num::$name> {
let i: i64 = value.into_rust()?;
let i: $primitive = i as $primitive;
Ok(i.try_into()?)
}
}
}
}

nonzero_int_from_lisp!(NonZeroU8(u8));
nonzero_int_from_lisp!(NonZeroU16(u16));
nonzero_int_from_lisp!(NonZeroU32(u32));
nonzero_int_from_lisp!(NonZeroU64(u64));
nonzero_int_from_lisp!(NonZeroUsize(usize));

nonzero_int_from_lisp!(NonZeroI8(i8));
nonzero_int_from_lisp!(NonZeroI16(i16));
nonzero_int_from_lisp!(NonZeroI32(i32));
nonzero_int_from_lisp!(NonZeroI64(i64));
nonzero_int_from_lisp!(NonZeroIsize(isize));

// -------------------------------------------------------------------------------------------------

impl IntoLisp<'_> for i64 {
fn into_lisp(self, env: &Env) -> Result<Value<'_>> {
unsafe_raw_call_value_unprotected!(env, make_integer, self)
Expand Down Expand Up @@ -81,3 +117,29 @@ int_into_lisp!(isize, lossless);
int_into_lisp!(u64, lossless);
#[cfg(not(feature = "lossy-integer-conversion"))]
int_into_lisp!(usize, lossless);

// -------------------------------------------------------------------------------------------------

macro_rules! nonzero_int_into_lisp {
($name:ident) => {
#[cfg(feature = "nonzero-integer-conversion")]
impl IntoLisp<'_> for std::num::$name {
#[inline]
fn into_lisp(self, env: &Env) -> Result<Value<'_>> {
self.get().into_lisp(env)
}
}
};
}

nonzero_int_into_lisp!(NonZeroU8);
nonzero_int_into_lisp!(NonZeroU16);
nonzero_int_into_lisp!(NonZeroU32);
nonzero_int_into_lisp!(NonZeroU64);
nonzero_int_into_lisp!(NonZeroUsize);

nonzero_int_into_lisp!(NonZeroI8);
nonzero_int_into_lisp!(NonZeroI16);
nonzero_int_into_lisp!(NonZeroI32);
nonzero_int_into_lisp!(NonZeroI64);
nonzero_int_into_lisp!(NonZeroIsize);

0 comments on commit 3ca7709

Please sign in to comment.