Type System
The driver’s type system consists of three main components:- Native types - Basic scalar types like integers, strings, and UUIDs
- Collection types - Lists, sets, maps, and vectors
- Composite types - User-defined types (UDTs) and tuples
Type Mapping Table
The following table shows the mapping between CQL types and Rust types:| CQL Type | Rust Type | Notes |
|---|---|---|
ascii | String, &str | ASCII-only strings |
bigint | i64 | 64-bit signed integer |
blob | Vec<u8>, &[u8], Bytes | Binary data |
boolean | bool | Boolean value |
counter | Counter | Counter column type |
date | CqlDate, chrono::NaiveDate, time::Date | Days since epoch |
decimal | CqlDecimal, bigdecimal::BigDecimal | Variable-precision decimal |
double | f64 | 64-bit floating point |
duration | CqlDuration | Duration with nanosecond precision |
float | f32 | 32-bit floating point |
inet | IpAddr | IPv4 or IPv6 address |
int | i32 | 32-bit signed integer |
smallint | i16 | 16-bit signed integer |
text | String, &str | UTF-8 string |
time | CqlTime, chrono::NaiveTime, time::Time | Nanoseconds since midnight |
timestamp | CqlTimestamp, chrono::DateTime<Utc>, time::OffsetDateTime | Milliseconds since epoch |
timeuuid | CqlTimeuuid | UUID v1 (time-based) |
tinyint | i8 | 8-bit signed integer |
uuid | Uuid | UUID (any version) |
varint | CqlVarint, num_bigint::BigInt | Arbitrary-precision integer |
list<T> | Vec<T> | Ordered collection |
set<T> | Vec<T>, HashSet<T>, BTreeSet<T> | Unordered unique collection |
map<K, V> | HashMap<K, V>, BTreeMap<K, V> | Key-value mapping |
tuple<T1, T2, ...> | (T1, T2, ...) | Fixed-size heterogeneous collection |
vector<T, N> | Vec<T> | Fixed-length vector |
| User-defined type | Custom struct with derive macros | Structured data |
Serialization and Deserialization
The driver provides two main traits for type conversion:SerializeValue
TheSerializeValue trait is used to serialize Rust types into CQL values for sending to the database:
DeserializeValue
TheDeserializeValue trait is used to deserialize CQL values received from the database into Rust types:
Nullability
CQL supports null values for most types. In Rust, useOption<T> to represent nullable values:
Empty Values
Some CQL types support a special “empty” value (distinct from null). Use theMaybeEmpty<T> wrapper:
Unset Values
To leave a value unset in anUPDATE or INSERT statement (which preserves the existing value), use MaybeUnset<T> or Unset:
Feature Flags
Some type mappings require optional feature flags:chrono-04- Support forchrono0.4 date/time typestime-03- Support fortime0.3 date/time typesnum-bigint-03/num-bigint-04- Support for arbitrary-precision integersbigdecimal-04- Support for arbitrary-precision decimalssecrecy-08/secrecy-10- Support for secret types
Type Safety
The driver performs type checking at runtime to ensure that Rust types match the expected CQL types. Type mismatches result in serialization or deserialization errors.Next Steps
- Primitive Types - Integers, floats, and booleans
- Text Types - Strings and ASCII
- UUID Types - UUID and Timeuuid
- Date and Time Types - Timestamps, dates, and durations
- Collection Types - Lists, sets, and maps
- User-Defined Types - Custom structured types
