Skip to content

Commit 9e98bb1

Browse files
authored
Merge pull request #1056 from schungx/master
Enable building with getrandom 0.3.
2 parents 6548c2b + cd28a36 commit 9e98bb1

File tree

6 files changed

+241
-11
lines changed

6 files changed

+241
-11
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
2323
- run: cargo hack check --feature-powerset --depth ${{ inputs.depth }} --no-dev-deps --exclude-features "$FEATURE_EXCLUDE"
2424
env:
25-
FEATURE_EXCLUDE: "no_std stdweb wasm-bindgen f32_float only_i32 unicode-xid-ident bin-features"
25+
FEATURE_EXCLUDE: "no_std wasm-bindgen f32_float only_i32 unicode-xid-ident bin-features"

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
Rhai Release Notes
22
==================
33

4+
Version 1.24.0
5+
==============
6+
7+
Breaking Changes
8+
----------------
9+
10+
* `stdweb` support is removed. The feature flag `stdweb` is also removed. Use `wasm-bindgen` instead.
11+
12+
13+
Enhancements
14+
------------
15+
16+
* The optimizer now optimizes constant object map property accesses (thanks [`@phsym`](https://github.com/phsym) [#1050](https://github.com/rhaiscript/rhai/pull/1050)).
17+
* The method `sort` for arrays is also aliased to `sort_by`.
18+
* The methods `sort_desc`, `order`, `order_by` and `order_desc` are added to arrays.
19+
20+
421
Version 1.23.6
522
==============
623

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ serde = { version = "1.0.136", default-features = false, features = ["derive", "
3535
serde_json = { version = "1.0.45", default-features = false, features = ["alloc"], optional = true }
3636
unicode-xid = { version = "0.2.0", default-features = false, optional = true }
3737
rust_decimal = { version = "1.24.0", default-features = false, features = ["maths"], optional = true }
38-
getrandom = { version = "0.2.7", optional = true }
38+
getrandom = { version = "0.3.4", optional = true }
3939
rustyline = { version = "15.0.0", optional = true }
4040
document-features = { version = "0.2.0", optional = true }
4141
arbitrary = { version = "1.3.2", optional = true, features = ["derive"] }
@@ -118,9 +118,7 @@ no_std = ["no-std-compat", "num-traits/libm", "core-error", "libm", "hashbrown",
118118
#! ### JavaScript Interface for WASM
119119

120120
## Use [`wasm-bindgen`](https://crates.io/crates/wasm-bindgen) as JavaScript interface.
121-
wasm-bindgen = ["getrandom/js", "instant/wasm-bindgen"]
122-
## Use [`stdweb`](https://crates.io/crates/stdweb) as JavaScript interface.
123-
stdweb = ["getrandom/js", "instant/stdweb"]
121+
wasm-bindgen = ["getrandom/wasm_js", "instant/wasm-bindgen"]
124122

125123
#! ### Features used in testing environments only
126124

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ Targets and builds
3232
Versions
3333
--------
3434

35-
The [`ahash`](https://crates.io/crates/ahash) crate that Rhai depends on has a breaking change since version `0.8.12` which bumps [`getrandom`](https://crates.io/crates/getrandom) to version `0.3`, braking certain `no-std` and `wasm` builds.
35+
The [`ahash`](https://crates.io/crates/ahash) crate that Rhai depends on has a breaking change since version `0.8.12` which bumps [`getrandom`](https://crates.io/crates/getrandom) to version `0.3`, braking certain `no-std` and WASM builds.
3636

37-
Version [`1.23.3`](https://crates.io/crates/rhai/1.23.3): Use this version when building for `no-std`
37+
Version [`1.23.5`](https://crates.io/crates/rhai/1.23.5): Use this version when building for `no-std` or WASM.
3838

39-
Version [`1.23.4`](https://crates.io/crates/rhai/1.23.4): This is the main version for `std` builds.
39+
Version [`1.23.6`](https://crates.io/crates/rhai/1.23.6): This is the main version for `std` builds.
4040

4141

4242
Standard features

src/api/deprecated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ pub mod deprecated_array_functions {
13431343
array: &mut Array,
13441344
comparer: &str,
13451345
) -> RhaiResultOf<()> {
1346-
sort(ctx, array, FnPtr::new(comparer)?);
1346+
sort_by(ctx, array, FnPtr::new(comparer)?);
13471347
Ok(())
13481348
}
13491349
/// Remove all elements in the array that returns `true` when applied a function named by `filter`

src/packages/array_basic.rs

Lines changed: 217 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,11 +1540,12 @@ pub mod array_functions {
15401540
/// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
15411541
///
15421542
/// // Do comparisons in reverse
1543-
/// x.sort(|a, b| if a > b { -1 } else if a < b { 1 } else { 0 });
1543+
/// x.sort_by(|a, b| if a > b { -1 } else if a < b { 1 } else { 0 });
15441544
///
15451545
/// print(x); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
15461546
/// ```
1547-
pub fn sort(ctx: NativeCallContext, array: &mut Array, comparer: FnPtr) {
1547+
#[rhai_fn(name = "sort", name = "sort_by")]
1548+
pub fn sort_by(ctx: NativeCallContext, array: &mut Array, comparer: FnPtr) {
15481549
if array.len() <= 1 {
15491550
return;
15501551
}
@@ -1664,6 +1665,220 @@ pub mod array_functions {
16641665

16651666
Ok(())
16661667
}
1668+
/// Sort the array in descending order.
1669+
///
1670+
/// All elements in the array must be of the same data type.
1671+
///
1672+
/// # Supported Data Types
1673+
///
1674+
/// * integer numbers
1675+
/// * floating-point numbers
1676+
/// * decimal numbers
1677+
/// * characters
1678+
/// * strings
1679+
/// * booleans
1680+
/// * `()`
1681+
///
1682+
/// # Example
1683+
///
1684+
/// ```rhai
1685+
/// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1686+
///
1687+
/// x.sort_desc();
1688+
///
1689+
/// print(x); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
1690+
/// ```
1691+
#[rhai_fn(name = "sort_desc", return_raw)]
1692+
pub fn sort_with_builtin_desc(array: &mut Array) -> RhaiResultOf<()> {
1693+
if array.len() <= 1 {
1694+
return Ok(());
1695+
}
1696+
1697+
let type_id = array[0].type_id();
1698+
1699+
if array.iter().any(|a| a.type_id() != type_id) {
1700+
return Err(ERR::ErrorFunctionNotFound(
1701+
"sort_desc() cannot be called with elements of different types".into(),
1702+
Position::NONE,
1703+
)
1704+
.into());
1705+
}
1706+
1707+
if type_id == TypeId::of::<INT>() {
1708+
array.sort_by(|a, b| {
1709+
let a = a.as_int().unwrap();
1710+
let b = b.as_int().unwrap();
1711+
match a.cmp(&b) {
1712+
Ordering::Less => Ordering::Greater,
1713+
Ordering::Equal => Ordering::Equal,
1714+
Ordering::Greater => Ordering::Less,
1715+
}
1716+
});
1717+
return Ok(());
1718+
}
1719+
if type_id == TypeId::of::<char>() {
1720+
array.sort_by(|a, b| {
1721+
let a = a.as_char().unwrap();
1722+
let b = b.as_char().unwrap();
1723+
match a.cmp(&b) {
1724+
Ordering::Less => Ordering::Greater,
1725+
Ordering::Equal => Ordering::Equal,
1726+
Ordering::Greater => Ordering::Less,
1727+
}
1728+
});
1729+
return Ok(());
1730+
}
1731+
#[cfg(not(feature = "no_float"))]
1732+
if type_id == TypeId::of::<crate::FLOAT>() {
1733+
array.sort_by(|a, b| {
1734+
let a = a.as_float().unwrap();
1735+
let b = b.as_float().unwrap();
1736+
match a.partial_cmp(&b).unwrap_or(Ordering::Equal) {
1737+
Ordering::Less => Ordering::Greater,
1738+
Ordering::Equal => Ordering::Equal,
1739+
Ordering::Greater => Ordering::Less,
1740+
}
1741+
});
1742+
return Ok(());
1743+
}
1744+
if type_id == TypeId::of::<ImmutableString>() {
1745+
array.sort_by(|a, b| {
1746+
let a = &*a.as_immutable_string_ref().unwrap();
1747+
let b = &*b.as_immutable_string_ref().unwrap();
1748+
match a.cmp(&b) {
1749+
Ordering::Less => Ordering::Greater,
1750+
Ordering::Equal => Ordering::Equal,
1751+
Ordering::Greater => Ordering::Less,
1752+
}
1753+
});
1754+
return Ok(());
1755+
}
1756+
#[cfg(feature = "decimal")]
1757+
if type_id == TypeId::of::<rust_decimal::Decimal>() {
1758+
array.sort_by(|a, b| {
1759+
let a = a.as_decimal().unwrap();
1760+
let b = b.as_decimal().unwrap();
1761+
match a.cmp(&b) {
1762+
Ordering::Less => Ordering::Greater,
1763+
Ordering::Equal => Ordering::Equal,
1764+
Ordering::Greater => Ordering::Less,
1765+
}
1766+
});
1767+
return Ok(());
1768+
}
1769+
if type_id == TypeId::of::<bool>() {
1770+
array.sort_by(|a, b| {
1771+
let a = a.as_bool().unwrap();
1772+
let b = b.as_bool().unwrap();
1773+
match a.cmp(&b) {
1774+
Ordering::Less => Ordering::Greater,
1775+
Ordering::Equal => Ordering::Equal,
1776+
Ordering::Greater => Ordering::Less,
1777+
}
1778+
});
1779+
return Ok(());
1780+
}
1781+
if type_id == TypeId::of::<()>() {
1782+
return Ok(());
1783+
}
1784+
1785+
Ok(())
1786+
}
1787+
/// Sort the array based on applying the `comparer` function and return it as a new array.
1788+
///
1789+
/// # Function Parameters
1790+
///
1791+
/// * `element1`: copy of the current array element to compare
1792+
/// * `element2`: copy of the next array element to compare
1793+
///
1794+
/// ## Return Value
1795+
///
1796+
/// An integer number:
1797+
///
1798+
/// * Any positive integer if `element1 > element2`
1799+
/// * 0 if `element1 == element2`
1800+
/// * Any negative integer if `element1 < element2`
1801+
///
1802+
/// or a boolean value:
1803+
///
1804+
/// * `true` if `element1 <= element2`
1805+
/// * `false` if `element1 > element2`
1806+
///
1807+
/// Any other return value type will yield unpredictable order.
1808+
///
1809+
/// # Example
1810+
///
1811+
/// ```rhai
1812+
/// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1813+
///
1814+
/// // Do comparisons in reverse
1815+
/// let y = x.order_by(|a, b| if a > b { -1 } else if a < b { 1 } else { 0 });
1816+
///
1817+
/// print(y); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
1818+
/// ```
1819+
pub fn order_by(ctx: NativeCallContext, array: &mut Array, comparer: FnPtr) -> Array {
1820+
let mut array = array.clone();
1821+
sort_by(ctx, &mut array, comparer);
1822+
array
1823+
}
1824+
/// Sort the array and return it as a new array.
1825+
///
1826+
/// All elements in the array must be of the same data type.
1827+
///
1828+
/// # Supported Data Types
1829+
///
1830+
/// * integer numbers
1831+
/// * floating-point numbers
1832+
/// * decimal numbers
1833+
/// * characters
1834+
/// * strings
1835+
/// * booleans
1836+
/// * `()`
1837+
///
1838+
/// # Example
1839+
///
1840+
/// ```rhai
1841+
/// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1842+
///
1843+
/// let y = x.order();
1844+
///
1845+
/// print(y); // prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
1846+
/// ```
1847+
#[rhai_fn(name = "order", return_raw)]
1848+
pub fn order_with_builtin(array: &mut Array) -> RhaiResultOf<Array> {
1849+
let mut array = array.clone();
1850+
sort_with_builtin(&mut array)?;
1851+
Ok(array)
1852+
}
1853+
/// Sort the array in descending order and return it as a new array.
1854+
///
1855+
/// All elements in the array must be of the same data type.
1856+
///
1857+
/// # Supported Data Types
1858+
///
1859+
/// * integer numbers
1860+
/// * floating-point numbers
1861+
/// * decimal numbers
1862+
/// * characters
1863+
/// * strings
1864+
/// * booleans
1865+
/// * `()`
1866+
///
1867+
/// # Example
1868+
///
1869+
/// ```rhai
1870+
/// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1871+
///
1872+
/// let y = x.order_desc();
1873+
///
1874+
/// print(y); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
1875+
/// ```
1876+
#[rhai_fn(name = "order_desc", return_raw)]
1877+
pub fn order_with_builtin_desc(array: &mut Array) -> RhaiResultOf<Array> {
1878+
let mut array = array.clone();
1879+
sort_with_builtin_desc(&mut array)?;
1880+
Ok(array)
1881+
}
16671882
/// Remove all elements in the array that returns `true` when applied the `filter` function and
16681883
/// return them as a new array.
16691884
///

0 commit comments

Comments
 (0)