nullable_struct 0.1.0

A derive macro that makes it easy to create nullable versions of structs.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented1 out of 2 items with examples
  • Size
  • Source code size: 23.74 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 281.31 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 5s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Ruben1729

Nullable Struct

Introduction

nullable_structs is a Rust crate that provides a Nullable derive macro. This macro makes it incredibly easy to create structs where each field is wrapped in Option, enabling each field to be nullable. In addition, the crate generates convenient getter and setter methods for working with these fields.

[dependencies]
nullable_struct = "0.1.0"

Features

  • Wraps each field of a struct in an Option.
  • Generates getter methods that return either the wrapped value or a default value.
  • Generates getter methods that return Option<&T>.
  • Generates setter methods for updating the value of each field.
  • Provides a constructor to initialize each field with None.
  • Implements the Default trait, initializing all fields to None.

Example

Here is a basic example demonstrating how to use nullable_struct.

extern crate nullable_structs;
use nullable_struct::Nullable;

#[derive(Nullable)]
struct MyStruct {
    field1: i32,
    field2: String,
}

fn main() {
    let mut instance = NullableMyStruct::new(42, "Hello".to_string());
    println!("Field1: {}", instance.field1());  // Output: 42
    println!("Field2: {}", instance.field2());  // Output: Hello

    instance.set_field1(13);
    instance.set_field2("World".to_string());

    if let Some(value) = instance.get_field1() {
        println!("Field1 exists: {}", value); // Output: 13
    }

    if let Some(value) = instance.get_field2() {
        println!("Field2 exists: {}", value); // Output: World
    }
}

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.