From 4fd4b4212415d39ad4c6b1dcf7fb85e5fe46c8c9 Mon Sep 17 00:00:00 2001 From: Armin Becher Date: Mon, 6 Apr 2020 21:56:29 +0200 Subject: [PATCH] Updaate docstrings --- src/lib.rs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d96ddcf..0c44c9c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -/*! +/*! # Two Dimensional Grid Continuos growable 2D data structure. The purpose of this crate is to provide an universal data structure that is faster, @@ -38,13 +38,6 @@ use std::ops::IndexMut; use std::slice::Iter; use std::slice::IterMut; -/// Stores elements of a certain type in a 2D grid structure. -pub struct Grid { - data: Vec, - cols: usize, - rows: usize, -} - #[doc(hidden)] #[macro_export] macro_rules! count { @@ -53,6 +46,14 @@ macro_rules! count { } /// Init a grid with values. +/// +/// Each array within `[]` represents a row starting from top to button. +/// +/// # Examples +/// +/// In this example a grid of numbers from 1 to 9 is created: +/// +/// /// ``` /// use grid::grid; /// let grid = grid![[1, 2, 3] @@ -60,6 +61,17 @@ macro_rules! count { /// [7, 8, 9]]; /// assert_eq!(grid.size(), (3, 3)) /// ``` +/// +/// # Examples +/// +/// Not that each row must be of the same length. The following example will not compile: +/// +/// ``` ignore +/// use grid::grid; +/// let grid = grid![[1, 2, 3] +/// [4, 5] // This does not work! +/// [7, 8, 9]]; +/// ``` #[macro_export] macro_rules! grid { () => { @@ -92,6 +104,18 @@ macro_rules! grid { }; } +/// Stores elements of a certain type in a 2D grid structure. +/// +/// Uses a rust `Vec` type to reference the grid data on the heap. +/// Also the number of rows and columns are stored in the grid data structure. +/// +/// The grid data is stored in a row-major memory layout. +pub struct Grid { + data: Vec, + cols: usize, + rows: usize, +} + impl Grid { /// Init a grid of size rows x columns with default values of the given type. /// For example this will generate a 2x3 grid of zeros: