# base/identifier (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/base/identifier/

Identifiers. (the original base/identifier.js)
- create_instance_id: in place of the original Object's instance_id. (numbers that never overlap regardless of thread)
- Identifier: like the original, issues sequence numbers from a starting number and an increment. reset goes back to the starting number. (the bug where the original reset put its own value back into itself
  and did nothing was also fixed in the original)
- The original Enum(begin, auto, readonly) imitates enums with JavaScript objects, so it is not ported. Rust uses enum.
- create_guid: createGUID of the original base/reflection.js. (a UUID-form string whose first 12 digits are the millisecond time and the rest random. The rest of the original reflection.js is JavaScript object reflection, so it is not ported)

## Identifier (struct) (Clone, Debug, Default)

Sequence number issuer. (the original Identifier)

Example: Increase by 5 from 100 and reset

```rust
use vanilla::Identifier;

let mut identifier = Identifier::new(100, 5);
assert_eq!(identifier.auto(0), 105);
assert_eq!(identifier.auto(0), 110);
assert_eq!(identifier.auto(1), 111);
identifier.reset();
assert_eq!(identifier.auto(0), 105);
```

- `pub fn new(initial_serial_number: i64, initial_increase_number: i64) -> Identifier` (constructor, since 0.4.0): Constructor. (starting number, amount to increase at a time; the original defaults are 0 and 1)
  - `initial_serial_number` i64: Starting number. (the number reset goes back to; the first auto returns this plus the increment)
  - `initial_increase_number` i64: Amount auto(0) increases at a time.
  - returns Identifier: An issuer at its starting number.
- `pub fn reset(&mut self)` (method, since 0.4.0): Resets to the starting number.
- `pub fn auto(&mut self, increase_number: i64) -> i64` (method, since 0.4.0): Next number. (increases by increase_number if it is not 0, or by the amount set at creation if 0, then returns it)
  - `increase_number` i64: Amount to increase this time. (the amount set at creation if 0)
  - returns i64: The number after increasing.

## Functions and constants

- `pub fn create_instance_id() -> u64` (method, since 0.3.2): New instance identifier. (from 1, never overlapping)
  - returns u64: A number that rises by 1 each time it is called within the process. (first number 1, never overlaps across threads either)
- `pub fn create_guid() -> String` (method, since 0.4.0): Generates a unique identifier. (the original createGUID, "0192a3f2-b1c4-4d8e-8f1a-0e5d4c3b2a19")
  - Puts the 48-bit millisecond time first as 12 hex digits; apart from the version 4 and variant 8 ~ b digits, the rest is random. (74 bits)
  - returns String: Lowercase hex string in 8-4-4-4-12 form. (36 characters; the time digits are 0 if the clock is before the Unix epoch (1970))

  Example: Check the UUID form

  ```rust
  use vanilla::create_guid;
  
  let guid = create_guid();
  assert_eq!(guid.len(), 36);
  assert_eq!(guid.split('-').map(|part| part.len()).collect::<Vec<usize>>(), vec![8, 4, 4, 4, 12]);
  assert_ne!(create_guid(), create_guid());
  ```

