objc2_gameplay_kit/generated/
GKRandomSource.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6use objc2_foundation::*;
7
8use crate::*;
9
10extern_protocol!(
11    /// A protocol for random sources that can generate random numbers. This is the minimal interface needed
12    /// to consume random values from a source; concrete subclasses should be used for configuring the
13    /// production of random values.
14    /// The availability of deterministic random sources is critical to creating reliable gameplay mechanics.
15    /// Ensure that systems that should not influence each other have unique random sources and avoid sharing
16    /// sources unless absolutely needed.
17    ///
18    /// This protocol allows you to provide custom random sources from classes that are not able to
19    /// derive from GKRandomSource directly.
20    ///
21    /// See also [Apple's documentation](https://developer.apple.com/documentation/gameplaykit/gkrandom?language=objc)
22    pub unsafe trait GKRandom {
23        /// Returns the next integer in the random sequence and moves ahead to the next one.
24        /// The value is in the range of [INT32_MIN, INT32_MAX].
25        /// The lower bits are not guaranteed to be random so please use another
26        /// property for simple choices.
27        ///
28        ///
29        /// See: nextBool
30        ///
31        /// See: nextUniform
32        #[unsafe(method(nextInt))]
33        #[unsafe(method_family = none)]
34        unsafe fn nextInt(&self) -> NSInteger;
35
36        /// Returns the next unsigned value in the random sequence that is less than upperBound.
37        /// The value is in the range of [0, upperBound). Thus the value never equals or exceeeds upperBound.
38        /// The unsigned nature and upper bound allows implementations to use logical shifts to return a
39        /// value whose lower bits are more random than a similar call to nextInt.
40        ///
41        /// This is used to implement nextBool and nextUniform by default.
42        #[unsafe(method(nextIntWithUpperBound:))]
43        #[unsafe(method_family = none)]
44        unsafe fn nextIntWithUpperBound(&self, upper_bound: NSUInteger) -> NSUInteger;
45
46        /// Returns the next uniform float in the random sequence and moves ahead to the next one.
47        /// The value is in the range of [0.0, 1.0].
48        /// There is no weighting across the range so remapping this with a curve may give the best
49        /// sampling distribution for your algorithm.
50        ///
51        /// By default this should be based on nextIntWithUpperBound:. Implementions may base it on
52        /// another representation if needed.
53        ///
54        ///
55        /// See: nextIntWithUpperBound:
56        ///
57        /// See: nextInt
58        #[unsafe(method(nextUniform))]
59        #[unsafe(method_family = none)]
60        unsafe fn nextUniform(&self) -> c_float;
61
62        /// Returns the next true or false value in the random sequence and moves ahead to the next one.
63        /// The value is either nonzero (true) or zero (false).
64        /// Use this for simple boolean switches in logic that don't require fuzzy evaluation.
65        /// For fuzzy evaluation use nextUniform.
66        ///
67        /// By default this should be based on nextIntWithUpperBound:. Implementations may base it on
68        /// another representation if needed.
69        ///
70        ///
71        /// See: nextIntWithUpperBound:
72        ///
73        /// See: nextUniform
74        #[unsafe(method(nextBool))]
75        #[unsafe(method_family = none)]
76        unsafe fn nextBool(&self) -> bool;
77    }
78);
79
80extern_class!(
81    /// A concrete random source that can generate random numbers. The implementation details are up to the system and
82    /// if a particular algorithm is needed then use one of the provided subclasses.
83    ///
84    /// For certain specialized applications a shared system source may be needed and for those instances there is
85    /// a wrapped interface over arc4random_*, accessible via +[GKRandomSource sharedRandom].
86    ///
87    ///
88    /// See: GKARC4RandomSource
89    ///
90    /// See: GKLinearCongruentialRandomSource
91    ///
92    /// See: GKMersenneTwisterRandomSource
93    ///
94    /// See: GKRandomSource.systemRandom
95    ///
96    /// See also [Apple's documentation](https://developer.apple.com/documentation/gameplaykit/gkrandomsource?language=objc)
97    #[unsafe(super(NSObject))]
98    #[derive(Debug, PartialEq, Eq, Hash)]
99    pub struct GKRandomSource;
100);
101
102extern_conformance!(
103    unsafe impl GKRandom for GKRandomSource {}
104);
105
106extern_conformance!(
107    unsafe impl NSCoding for GKRandomSource {}
108);
109
110extern_conformance!(
111    unsafe impl NSCopying for GKRandomSource {}
112);
113
114unsafe impl CopyingHelper for GKRandomSource {
115    type Result = Self;
116}
117
118extern_conformance!(
119    unsafe impl NSObjectProtocol for GKRandomSource {}
120);
121
122extern_conformance!(
123    unsafe impl NSSecureCoding for GKRandomSource {}
124);
125
126impl GKRandomSource {
127    extern_methods!(
128        /// Creates a new random source initialized using bits from an entropy source like SecRandomCopyBytes.
129        /// When used directly from the base class; this source is deterministic and performant but the underlying implementation
130        /// details are not specified. Use a subclass with a specific algorithm implementation guaranteed if your application requires
131        /// very stringent random source charateristics.
132        ///
133        ///
134        /// See: GKARC4RandomSource
135        ///
136        /// See: GKLinearCongruentialRandomSource
137        ///
138        /// See: GKMersenneTwisterRandomSource
139        #[unsafe(method(init))]
140        #[unsafe(method_family = init)]
141        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
142
143        /// Deserializes a random source from an NSCoder. All random sources support coding for serializing and deserializing the state
144        /// of the random source. Each subclass has its own contract for what parts of the state is preserved when serialized but the
145        /// general contract is that a serialized source must generate the same sequence of values as the original source would from the
146        /// instant it was serialized.
147        ///
148        /// Note that the sharedRandom instance is an exception as it is explicitly seedless and a shared singleton instance.
149        /// When serialized and deserialized it will return the current sharedRandom instance instead.
150        ///
151        /// # Safety
152        ///
153        /// `a_decoder` possibly has further requirements.
154        #[unsafe(method(initWithCoder:))]
155        #[unsafe(method_family = init)]
156        pub unsafe fn initWithCoder(this: Allocated<Self>, a_decoder: &NSCoder) -> Retained<Self>;
157
158        /// Returns a shared instance of a random source that uses the system's underlying random source.
159        /// Using this instance modifies the outcome of future calls to the arc4random family of C calls. It is
160        /// also affected by calls to the C apis and should not be used for sources that are intended to
161        /// be deterministic.
162        ///
163        ///
164        /// Note that while it may seem semantically similar to a GKARC4RandomSource, this is not a drop in replacement.
165        #[unsafe(method(sharedRandom))]
166        #[unsafe(method_family = none)]
167        pub unsafe fn sharedRandom() -> Retained<GKRandomSource>;
168
169        /// Returns a shuffled instance of the given array. The objects in the array are shuffled based on a Fisher-Yates shuffle.
170        ///
171        /// Any random, be it custom, source or a distribution, that can provide a number with an upper bound of at least the
172        /// array.count is suitable for this shuffle.
173        ///
174        /// # Safety
175        ///
176        /// `array` generic should be of the correct type.
177        #[unsafe(method(arrayByShufflingObjectsInArray:))]
178        #[unsafe(method_family = none)]
179        pub unsafe fn arrayByShufflingObjectsInArray(&self, array: &NSArray) -> Retained<NSArray>;
180    );
181}
182
183/// Methods declared on superclass `NSObject`.
184impl GKRandomSource {
185    extern_methods!(
186        #[unsafe(method(new))]
187        #[unsafe(method_family = new)]
188        pub unsafe fn new() -> Retained<Self>;
189    );
190}
191
192mod private_NSArrayGameplayKit {
193    pub trait Sealed {}
194}
195
196/// Category "GameplayKit" on [`NSArray`].
197#[doc(alias = "GameplayKit")]
198pub unsafe trait NSArrayGameplayKit<ObjectType: Message>:
199    ClassType + Sized + private_NSArrayGameplayKit::Sealed
200{
201    extern_methods!(
202        #[unsafe(method(shuffledArrayWithRandomSource:))]
203        #[unsafe(method_family = none)]
204        unsafe fn shuffledArrayWithRandomSource(
205            &self,
206            random_source: &GKRandomSource,
207        ) -> Retained<NSArray<ObjectType>>;
208
209        #[unsafe(method(shuffledArray))]
210        #[unsafe(method_family = none)]
211        unsafe fn shuffledArray(&self) -> Retained<NSArray<ObjectType>>;
212    );
213}
214
215impl<ObjectType: Message> private_NSArrayGameplayKit::Sealed for NSArray<ObjectType> {}
216unsafe impl<ObjectType: Message> NSArrayGameplayKit<ObjectType> for NSArray<ObjectType> {}
217
218extern_class!(
219    /// A deterministic pseudo-random source that generates random numbers based on an arc4 algorithm.
220    /// This is a deterministic random source suitable for creating reliable gameplay mechanics.
221    ///
222    /// While deterministic, this is not a cryptographic random source, however it may be useful
223    /// for obfuscation of gameplay data in manner similar to a stream cipher.
224    ///
225    /// See also [Apple's documentation](https://developer.apple.com/documentation/gameplaykit/gkarc4randomsource?language=objc)
226    #[unsafe(super(GKRandomSource, NSObject))]
227    #[derive(Debug, PartialEq, Eq, Hash)]
228    pub struct GKARC4RandomSource;
229);
230
231extern_conformance!(
232    unsafe impl GKRandom for GKARC4RandomSource {}
233);
234
235extern_conformance!(
236    unsafe impl NSCoding for GKARC4RandomSource {}
237);
238
239extern_conformance!(
240    unsafe impl NSCopying for GKARC4RandomSource {}
241);
242
243unsafe impl CopyingHelper for GKARC4RandomSource {
244    type Result = Self;
245}
246
247extern_conformance!(
248    unsafe impl NSObjectProtocol for GKARC4RandomSource {}
249);
250
251extern_conformance!(
252    unsafe impl NSSecureCoding for GKARC4RandomSource {}
253);
254
255impl GKARC4RandomSource {
256    extern_methods!(
257        /// The seed used to stir the arc4 random source.
258        /// The seed is not encoded through archiving, but the equivalent state buffers are encoded.
259        #[unsafe(method(seed))]
260        #[unsafe(method_family = none)]
261        pub unsafe fn seed(&self) -> Retained<NSData>;
262
263        /// Setter for [`seed`][Self::seed].
264        ///
265        /// This is [copied][objc2_foundation::NSCopying::copy] when set.
266        #[unsafe(method(setSeed:))]
267        #[unsafe(method_family = none)]
268        pub unsafe fn setSeed(&self, seed: &NSData);
269
270        /// Initializes an arc4 random source with bits from high entropy system resource like SecRandomCopyBytes.
271        #[unsafe(method(init))]
272        #[unsafe(method_family = init)]
273        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
274
275        /// Initializes an arc4 random source with bits from the seed.
276        #[unsafe(method(initWithSeed:))]
277        #[unsafe(method_family = init)]
278        pub unsafe fn initWithSeed(this: Allocated<Self>, seed: &NSData) -> Retained<Self>;
279
280        /// Arc4 based random sources have repeatable initial sequences. If used for obfuscation you should
281        /// drop N values from the start, where N should be any number larger than 768 to ensure the initial
282        /// sequence is flushed.
283        #[unsafe(method(dropValuesWithCount:))]
284        #[unsafe(method_family = none)]
285        pub unsafe fn dropValuesWithCount(&self, count: NSUInteger);
286    );
287}
288
289/// Methods declared on superclass `GKRandomSource`.
290impl GKARC4RandomSource {
291    extern_methods!(
292        /// Deserializes a random source from an NSCoder. All random sources support coding for serializing and deserializing the state
293        /// of the random source. Each subclass has its own contract for what parts of the state is preserved when serialized but the
294        /// general contract is that a serialized source must generate the same sequence of values as the original source would from the
295        /// instant it was serialized.
296        ///
297        /// Note that the sharedRandom instance is an exception as it is explicitly seedless and a shared singleton instance.
298        /// When serialized and deserialized it will return the current sharedRandom instance instead.
299        ///
300        /// # Safety
301        ///
302        /// `a_decoder` possibly has further requirements.
303        #[unsafe(method(initWithCoder:))]
304        #[unsafe(method_family = init)]
305        pub unsafe fn initWithCoder(this: Allocated<Self>, a_decoder: &NSCoder) -> Retained<Self>;
306    );
307}
308
309/// Methods declared on superclass `NSObject`.
310impl GKARC4RandomSource {
311    extern_methods!(
312        #[unsafe(method(new))]
313        #[unsafe(method_family = new)]
314        pub unsafe fn new() -> Retained<Self>;
315    );
316}
317
318extern_class!(
319    /// A deterministic pseudo-random source that generates random numbers based on a linear congruential algorithm.
320    /// This is a deterministic random source suitable for creating reliable gameplay mechanics.
321    /// It is slightly faster than an Arc4 source, but less random. In particular the lower bits
322    /// of the generated values are less random than the higher bits.
323    ///
324    /// While deterministic, this is not a cryptographic random source. It is also not suitable
325    /// for obfuscation of gameplay data.
326    ///
327    /// See also [Apple's documentation](https://developer.apple.com/documentation/gameplaykit/gklinearcongruentialrandomsource?language=objc)
328    #[unsafe(super(GKRandomSource, NSObject))]
329    #[derive(Debug, PartialEq, Eq, Hash)]
330    pub struct GKLinearCongruentialRandomSource;
331);
332
333extern_conformance!(
334    unsafe impl GKRandom for GKLinearCongruentialRandomSource {}
335);
336
337extern_conformance!(
338    unsafe impl NSCoding for GKLinearCongruentialRandomSource {}
339);
340
341extern_conformance!(
342    unsafe impl NSCopying for GKLinearCongruentialRandomSource {}
343);
344
345unsafe impl CopyingHelper for GKLinearCongruentialRandomSource {
346    type Result = Self;
347}
348
349extern_conformance!(
350    unsafe impl NSObjectProtocol for GKLinearCongruentialRandomSource {}
351);
352
353extern_conformance!(
354    unsafe impl NSSecureCoding for GKLinearCongruentialRandomSource {}
355);
356
357impl GKLinearCongruentialRandomSource {
358    extern_methods!(
359        /// The seed used to stir the linear congruential random source.
360        /// The seed changes each time a random value is generated from this source, as the seed is the state buffer.
361        /// The seed is encoded through archiving.
362        #[unsafe(method(seed))]
363        #[unsafe(method_family = none)]
364        pub unsafe fn seed(&self) -> u64;
365
366        /// Setter for [`seed`][Self::seed].
367        #[unsafe(method(setSeed:))]
368        #[unsafe(method_family = none)]
369        pub unsafe fn setSeed(&self, seed: u64);
370
371        /// Initializes a linear congruential random source with bits from high entropy system resource like SecRandomCopyBytes.
372        #[unsafe(method(init))]
373        #[unsafe(method_family = init)]
374        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
375
376        /// Initializes a linear congruential random source with bits the given 64 bit seed.
377        #[unsafe(method(initWithSeed:))]
378        #[unsafe(method_family = init)]
379        pub unsafe fn initWithSeed(this: Allocated<Self>, seed: u64) -> Retained<Self>;
380    );
381}
382
383/// Methods declared on superclass `GKRandomSource`.
384impl GKLinearCongruentialRandomSource {
385    extern_methods!(
386        /// Deserializes a random source from an NSCoder. All random sources support coding for serializing and deserializing the state
387        /// of the random source. Each subclass has its own contract for what parts of the state is preserved when serialized but the
388        /// general contract is that a serialized source must generate the same sequence of values as the original source would from the
389        /// instant it was serialized.
390        ///
391        /// Note that the sharedRandom instance is an exception as it is explicitly seedless and a shared singleton instance.
392        /// When serialized and deserialized it will return the current sharedRandom instance instead.
393        ///
394        /// # Safety
395        ///
396        /// `a_decoder` possibly has further requirements.
397        #[unsafe(method(initWithCoder:))]
398        #[unsafe(method_family = init)]
399        pub unsafe fn initWithCoder(this: Allocated<Self>, a_decoder: &NSCoder) -> Retained<Self>;
400    );
401}
402
403/// Methods declared on superclass `NSObject`.
404impl GKLinearCongruentialRandomSource {
405    extern_methods!(
406        #[unsafe(method(new))]
407        #[unsafe(method_family = new)]
408        pub unsafe fn new() -> Retained<Self>;
409    );
410}
411
412extern_class!(
413    /// A deterministic pseudo-random source that generates random numbers based on a mersenne twister algorithm.
414    /// This is a deterministic random source suitable for creating reliable gameplay mechanics.
415    /// It is slightly slower than an Arc4 source, but more random, in that it has a longer period until repeating sequences.
416    ///
417    /// While deterministic, this is not a cryptographic random source. It is however suitable
418    /// for obfuscation of gameplay data.
419    ///
420    /// See also [Apple's documentation](https://developer.apple.com/documentation/gameplaykit/gkmersennetwisterrandomsource?language=objc)
421    #[unsafe(super(GKRandomSource, NSObject))]
422    #[derive(Debug, PartialEq, Eq, Hash)]
423    pub struct GKMersenneTwisterRandomSource;
424);
425
426extern_conformance!(
427    unsafe impl GKRandom for GKMersenneTwisterRandomSource {}
428);
429
430extern_conformance!(
431    unsafe impl NSCoding for GKMersenneTwisterRandomSource {}
432);
433
434extern_conformance!(
435    unsafe impl NSCopying for GKMersenneTwisterRandomSource {}
436);
437
438unsafe impl CopyingHelper for GKMersenneTwisterRandomSource {
439    type Result = Self;
440}
441
442extern_conformance!(
443    unsafe impl NSObjectProtocol for GKMersenneTwisterRandomSource {}
444);
445
446extern_conformance!(
447    unsafe impl NSSecureCoding for GKMersenneTwisterRandomSource {}
448);
449
450impl GKMersenneTwisterRandomSource {
451    extern_methods!(
452        /// The seed used to stir the mersenne twister random source.
453        /// The seed is not encoded through archiving, but the equivalent state buffers are encoded.
454        #[unsafe(method(seed))]
455        #[unsafe(method_family = none)]
456        pub unsafe fn seed(&self) -> u64;
457
458        /// Setter for [`seed`][Self::seed].
459        #[unsafe(method(setSeed:))]
460        #[unsafe(method_family = none)]
461        pub unsafe fn setSeed(&self, seed: u64);
462
463        /// Initializes a linear congruential random source with bits from a high entropy system resource like SecRandomCopyBytes.
464        #[unsafe(method(init))]
465        #[unsafe(method_family = init)]
466        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
467
468        /// Initializes a linear congruential random source with bits the given 64 bit seed.
469        #[unsafe(method(initWithSeed:))]
470        #[unsafe(method_family = init)]
471        pub unsafe fn initWithSeed(this: Allocated<Self>, seed: u64) -> Retained<Self>;
472    );
473}
474
475/// Methods declared on superclass `GKRandomSource`.
476impl GKMersenneTwisterRandomSource {
477    extern_methods!(
478        /// Deserializes a random source from an NSCoder. All random sources support coding for serializing and deserializing the state
479        /// of the random source. Each subclass has its own contract for what parts of the state is preserved when serialized but the
480        /// general contract is that a serialized source must generate the same sequence of values as the original source would from the
481        /// instant it was serialized.
482        ///
483        /// Note that the sharedRandom instance is an exception as it is explicitly seedless and a shared singleton instance.
484        /// When serialized and deserialized it will return the current sharedRandom instance instead.
485        ///
486        /// # Safety
487        ///
488        /// `a_decoder` possibly has further requirements.
489        #[unsafe(method(initWithCoder:))]
490        #[unsafe(method_family = init)]
491        pub unsafe fn initWithCoder(this: Allocated<Self>, a_decoder: &NSCoder) -> Retained<Self>;
492    );
493}
494
495/// Methods declared on superclass `NSObject`.
496impl GKMersenneTwisterRandomSource {
497    extern_methods!(
498        #[unsafe(method(new))]
499        #[unsafe(method_family = new)]
500        pub unsafe fn new() -> Retained<Self>;
501    );
502}