Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trait for auto-inc sentinel values #1728

Open
kazimuth opened this issue Sep 23, 2024 · 3 comments
Open

Trait for auto-inc sentinel values #1728

kazimuth opened this issue Sep 23, 2024 · 3 comments

Comments

@kazimuth
Copy link
Contributor

kazimuth commented Sep 23, 2024

Currently we use 0 all over the place in the codebase, implicitly because it is the sentinel value that means "replace me when I am inserted in to a table with a sequence defined on my column". We should make a trait for this. I would propose something like:

trait AutoIncrementable {
   /// A constant that will be replaced with a unique value upon insertion into a column with the `auto_inc` attribute.
   const SENTINEL: Self;
}

or something like that in spacetimedb_lib. This can be used in the client SDK, module SDK, and in the core database for e.g. SystemIDs.

Note that "auto-inc" and "sequence" are used as synonyms throughout the codebase. We should probably also settle on one of these for consistency of documentation...

@Centril
Copy link
Contributor

Centril commented Oct 7, 2024

There is a trait in crates/bindings/src/table.rs:

/// A trait for types that know if their value will trigger a sequence.
/// This is used for auto-inc columns to determine if an insertion of a row
/// will require the column to be updated in the row.
///
/// For now, this is equivalent to a "is zero" test.
pub trait IsSequenceTrigger {
    /// Is this value one that will trigger a sequence, if any,
    /// when used as a column value.
    fn is_sequence_trigger(&self) -> bool;
}

@gefjon
Copy link
Contributor

gefjon commented Oct 8, 2024

What implementations of is_sequence_trigger or values of SENTINEL other than the integer 0 would we want? I have a hard time imagining any case where we'd want to allow #[auto_inc] on non-integral types.

@Centril
Copy link
Contributor

Centril commented Oct 14, 2024

My primary motivation for the trait was to have something reliable to use for type checking purposes, as opposed to the heuristic check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants