poet_rhai/api/deprecated.rs
1//! Module containing all deprecated API that will be removed in the next major version.
2
3use crate::func::{RhaiFunc, SendSync};
4use crate::types::dynamic::Variant;
5use crate::{
6 Dynamic, Engine, EvalAltResult, FnAccess, FnNamespace, FnPtr, FuncRegistration, Identifier,
7 ImmutableString, Module, NativeCallContext, Position, RhaiNativeFunc, RhaiResult, RhaiResultOf,
8 Scope, SharedModule, TypeBuilder, AST,
9};
10use std::any::TypeId;
11#[cfg(feature = "no_std")]
12use std::prelude::v1::*;
13
14#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
15use crate::func::register::Mut;
16
17#[cfg(not(feature = "no_std"))]
18#[cfg(any(not(target_family = "wasm"), not(target_os = "unknown")))]
19use std::path::PathBuf;
20
21impl Engine {
22 /// Evaluate a file, but throw away the result and only return error (if any).
23 /// Useful for when you don't need the result, but still need to keep track of possible errors.
24 ///
25 /// Not available under `no_std` or `WASM`.
26 ///
27 /// # Deprecated
28 ///
29 /// This method is deprecated.
30 /// Use [`run_file`][Engine::run_file] instead.
31 ///
32 /// This method will be removed in the next major version.
33 #[deprecated(since = "1.1.0", note = "use `run_file` instead")]
34 #[cfg(not(feature = "no_std"))]
35 #[cfg(any(not(target_family = "wasm"), not(target_os = "unknown")))]
36 #[inline(always)]
37 pub fn consume_file(&self, path: PathBuf) -> RhaiResultOf<()> {
38 self.run_file(path)
39 }
40
41 /// Evaluate a file with own scope, but throw away the result and only return error (if any).
42 /// Useful for when you don't need the result, but still need to keep track of possible errors.
43 ///
44 /// Not available under `no_std` or `WASM`.
45 ///
46 /// # Deprecated
47 ///
48 /// This method is deprecated.
49 /// Use [`run_file_with_scope`][Engine::run_file_with_scope] instead.
50 ///
51 /// This method will be removed in the next major version.
52 #[deprecated(since = "1.1.0", note = "use `run_file_with_scope` instead")]
53 #[cfg(not(feature = "no_std"))]
54 #[cfg(any(not(target_family = "wasm"), not(target_os = "unknown")))]
55 #[inline(always)]
56 pub fn consume_file_with_scope(&self, scope: &mut Scope, path: PathBuf) -> RhaiResultOf<()> {
57 self.run_file_with_scope(scope, path)
58 }
59
60 /// Evaluate a string, but throw away the result and only return error (if any).
61 /// Useful for when you don't need the result, but still need to keep track of possible errors.
62 ///
63 /// # Deprecated
64 ///
65 /// This method is deprecated.
66 /// Use [`run`][Engine::run] instead.
67 ///
68 /// This method will be removed in the next major version.
69 #[deprecated(since = "1.1.0", note = "use `run` instead")]
70 #[inline(always)]
71 pub fn consume(&self, script: &str) -> RhaiResultOf<()> {
72 self.run(script)
73 }
74
75 /// Evaluate a string with own scope, but throw away the result and only return error (if any).
76 /// Useful for when you don't need the result, but still need to keep track of possible errors.
77 ///
78 /// # Deprecated
79 ///
80 /// This method is deprecated.
81 /// Use [`run_with_scope`][Engine::run_with_scope] instead.
82 ///
83 /// This method will be removed in the next major version.
84 #[deprecated(since = "1.1.0", note = "use `run_with_scope` instead")]
85 #[inline(always)]
86 pub fn consume_with_scope(&self, scope: &mut Scope, script: &str) -> RhaiResultOf<()> {
87 self.run_with_scope(scope, script)
88 }
89
90 /// Evaluate an [`AST`], but throw away the result and only return error (if any).
91 /// Useful for when you don't need the result, but still need to keep track of possible errors.
92 ///
93 /// # Deprecated
94 ///
95 /// This method is deprecated.
96 /// Use [`run_ast`][Engine::run_ast] instead.
97 ///
98 /// This method will be removed in the next major version.
99 #[deprecated(since = "1.1.0", note = "use `run_ast` instead")]
100 #[inline(always)]
101 pub fn consume_ast(&self, ast: &AST) -> RhaiResultOf<()> {
102 self.run_ast(ast)
103 }
104
105 /// Evaluate an [`AST`] with own scope, but throw away the result and only return error (if any).
106 /// Useful for when you don't need the result, but still need to keep track of possible errors.
107 ///
108 /// # Deprecated
109 ///
110 /// This method is deprecated.
111 /// Use [`run_ast_with_scope`][Engine::run_ast_with_scope] instead.
112 ///
113 /// This method will be removed in the next major version.
114 #[deprecated(since = "1.1.0", note = "use `run_ast_with_scope` instead")]
115 #[inline(always)]
116 pub fn consume_ast_with_scope(&self, scope: &mut Scope, ast: &AST) -> RhaiResultOf<()> {
117 self.run_ast_with_scope(scope, ast)
118 }
119 /// Call a script function defined in an [`AST`] with multiple [`Dynamic`] arguments
120 /// and optionally a value for binding to the `this` pointer.
121 ///
122 /// Not available under `no_function`.
123 ///
124 /// There is an option to evaluate the [`AST`] to load necessary modules before calling the function.
125 ///
126 /// # Deprecated
127 ///
128 /// This method is deprecated.
129 /// Use [`call_fn_with_options`][Engine::call_fn_with_options] instead.
130 ///
131 /// This method will be removed in the next major version.
132 #[deprecated(since = "1.1.0", note = "use `call_fn_with_options` instead")]
133 #[cfg(not(feature = "no_function"))]
134 #[inline(always)]
135 pub fn call_fn_dynamic(
136 &self,
137 scope: &mut Scope,
138 ast: &AST,
139 eval_ast: bool,
140 name: impl AsRef<str>,
141 this_ptr: Option<&mut Dynamic>,
142 arg_values: impl AsMut<[Dynamic]>,
143 ) -> RhaiResult {
144 #[allow(deprecated)]
145 self.call_fn_raw(scope, ast, eval_ast, true, name, this_ptr, arg_values)
146 }
147 /// Call a script function defined in an [`AST`] with multiple [`Dynamic`] arguments.
148 ///
149 /// Not available under `no_function`.
150 ///
151 /// # Deprecated
152 ///
153 /// This method is deprecated.
154 /// Use [`call_fn_with_options`][Engine::call_fn_with_options] instead.
155 ///
156 /// This method will be removed in the next major version.
157 #[deprecated(since = "1.12.0", note = "use `call_fn_with_options` instead")]
158 #[cfg(not(feature = "no_function"))]
159 #[inline(always)]
160 pub fn call_fn_raw(
161 &self,
162 scope: &mut Scope,
163 ast: &AST,
164 eval_ast: bool,
165 rewind_scope: bool,
166 name: impl AsRef<str>,
167 this_ptr: Option<&mut Dynamic>,
168 arg_values: impl AsMut<[Dynamic]>,
169 ) -> RhaiResult {
170 let mut arg_values = arg_values;
171
172 let options = crate::CallFnOptions {
173 this_ptr,
174 eval_ast,
175 rewind_scope,
176 ..<_>::default()
177 };
178
179 self._call_fn(
180 options,
181 scope,
182 ast,
183 name.as_ref(),
184 arg_values.as_mut(),
185 &mut self.new_global_runtime_state(),
186 &mut crate::eval::Caches::new(),
187 )
188 }
189 /// Register a custom fallible function with the [`Engine`].
190 ///
191 /// # Deprecated
192 ///
193 /// This method is deprecated.
194 /// Use [`register_fn`][Engine::register_fn] instead.
195 ///
196 /// This method will be removed in the next major version.
197 #[deprecated(since = "1.9.1", note = "use `register_fn` instead")]
198 #[inline(always)]
199 pub fn register_result_fn<A: 'static, const N: usize, const X: bool, R: Variant + Clone>(
200 &mut self,
201 name: impl AsRef<str> + Into<Identifier>,
202 func: impl RhaiNativeFunc<A, N, X, R, true> + SendSync + 'static,
203 ) -> &mut Self {
204 self.register_fn(name, func)
205 }
206 /// Register a getter function for a member of a registered type with the [`Engine`].
207 ///
208 /// The function signature must start with `&mut self` and not `&self`.
209 ///
210 /// Not available under `no_object`.
211 ///
212 /// # Deprecated
213 ///
214 /// This method is deprecated.
215 /// Use [`register_get`][Engine::register_get] instead.
216 ///
217 /// This method will be removed in the next major version.
218 #[deprecated(since = "1.9.1", note = "use `register_get` instead")]
219 #[cfg(not(feature = "no_object"))]
220 #[inline(always)]
221 pub fn register_get_result<T: Variant + Clone, const X: bool, R: Variant + Clone>(
222 &mut self,
223 name: impl AsRef<str>,
224 get_fn: impl RhaiNativeFunc<(Mut<T>,), 1, X, R, true> + SendSync + 'static,
225 ) -> &mut Self {
226 self.register_get(name, get_fn)
227 }
228 /// Register a setter function for a member of a registered type with the [`Engine`].
229 ///
230 /// Not available under `no_object`.
231 ///
232 /// # Deprecated
233 ///
234 /// This method is deprecated.
235 /// Use [`register_set`][Engine::register_set] instead.
236 ///
237 /// This method will be removed in the next major version.
238 #[deprecated(since = "1.9.1", note = "use `register_set` instead")]
239 #[cfg(not(feature = "no_object"))]
240 #[inline(always)]
241 pub fn register_set_result<T: Variant + Clone, V: Variant + Clone, const X: bool>(
242 &mut self,
243 name: impl AsRef<str>,
244 set_fn: impl RhaiNativeFunc<(Mut<T>, V), 2, X, (), true> + SendSync + 'static,
245 ) -> &mut Self {
246 self.register_set(name, set_fn)
247 }
248 /// Register an index getter for a custom type with the [`Engine`].
249 ///
250 /// The function signature must start with `&mut self` and not `&self`.
251 ///
252 /// Not available under both `no_index` and `no_object`.
253 ///
254 /// # Deprecated
255 ///
256 /// This method is deprecated.
257 /// Use [`register_indexer_get`][Engine::register_indexer_get] instead.
258 ///
259 /// This method will be removed in the next major version.
260 #[deprecated(since = "1.9.1", note = "use `register_indexer_get` instead")]
261 #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
262 #[inline(always)]
263 pub fn register_indexer_get_result<
264 T: Variant + Clone,
265 IDX: Variant + Clone,
266 R: Variant + Clone,
267 const X: bool,
268 >(
269 &mut self,
270 get_fn: impl RhaiNativeFunc<(Mut<T>, IDX), 2, X, R, true> + SendSync + 'static,
271 ) -> &mut Self {
272 self.register_indexer_get(get_fn)
273 }
274 /// Register an index setter for a custom type with the [`Engine`].
275 ///
276 /// Not available under both `no_index` and `no_object`.
277 ///
278 /// # Deprecated
279 ///
280 /// This method is deprecated.
281 /// Use [`register_indexer_set`][Engine::register_indexer_set] instead.
282 ///
283 /// This method will be removed in the next major version.
284 #[deprecated(since = "1.9.1", note = "use `register_indexer_set` instead")]
285 #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
286 #[inline(always)]
287 pub fn register_indexer_set_result<
288 T: Variant + Clone,
289 IDX: Variant + Clone,
290 R: Variant + Clone,
291 const X: bool,
292 >(
293 &mut self,
294 set_fn: impl RhaiNativeFunc<(Mut<T>, IDX, R), 3, X, (), true> + SendSync + 'static,
295 ) -> &mut Self {
296 self.register_indexer_set(set_fn)
297 }
298 /// Register a custom syntax with the [`Engine`].
299 ///
300 /// Not available under `no_custom_syntax`.
301 ///
302 /// # Deprecated
303 ///
304 /// This method is deprecated.
305 /// Use [`register_custom_syntax_with_state_raw`][Engine::register_custom_syntax_with_state_raw] instead.
306 ///
307 /// This method will be removed in the next major version.
308 #[deprecated(
309 since = "1.11.0",
310 note = "use `register_custom_syntax_with_state_raw` instead"
311 )]
312 #[inline(always)]
313 #[cfg(not(feature = "no_custom_syntax"))]
314 pub fn register_custom_syntax_raw(
315 &mut self,
316 key: impl Into<Identifier>,
317 parse: impl Fn(&[ImmutableString], &str) -> crate::parser::ParseResult<Option<ImmutableString>>
318 + SendSync
319 + 'static,
320 scope_may_be_changed: bool,
321 func: impl Fn(&mut crate::EvalContext, &[crate::Expression]) -> RhaiResult + SendSync + 'static,
322 ) -> &mut Self {
323 self.register_custom_syntax_with_state_raw(
324 key,
325 move |keywords, look_ahead, _| parse(keywords, look_ahead),
326 scope_may_be_changed,
327 move |context, expressions, _| func(context, expressions),
328 )
329 }
330 /// _(internals)_ Evaluate a list of statements with no `this` pointer.
331 /// Exported under the `internals` feature only.
332 ///
333 /// # Deprecated
334 ///
335 /// This method is deprecated. It will be removed in the next major version.
336 #[cfg(feature = "internals")]
337 #[inline(always)]
338 #[deprecated(since = "1.12.0")]
339 pub fn eval_statements_raw(
340 &self,
341 global: &mut crate::eval::GlobalRuntimeState,
342 caches: &mut crate::eval::Caches,
343 scope: &mut Scope,
344 statements: &[crate::ast::Stmt],
345 ) -> RhaiResult {
346 self.eval_global_statements(global, caches, scope, statements, true)
347 }
348}
349
350impl Dynamic {
351 /// Convert the [`Dynamic`] into a [`String`] and return it.
352 /// If there are other references to the same string, a cloned copy is returned.
353 /// Returns the name of the actual type if the cast fails.
354 ///
355 /// # Deprecated
356 ///
357 /// This method is deprecated.
358 /// Use [`into_string`][Dynamic::into_string] instead.
359 ///
360 /// This method will be removed in the next major version.
361 #[deprecated(since = "1.1.0", note = "use `into_string` instead")]
362 #[inline(always)]
363 pub fn as_string(self) -> Result<String, &'static str> {
364 self.into_string()
365 }
366
367 /// Convert the [`Dynamic`] into an [`ImmutableString`] and return it.
368 /// Returns the name of the actual type if the cast fails.
369 ///
370 /// # Deprecated
371 ///
372 /// This method is deprecated.
373 /// Use [`into_immutable_string`][Dynamic::into_immutable_string] instead.
374 ///
375 /// This method will be removed in the next major version.
376 #[deprecated(since = "1.1.0", note = "use `into_immutable_string` instead")]
377 #[inline(always)]
378 pub fn as_immutable_string(self) -> Result<ImmutableString, &'static str> {
379 self.into_immutable_string()
380 }
381}
382
383impl AST {
384 /// _(internals)_ Get the internal [`Module`][crate::Module] containing all script-defined functions.
385 /// Exported under the `internals` feature only.
386 ///
387 /// Not available under `no_function`.
388 ///
389 /// # Deprecated
390 ///
391 /// This method is deprecated. Use [`shared_lib`][AST::shared_lib] instead.
392 ///
393 /// This method will be removed in the next major version.
394 #[deprecated(since = "1.3.0", note = "use `shared_lib` instead")]
395 #[cfg(feature = "internals")]
396 #[cfg(not(feature = "no_function"))]
397 #[inline(always)]
398 #[must_use]
399 pub fn lib(&self) -> &crate::Module {
400 self.shared_lib()
401 }
402 /// Clear the documentation.
403 /// Exported under the `metadata` feature only.
404 ///
405 /// # Deprecated
406 ///
407 /// This method will be removed in the next major version.
408 #[deprecated(since = "1.17.0")]
409 #[cfg(feature = "metadata")]
410 #[inline(always)]
411 pub fn clear_doc(&mut self) -> &mut Self {
412 self.doc.clear();
413 self
414 }
415}
416
417impl NativeCallContext<'_> {
418 /// Create a new [`NativeCallContext`].
419 ///
420 /// # Unimplemented
421 ///
422 /// This method is deprecated. It is no longer implemented and always panics.
423 ///
424 /// Use [`FnPtr::call`] to call a function pointer directly.
425 ///
426 /// This method will be removed in the next major version.
427 #[deprecated(
428 since = "1.3.0",
429 note = "use `FnPtr::call` to call a function pointer directly."
430 )]
431 #[inline(always)]
432 #[must_use]
433 #[allow(unused_variables)]
434 pub fn new(engine: &Engine, fn_name: &str, lib: &[SharedModule]) -> Self {
435 unimplemented!("`NativeCallContext::new` is deprecated");
436 }
437
438 /// Source of the current function.
439 ///
440 /// # Deprecated
441 ///
442 /// This method is deprecated.
443 /// Use [`fn_source`][NativeCallContext::fn_source] instead.
444 ///
445 /// To get the source of the caller, use [`call_source`][NativeCallContext::call_source].
446 ///
447 /// This method will be removed in the next major version.
448 #[inline(always)]
449 #[must_use]
450 #[deprecated(
451 since = "1.22.0",
452 note = "use `fn_source` instead, or `call_source` for the source of the caller."
453 )]
454 pub const fn source(&self) -> Option<&str> {
455 self.fn_source()
456 }
457
458 /// [Position] of the function call in the caller.
459 ///
460 /// # Deprecated
461 ///
462 /// This method is deprecated.
463 /// Use [`call_position`][NativeCallContext::call_position] instead.
464 ///
465 /// This method will be removed in the next major version.
466 #[inline(always)]
467 #[must_use]
468 #[deprecated(since = "1.22.0", note = "use `call_position` instead.")]
469 pub const fn position(&self) -> Position {
470 self.call_position()
471 }
472
473 /// Call a function inside the call context.
474 ///
475 /// # Deprecated
476 ///
477 /// This method is deprecated.
478 /// Use [`call_fn_raw`][NativeCallContext::call_fn_raw] instead.
479 ///
480 /// This method will be removed in the next major version.
481 #[deprecated(since = "1.2.0", note = "use `call_fn_raw` instead")]
482 #[inline(always)]
483 pub fn call_fn_dynamic_raw(
484 &self,
485 fn_name: impl AsRef<str>,
486 is_method_call: bool,
487 args: &mut [&mut Dynamic],
488 ) -> RhaiResult {
489 self.call_fn_raw(fn_name.as_ref(), is_method_call, is_method_call, args)
490 }
491}
492
493#[allow(useless_deprecated)]
494#[deprecated(since = "1.2.0", note = "explicitly wrap `EvalAltResult` in `Err`")]
495impl<T> From<EvalAltResult> for RhaiResultOf<T> {
496 #[inline(always)]
497 fn from(err: EvalAltResult) -> Self {
498 Err(err.into())
499 }
500}
501
502impl FnPtr {
503 /// Get the number of curried arguments.
504 ///
505 /// # Deprecated
506 ///
507 /// This method is deprecated.
508 /// Use [`curry().len()`][`FnPtr::curry`] instead.
509 ///
510 /// This method will be removed in the next major version.
511 #[deprecated(since = "1.8.0", note = "use `curry().len()` instead")]
512 #[inline(always)]
513 #[must_use]
514 pub fn num_curried(&self) -> usize {
515 self.curry().len()
516 }
517 /// Call the function pointer with curried arguments (if any).
518 /// The function may be script-defined (not available under `no_function`) or native Rust.
519 ///
520 /// This method is intended for calling a function pointer that is passed into a native Rust
521 /// function as an argument. Therefore, the [`AST`] is _NOT_ evaluated before calling the
522 /// function.
523 ///
524 /// # Deprecated
525 ///
526 /// This method is deprecated.
527 /// Use [`call_within_context`][FnPtr::call_within_context] or [`call_raw`][FnPtr::call_raw] instead.
528 ///
529 /// This method will be removed in the next major version.
530 #[deprecated(
531 since = "1.3.0",
532 note = "use `call_within_context` or `call_raw` instead"
533 )]
534 #[inline(always)]
535 pub fn call_dynamic(
536 &self,
537 context: &NativeCallContext,
538 this_ptr: Option<&mut Dynamic>,
539 arg_values: impl AsMut<[Dynamic]>,
540 ) -> RhaiResult {
541 self.call_raw(context, this_ptr, arg_values)
542 }
543}
544
545#[cfg(not(feature = "no_custom_syntax"))]
546impl crate::Expression<'_> {
547 /// If this expression is a variable name, return it. Otherwise [`None`].
548 ///
549 /// # Deprecated
550 ///
551 /// This method is deprecated.
552 /// Use [`get_string_value`][crate::Expression::get_string_value] instead.
553 ///
554 /// This method will be removed in the next major version.
555 #[deprecated(since = "1.4.0", note = "use `get_string_value` instead")]
556 #[inline(always)]
557 #[must_use]
558 pub fn get_variable_name(&self) -> Option<&str> {
559 self.get_string_value()
560 }
561}
562
563impl Position {
564 /// Create a new [`Position`].
565 ///
566 /// If `line` is zero, then [`None`] is returned.
567 ///
568 /// If `position` is zero, then it is at the beginning of a line.
569 ///
570 /// # Deprecated
571 ///
572 /// This function is deprecated.
573 /// Use [`new`][Position::new] (which panics when `line` is zero) instead.
574 ///
575 /// This method will be removed in the next major version.
576 #[deprecated(since = "1.6.0", note = "use `new` instead")]
577 #[inline(always)]
578 #[must_use]
579 pub const fn new_const(line: u16, position: u16) -> Option<Self> {
580 if line == 0 {
581 None
582 } else {
583 Some(Self::new(line, position))
584 }
585 }
586}
587
588#[allow(deprecated)]
589impl<T: Variant + Clone> TypeBuilder<'_, T> {
590 /// Register a custom fallible function.
591 ///
592 /// # Deprecated
593 ///
594 /// This method is deprecated.
595 /// Use [`with_fn`][`TypeBuilder::with_fn`] instead.
596 ///
597 /// This method will be removed in the next major version.
598 #[deprecated(since = "1.9.1", note = "use `with_fn` instead")]
599 #[inline(always)]
600 pub fn with_result_fn<S, A: 'static, const N: usize, const X: bool, R, FUNC>(
601 &mut self,
602 name: S,
603 method: FUNC,
604 ) -> &mut Self
605 where
606 S: AsRef<str> + Into<Identifier>,
607 R: Variant + Clone,
608 FUNC: RhaiNativeFunc<A, N, X, R, true> + SendSync + 'static,
609 {
610 self.with_fn(name, method)
611 }
612
613 /// Register a fallible getter function.
614 ///
615 /// The function signature must start with `&mut self` and not `&self`.
616 ///
617 /// Not available under `no_object`.
618 ///
619 /// # Deprecated
620 ///
621 /// This method is deprecated.
622 /// Use [`with_get`][`TypeBuilder::with_get`] instead.
623 ///
624 /// This method will be removed in the next major version.
625 #[deprecated(since = "1.9.1", note = "use `with_get` instead")]
626 #[cfg(not(feature = "no_object"))]
627 #[inline(always)]
628 pub fn with_get_result<const X: bool, R: Variant + Clone>(
629 &mut self,
630 name: impl AsRef<str>,
631 get_fn: impl RhaiNativeFunc<(Mut<T>,), 1, X, R, true> + SendSync + 'static,
632 ) -> &mut Self {
633 self.with_get(name, get_fn)
634 }
635
636 /// Register a fallible setter function.
637 ///
638 /// Not available under `no_object`.
639 ///
640 /// # Deprecated
641 ///
642 /// This method is deprecated.
643 /// Use [`with_set`][`TypeBuilder::with_set`] instead.
644 ///
645 /// This method will be removed in the next major version.
646 #[deprecated(since = "1.9.1", note = "use `with_set` instead")]
647 #[cfg(not(feature = "no_object"))]
648 #[inline(always)]
649 pub fn with_set_result<const X: bool, R: Variant + Clone>(
650 &mut self,
651 name: impl AsRef<str>,
652 set_fn: impl RhaiNativeFunc<(Mut<T>, R), 2, X, (), true> + SendSync + 'static,
653 ) -> &mut Self {
654 self.with_set(name, set_fn)
655 }
656
657 /// Register an fallible index getter.
658 ///
659 /// The function signature must start with `&mut self` and not `&self`.
660 ///
661 /// Not available under both `no_index` and `no_object`.
662 ///
663 /// # Deprecated
664 ///
665 /// This method is deprecated.
666 /// Use [`with_indexer_get`][`TypeBuilder::with_indexer_get`] instead.
667 ///
668 /// This method will be removed in the next major version.
669 #[deprecated(since = "1.9.1", note = "use `with_indexer_get` instead")]
670 #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
671 #[inline(always)]
672 pub fn with_indexer_get_result<IDX: Variant + Clone, R: Variant + Clone, const X: bool>(
673 &mut self,
674 get_fn: impl RhaiNativeFunc<(Mut<T>, IDX), 2, X, R, true> + SendSync + 'static,
675 ) -> &mut Self {
676 self.with_indexer_get(get_fn)
677 }
678
679 /// Register an fallible index setter.
680 ///
681 /// Not available under both `no_index` and `no_object`.
682 ///
683 /// # Deprecated
684 ///
685 /// This method is deprecated.
686 /// Use [`with_indexer_set`][`TypeBuilder::with_indexer_set`] instead.
687 ///
688 /// This method will be removed in the next major version.
689 #[deprecated(since = "1.9.1", note = "use `with_indexer_set` instead")]
690 #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
691 #[inline(always)]
692 pub fn with_indexer_set_result<IDX: Variant + Clone, R: Variant + Clone, const X: bool>(
693 &mut self,
694 set_fn: impl RhaiNativeFunc<(Mut<T>, IDX, R), 3, X, (), true> + SendSync + 'static,
695 ) -> &mut Self {
696 self.with_indexer_set(set_fn)
697 }
698}
699
700impl Module {
701 /// Create a new [`Module`] with a pre-sized capacity for functions.
702 ///
703 /// # Deprecated
704 ///
705 /// This method is deprecated.
706 /// Use [`new`][`Module::new`] instead.
707 ///
708 /// This method will be removed in the next major version.
709 #[inline(always)]
710 #[must_use]
711 #[deprecated(since = "1.12.0", note = "use `new` instead")]
712 pub const fn with_capacity(_capacity: usize) -> Self {
713 Self::new()
714 }
715
716 /// Get the display name of a registered custom type.
717 ///
718 /// # Deprecated
719 ///
720 /// This method is deprecated.
721 /// Use [`get_custom_type_display_by_name`][`Module::get_custom_type_display_by_name`] instead.
722 ///
723 /// This method will be removed in the next major version.
724 #[inline(always)]
725 #[must_use]
726 #[deprecated(
727 since = "1.16.0",
728 note = "use `get_custom_type_display_by_name` instead"
729 )]
730 pub fn get_custom_type(&self, type_name: &str) -> Option<&str> {
731 self.get_custom_type_display_by_name(type_name)
732 }
733
734 /// Set a native Rust function into the [`Module`], returning a [`u64`] hash key.
735 ///
736 /// If there is an existing Rust function of the same hash, it is replaced.
737 ///
738 /// # Deprecated
739 ///
740 /// This method is deprecated.
741 /// Use the [`FuncRegistration`] API instead.
742 ///
743 /// This method will be removed in the next major version.
744 #[deprecated(since = "1.17.0", note = "use the `FuncRegistration` API instead")]
745 #[inline(always)]
746 pub fn set_fn(
747 &mut self,
748 name: impl Into<Identifier>,
749 namespace: FnNamespace,
750 _access: FnAccess,
751 arg_names: Option<&[&str]>,
752 arg_types: impl AsRef<[TypeId]>,
753 func: RhaiFunc,
754 ) -> u64 {
755 let _arg_names = arg_names;
756
757 let fx = FuncRegistration::new(name).with_namespace(namespace);
758
759 #[cfg(feature = "metadata")]
760 let fx = if let Some(arg_names) = _arg_names {
761 fx.with_params_info(arg_names)
762 } else {
763 fx
764 };
765
766 fx.set_into_module_raw(self, arg_types, func).hash
767 }
768
769 /// _(metadata)_ Set a native Rust function into the [`Module`], returning a [`u64`] hash key.
770 /// Exported under the `metadata` feature only.
771 ///
772 /// If there is an existing Rust function of the same hash, it is replaced.
773 ///
774 /// # Deprecated
775 ///
776 /// This method is deprecated.
777 /// Use the [`FuncRegistration`] API instead.
778 ///
779 /// This method will be removed in the next major version.
780 #[deprecated(since = "1.17.0", note = "use the `FuncRegistration` API instead")]
781 #[cfg(feature = "metadata")]
782 #[inline(always)]
783 pub fn set_fn_with_comments<C: AsRef<str>>(
784 &mut self,
785 name: impl Into<Identifier>,
786 namespace: FnNamespace,
787 _access: FnAccess,
788 arg_names: Option<&[&str]>,
789 arg_types: impl AsRef<[TypeId]>,
790 comments: impl IntoIterator<Item = C>,
791 func: RhaiFunc,
792 ) -> u64 {
793 FuncRegistration::new(name)
794 .with_namespace(namespace)
795 .with_params_info(arg_names.unwrap_or(&[]))
796 .with_comments(comments)
797 .set_into_module_raw(self, arg_types, func)
798 .hash
799 }
800
801 /// _(metadata)_ Update the metadata (parameter names/types and return type) of a registered function.
802 /// Exported under the `metadata` feature only.
803 ///
804 /// The [`u64`] hash is returned by the [`set_native_fn`][Module::set_native_fn] call.
805 ///
806 /// # Deprecated
807 ///
808 /// This method is deprecated.
809 /// Use [`update_fn_metadata_with_comments`][`Module::update_fn_metadata_with_comments`] instead.
810 ///
811 /// This method will be removed in the next major version.
812 #[deprecated(
813 since = "1.17.0",
814 note = "use `update_fn_metadata_with_comments` instead"
815 )]
816 #[cfg(feature = "metadata")]
817 #[inline(always)]
818 #[allow(deprecated)]
819 pub fn update_fn_metadata<S: Into<Identifier>>(
820 &mut self,
821 hash_fn: u64,
822 arg_names: impl IntoIterator<Item = S>,
823 ) -> &mut Self {
824 self.update_fn_metadata_with_comments(hash_fn, arg_names, [""; 0])
825 }
826
827 /// _(metadata)_ Generate signatures for all the non-private functions in the [`Module`].
828 /// Exported under the `metadata` feature only.
829 ///
830 /// # Deprecated
831 ///
832 /// This method is deprecated.
833 /// Use [`gen_fn_signatures_with_mapper`][`Module::gen_fn_signatures_with_mapper`] instead.
834 ///
835 /// This method will be removed in the next major version.
836 #[deprecated(since = "1.17.0", note = "use `gen_fn_signatures_with_mapper` instead")]
837 #[cfg(feature = "metadata")]
838 #[inline(always)]
839 pub fn gen_fn_signatures(&self) -> impl Iterator<Item = String> + '_ {
840 self.gen_fn_signatures_with_mapper(Into::into)
841 }
842}
843
844#[cfg(not(feature = "no_index"))]
845use crate::plugin::*;
846
847#[cfg(not(feature = "no_index"))]
848#[export_module]
849pub mod deprecated_array_functions {
850 use crate::packages::array_basic::array_functions::*;
851 use crate::{Array, INT};
852
853 /// Iterate through all the elements in the array, applying a function named by `mapper` to each
854 /// element in turn, and return the results as a new array.
855 ///
856 /// # Deprecated API
857 ///
858 /// This method is deprecated and will be removed from the next major version.
859 /// Use `array.map(Fn("fn_name"))` instead.
860 ///
861 /// # Function Parameters
862 ///
863 /// A function with the same name as the value of `mapper` must exist taking these parameters:
864 ///
865 /// * `element`: copy of array element
866 /// * `index` _(optional)_: current index in the array
867 ///
868 /// # Example
869 ///
870 /// ```rhai
871 /// fn square(x) { x * x }
872 ///
873 /// fn multiply(x, i) { x * i }
874 ///
875 /// let x = [1, 2, 3, 4, 5];
876 ///
877 /// let y = x.map("square");
878 ///
879 /// print(y); // prints "[1, 4, 9, 16, 25]"
880 ///
881 /// let y = x.map("multiply");
882 ///
883 /// print(y); // prints "[0, 2, 6, 12, 20]"
884 /// ```
885 #[rhai_fn(name = "map", return_raw)]
886 pub fn map_by_fn_name(
887 ctx: NativeCallContext,
888 array: &mut Array,
889 mapper: &str,
890 ) -> RhaiResultOf<Array> {
891 map(ctx, array, FnPtr::new(mapper)?)
892 }
893 /// Iterate through all the elements in the array, applying a function named by `filter` to each
894 /// element in turn, and return a copy of all elements (in order) that return `true` as a new array.
895 ///
896 /// # Deprecated API
897 ///
898 /// This method is deprecated and will be removed from the next major version.
899 /// Use `array.filter(Fn("fn_name"))` instead.
900 ///
901 /// # Function Parameters
902 ///
903 /// A function with the same name as the value of `filter` must exist taking these parameters:
904 ///
905 /// * `element`: copy of array element
906 /// * `index` _(optional)_: current index in the array
907 ///
908 /// # Example
909 ///
910 /// ```rhai
911 /// fn screen(x, i) { x * i >= 10 }
912 ///
913 /// let x = [1, 2, 3, 4, 5];
914 ///
915 /// let y = x.filter("is_odd");
916 ///
917 /// print(y); // prints "[1, 3, 5]"
918 ///
919 /// let y = x.filter("screen");
920 ///
921 /// print(y); // prints "[12, 20]"
922 /// ```
923 #[rhai_fn(name = "filter", return_raw)]
924 pub fn filter_by_fn_name(
925 ctx: NativeCallContext,
926 array: &mut Array,
927 filter_func: &str,
928 ) -> RhaiResultOf<Array> {
929 filter(ctx, array, FnPtr::new(filter_func)?)
930 }
931 /// Iterate through all the elements in the array, applying a function named by `filter` to each
932 /// element in turn, and return the index of the first element that returns `true`.
933 /// If no element returns `true`, `-1` is returned.
934 ///
935 /// # Deprecated API
936 ///
937 /// This method is deprecated and will be removed from the next major version.
938 /// Use `array.index_of(Fn("fn_name"))` instead.
939 ///
940 /// # Function Parameters
941 ///
942 /// A function with the same name as the value of `filter` must exist taking these parameters:
943 ///
944 /// * `element`: copy of array element
945 /// * `index` _(optional)_: current index in the array
946 ///
947 /// # Example
948 ///
949 /// ```rhai
950 /// fn is_special(x) { x > 3 }
951 ///
952 /// fn is_dumb(x) { x > 8 }
953 ///
954 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
955 ///
956 /// print(x.index_of("is_special")); // prints 3
957 ///
958 /// print(x.index_of("is_dumb")); // prints -1
959 /// ```
960 #[rhai_fn(name = "index_of", return_raw, pure)]
961 pub fn index_of_by_fn_name(
962 ctx: NativeCallContext,
963 array: &mut Array,
964 filter: &str,
965 ) -> RhaiResultOf<INT> {
966 index_of_filter(ctx, array, FnPtr::new(filter)?)
967 }
968 /// Iterate through all the elements in the array, starting from a particular `start` position,
969 /// applying a function named by `filter` to each element in turn, and return the index of the
970 /// first element that returns `true`. If no element returns `true`, `-1` is returned.
971 ///
972 /// * If `start` < 0, position counts from the end of the array (`-1` is the last element).
973 /// * If `start` < -length of array, position counts from the beginning of the array.
974 /// * If `start` ≥ length of array, `-1` is returned.
975 ///
976 /// # Deprecated API
977 ///
978 /// This method is deprecated and will be removed from the next major version.
979 /// Use `array.index_of(Fn("fn_name"), start)` instead.
980 ///
981 /// # Function Parameters
982 ///
983 /// A function with the same name as the value of `filter` must exist taking these parameters:
984 ///
985 /// * `element`: copy of array element
986 /// * `index` _(optional)_: current index in the array
987 ///
988 /// # Example
989 ///
990 /// ```rhai
991 /// fn plural(x) { x > 1 }
992 ///
993 /// fn singular(x) { x < 2 }
994 ///
995 /// fn screen(x, i) { x * i > 20 }
996 ///
997 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
998 ///
999 /// print(x.index_of("plural", 3)); // prints 5: 2 > 1
1000 ///
1001 /// print(x.index_of("singular", 9)); // prints -1: nothing < 2 past index 9
1002 ///
1003 /// print(x.index_of("plural", 15)); // prints -1: nothing found past end of array
1004 ///
1005 /// print(x.index_of("plural", -5)); // prints 9: -5 = start from index 8
1006 ///
1007 /// print(x.index_of("plural", -99)); // prints 1: -99 = start from beginning
1008 ///
1009 /// print(x.index_of("screen", 8)); // prints 10: 3 * 10 > 20
1010 /// ```
1011 #[rhai_fn(name = "index_of", return_raw, pure)]
1012 pub fn index_of_by_fn_name_starting_from(
1013 ctx: NativeCallContext,
1014 array: &mut Array,
1015 filter: &str,
1016 start: INT,
1017 ) -> RhaiResultOf<INT> {
1018 index_of_filter_starting_from(ctx, array, FnPtr::new(filter)?, start)
1019 }
1020 /// Return `true` if any element in the array that returns `true` when applied a function named
1021 /// by `filter`.
1022 ///
1023 /// # Deprecated API
1024 ///
1025 /// This method is deprecated and will be removed from the next major version.
1026 /// Use `array.some(Fn("fn_name"))` instead.
1027 ///
1028 /// # Function Parameters
1029 ///
1030 /// A function with the same name as the value of `filter` must exist taking these parameters:
1031 ///
1032 /// * `element`: copy of array element
1033 /// * `index` _(optional)_: current index in the array
1034 ///
1035 /// # Example
1036 ///
1037 /// ```rhai
1038 /// fn large(x) { x > 3 }
1039 ///
1040 /// fn huge(x) { x > 10 }
1041 ///
1042 /// fn screen(x, i) { i > x }
1043 ///
1044 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
1045 ///
1046 /// print(x.some("large")); // prints true
1047 ///
1048 /// print(x.some("huge")); // prints false
1049 ///
1050 /// print(x.some("screen")); // prints true
1051 /// ```
1052 #[rhai_fn(name = "some", return_raw, pure)]
1053 pub fn some_by_fn_name(
1054 ctx: NativeCallContext,
1055 array: &mut Array,
1056 filter: &str,
1057 ) -> RhaiResultOf<bool> {
1058 some(ctx, array, FnPtr::new(filter)?)
1059 }
1060 /// Return `true` if all elements in the array return `true` when applied a function named by `filter`.
1061 ///
1062 /// # Deprecated API
1063 ///
1064 /// This method is deprecated and will be removed from the next major version.
1065 /// Use `array.all(Fn("fn_name"))` instead.
1066 ///
1067 /// # Function Parameters
1068 ///
1069 /// A function with the same name as the value of `filter` must exist taking these parameters:
1070 ///
1071 /// * `element`: copy of array element
1072 /// * `index` _(optional)_: current index in the array
1073 ///
1074 /// # Example
1075 ///
1076 /// ```rhai
1077 /// let x = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5];
1078 ///
1079 /// print(x.all(|v| v > 3)); // prints false
1080 ///
1081 /// print(x.all(|v| v > 1)); // prints true
1082 ///
1083 /// print(x.all(|v, i| i > v)); // prints false
1084 /// ```
1085 #[rhai_fn(name = "all", return_raw, pure)]
1086 pub fn all_by_fn_name(
1087 ctx: NativeCallContext,
1088 array: &mut Array,
1089 filter: &str,
1090 ) -> RhaiResultOf<bool> {
1091 all(ctx, array, FnPtr::new(filter)?)
1092 }
1093 /// Remove duplicated _consecutive_ elements from the array that return `true` when applied a
1094 /// function named by `comparer`.
1095 ///
1096 /// # Deprecated API
1097 ///
1098 /// This method is deprecated and will be removed from the next major version.
1099 /// Use `array.dedup(Fn("fn_name"))` instead.
1100 ///
1101 /// No element is removed if the correct `comparer` function does not exist.
1102 ///
1103 /// # Function Parameters
1104 ///
1105 /// * `element1`: copy of the current array element to compare
1106 /// * `element2`: copy of the next array element to compare
1107 ///
1108 /// ## Return Value
1109 ///
1110 /// `true` if `element1 == element2`, otherwise `false`.
1111 ///
1112 /// # Example
1113 ///
1114 /// ```rhai
1115 /// fn declining(a, b) { a >= b }
1116 ///
1117 /// let x = [1, 2, 2, 2, 3, 1, 2, 3, 4, 3, 3, 2, 1];
1118 ///
1119 /// x.dedup("declining");
1120 ///
1121 /// print(x); // prints "[1, 2, 3, 4]"
1122 /// ```
1123 #[rhai_fn(name = "dedup", return_raw)]
1124 pub fn dedup_by_fn_name(
1125 ctx: NativeCallContext,
1126 array: &mut Array,
1127 comparer: &str,
1128 ) -> RhaiResultOf<()> {
1129 dedup_by_comparer(ctx, array, FnPtr::new(comparer)?);
1130 Ok(())
1131 }
1132 /// Reduce an array by iterating through all elements while applying a function named by `reducer`.
1133 ///
1134 /// # Deprecated API
1135 ///
1136 /// This method is deprecated and will be removed from the next major version.
1137 /// Use `array.reduce(Fn("fn_name"))` instead.
1138 ///
1139 /// # Function Parameters
1140 ///
1141 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1142 ///
1143 /// * `result`: accumulated result, initially `()`
1144 /// * `element`: copy of array element
1145 /// * `index` _(optional)_: current index in the array
1146 ///
1147 /// # Example
1148 ///
1149 /// ```rhai
1150 /// fn process(r, x) {
1151 /// x + (r ?? 0)
1152 /// }
1153 /// fn process_extra(r, x, i) {
1154 /// x + i + (r ?? 0)
1155 /// }
1156 ///
1157 /// let x = [1, 2, 3, 4, 5];
1158 ///
1159 /// let y = x.reduce("process");
1160 ///
1161 /// print(y); // prints 15
1162 ///
1163 /// let y = x.reduce("process_extra");
1164 ///
1165 /// print(y); // prints 25
1166 /// ```
1167 #[rhai_fn(name = "reduce", return_raw, pure)]
1168 pub fn reduce_by_fn_name(
1169 ctx: NativeCallContext,
1170 array: &mut Array,
1171 reducer: &str,
1172 ) -> RhaiResult {
1173 reduce(ctx, array, FnPtr::new(reducer)?)
1174 }
1175 /// Reduce an array by iterating through all elements while applying a function named by `reducer`.
1176 ///
1177 /// # Deprecated API
1178 ///
1179 /// This method is deprecated and will be removed from the next major version.
1180 /// Use `array.reduce(Fn("fn_name"), initial)` instead.
1181 ///
1182 /// # Function Parameters
1183 ///
1184 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1185 ///
1186 /// * `result`: accumulated result, starting with the value of `initial`
1187 /// * `element`: copy of array element
1188 /// * `index` _(optional)_: current index in the array
1189 ///
1190 /// # Example
1191 ///
1192 /// ```rhai
1193 /// fn process(r, x) { x + r }
1194 ///
1195 /// fn process_extra(r, x, i) { x + i + r }
1196 ///
1197 /// let x = [1, 2, 3, 4, 5];
1198 ///
1199 /// let y = x.reduce("process", 5);
1200 ///
1201 /// print(y); // prints 20
1202 ///
1203 /// let y = x.reduce("process_extra", 5);
1204 ///
1205 /// print(y); // prints 30
1206 /// ```
1207 #[rhai_fn(name = "reduce", return_raw, pure)]
1208 pub fn reduce_by_fn_name_with_initial(
1209 ctx: NativeCallContext,
1210 array: &mut Array,
1211 reducer: &str,
1212 initial: Dynamic,
1213 ) -> RhaiResult {
1214 reduce_with_initial(ctx, array, FnPtr::new(reducer)?, initial)
1215 }
1216 /// Reduce an array by iterating through all elements, in _reverse_ order,
1217 /// while applying a function named by `reducer`.
1218 ///
1219 /// # Deprecated API
1220 ///
1221 /// This method is deprecated and will be removed from the next major version.
1222 /// Use `array.reduce_rev(Fn("fn_name"))` instead.
1223 ///
1224 /// # Function Parameters
1225 ///
1226 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1227 ///
1228 /// * `result`: accumulated result, initially `()`
1229 /// * `element`: copy of array element
1230 /// * `index` _(optional)_: current index in the array
1231 ///
1232 /// # Example
1233 ///
1234 /// ```rhai
1235 /// fn process(r, x) {
1236 /// x + (r ?? 0)
1237 /// }
1238 /// fn process_extra(r, x, i) {
1239 /// x + i + (r ?? 0)
1240 /// }
1241 ///
1242 /// let x = [1, 2, 3, 4, 5];
1243 ///
1244 /// let y = x.reduce_rev("process");
1245 ///
1246 /// print(y); // prints 15
1247 ///
1248 /// let y = x.reduce_rev("process_extra");
1249 ///
1250 /// print(y); // prints 25
1251 /// ```
1252 #[rhai_fn(name = "reduce_rev", return_raw, pure)]
1253 pub fn reduce_rev_by_fn_name(
1254 ctx: NativeCallContext,
1255 array: &mut Array,
1256 reducer: &str,
1257 ) -> RhaiResult {
1258 reduce_rev(ctx, array, FnPtr::new(reducer)?)
1259 }
1260 /// Reduce an array by iterating through all elements, in _reverse_ order,
1261 /// while applying a function named by `reducer`.
1262 ///
1263 /// # Deprecated API
1264 ///
1265 /// This method is deprecated and will be removed from the next major version.
1266 /// Use `array.reduce_rev(Fn("fn_name"), initial)` instead.
1267 ///
1268 /// # Function Parameters
1269 ///
1270 /// A function with the same name as the value of `reducer` must exist taking these parameters:
1271 ///
1272 /// * `result`: accumulated result, starting with the value of `initial`
1273 /// * `element`: copy of array element
1274 /// * `index` _(optional)_: current index in the array
1275 ///
1276 /// # Example
1277 ///
1278 /// ```rhai
1279 /// fn process(r, x) { x + r }
1280 ///
1281 /// fn process_extra(r, x, i) { x + i + r }
1282 ///
1283 /// let x = [1, 2, 3, 4, 5];
1284 ///
1285 /// let y = x.reduce_rev("process", 5);
1286 ///
1287 /// print(y); // prints 20
1288 ///
1289 /// let y = x.reduce_rev("process_extra", 5);
1290 ///
1291 /// print(y); // prints 30
1292 /// ```
1293 #[rhai_fn(name = "reduce_rev", return_raw, pure)]
1294 pub fn reduce_rev_by_fn_name_with_initial(
1295 ctx: NativeCallContext,
1296 array: &mut Array,
1297 reducer: &str,
1298 initial: Dynamic,
1299 ) -> RhaiResult {
1300 reduce_rev_with_initial(ctx, array, FnPtr::new(reducer)?, initial)
1301 }
1302 /// Sort the array based on applying a function named by `comparer`.
1303 ///
1304 /// # Deprecated API
1305 ///
1306 /// This method is deprecated and will be removed from the next major version.
1307 /// Use `array.sort(Fn("fn_name"))` instead.
1308 ///
1309 /// # Function Parameters
1310 ///
1311 /// A function with the same name as the value of `comparer` must exist taking these parameters:
1312 ///
1313 /// * `element1`: copy of the current array element to compare
1314 /// * `element2`: copy of the next array element to compare
1315 ///
1316 /// ## Return Value
1317 ///
1318 /// * Any integer > 0 if `element1 > element2`
1319 /// * Zero if `element1 == element2`
1320 /// * Any integer < 0 if `element1 < element2`
1321 ///
1322 /// # Example
1323 ///
1324 /// ```rhai
1325 /// fn reverse(a, b) {
1326 /// if a > b {
1327 /// -1
1328 /// } else if a < b {
1329 /// 1
1330 /// } else {
1331 /// 0
1332 /// }
1333 /// }
1334 /// let x = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10];
1335 ///
1336 /// x.sort("reverse");
1337 ///
1338 /// print(x); // prints "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
1339 /// ```
1340 #[rhai_fn(name = "sort", return_raw)]
1341 pub fn sort_by_fn_name(
1342 ctx: NativeCallContext,
1343 array: &mut Array,
1344 comparer: &str,
1345 ) -> RhaiResultOf<()> {
1346 sort(ctx, array, FnPtr::new(comparer)?);
1347 Ok(())
1348 }
1349 /// Remove all elements in the array that returns `true` when applied a function named by `filter`
1350 /// and return them as a new array.
1351 ///
1352 /// # Deprecated API
1353 ///
1354 /// This method is deprecated and will be removed from the next major version.
1355 /// Use `array.drain(Fn("fn_name"))` instead.
1356 ///
1357 /// # Function Parameters
1358 ///
1359 /// A function with the same name as the value of `filter` must exist taking these parameters:
1360 ///
1361 /// * `element`: copy of array element
1362 /// * `index` _(optional)_: current index in the array
1363 ///
1364 /// # Example
1365 ///
1366 /// ```rhai
1367 /// fn small(x) { x < 3 }
1368 ///
1369 /// fn screen(x, i) { x + i > 5 }
1370 ///
1371 /// let x = [1, 2, 3, 4, 5];
1372 ///
1373 /// let y = x.drain("small");
1374 ///
1375 /// print(x); // prints "[3, 4, 5]"
1376 ///
1377 /// print(y); // prints "[1, 2]"
1378 ///
1379 /// let z = x.drain("screen");
1380 ///
1381 /// print(x); // prints "[3, 4]"
1382 ///
1383 /// print(z); // prints "[5]"
1384 /// ```
1385 #[rhai_fn(name = "drain", return_raw)]
1386 pub fn drain_by_fn_name(
1387 ctx: NativeCallContext,
1388 array: &mut Array,
1389 filter: &str,
1390 ) -> RhaiResultOf<Array> {
1391 drain(ctx, array, FnPtr::new(filter)?)
1392 }
1393 /// Remove all elements in the array that do not return `true` when applied a function named by
1394 /// `filter` and return them as a new array.
1395 ///
1396 /// # Deprecated API
1397 ///
1398 /// This method is deprecated and will be removed from the next major version.
1399 /// Use `array.retain(Fn("fn_name"))` instead.
1400 ///
1401 /// # Function Parameters
1402 ///
1403 /// A function with the same name as the value of `filter` must exist taking these parameters:
1404 ///
1405 /// * `element`: copy of array element
1406 /// * `index` _(optional)_: current index in the array
1407 ///
1408 /// # Example
1409 ///
1410 /// ```rhai
1411 /// fn large(x) { x >= 3 }
1412 ///
1413 /// fn screen(x, i) { x + i <= 5 }
1414 ///
1415 /// let x = [1, 2, 3, 4, 5];
1416 ///
1417 /// let y = x.retain("large");
1418 ///
1419 /// print(x); // prints "[3, 4, 5]"
1420 ///
1421 /// print(y); // prints "[1, 2]"
1422 ///
1423 /// let z = x.retain("screen");
1424 ///
1425 /// print(x); // prints "[3, 4]"
1426 ///
1427 /// print(z); // prints "[5]"
1428 /// ```
1429 #[rhai_fn(name = "retain", return_raw)]
1430 pub fn retain_by_fn_name(
1431 ctx: NativeCallContext,
1432 array: &mut Array,
1433 filter: &str,
1434 ) -> RhaiResultOf<Array> {
1435 retain(ctx, array, FnPtr::new(filter)?)
1436 }
1437}
1438
1439pub mod config {
1440 pub mod hashing {
1441 /// Set the hashing seed. This is used to hash functions etc.
1442 ///
1443 /// # Deprecated
1444 ///
1445 /// This method is deprecated.
1446 /// Use [`set_hashing_seed`][crate::config::hashing::set_hashing_seed] instead.
1447 ///
1448 /// This method will be removed in the next major version.
1449 #[deprecated(since = "1.17.0", note = "use `set_hashing_seed` instead")]
1450 #[inline(always)]
1451 pub fn set_ahash_seed(new_seed: Option<[u64; 4]>) -> Result<(), Option<[u64; 4]>> {
1452 crate::config::hashing::set_hashing_seed(new_seed)
1453 }
1454
1455 /// Get the current hashing Seed.
1456 ///
1457 /// # Deprecated
1458 ///
1459 /// This method is deprecated.
1460 /// Use [`get_hashing_seed`][crate::config::hashing::get_hashing_seed] instead.
1461 ///
1462 /// This method will be removed in the next major version.
1463 #[deprecated(since = "1.17.0", note = "use `get_hashing_seed` instead")]
1464 #[inline]
1465 #[must_use]
1466 pub fn get_ahash_seed() -> &'static Option<[u64; 4]> {
1467 crate::config::hashing::get_hashing_seed()
1468 }
1469 }
1470}