5 releases (3 breaking)

0.4.0 Dec 24, 2023
0.3.1 Dec 19, 2022
0.3.0 Aug 13, 2022
0.2.0 Aug 9, 2022
0.1.0 Aug 9, 2022

#48 in Finance

Download history 9/week @ 2023-12-22 6/week @ 2024-02-23 3/week @ 2024-03-01 2/week @ 2024-03-08 3/week @ 2024-03-15 47/week @ 2024-03-22 6/week @ 2024-03-29

58 downloads per month

MIT license

15KB
258 lines

A data type for representing stock symbols.

The data associated with a Symbol is allocated on the stack rather than the heap. In order to accomodate this optimization, the length of a Symbol is limited to 7 characters. The representation in memory also allows for highly optimized comparisons, exceeding the performance of stack-allocated arrays. Note because of its optimized size, Symbol implements the Copy trait, and should be passed by value rather than by reference.

Symbols can be easily converted from and to &strs via from_str and as_str. For convenience, Symbol also implements AsRef<str>, and Deref<Target = str>. Moreover, equality comparison against string types is implemented for Symbol as well.

Examples

use stock_symbol::Symbol;

// Make a new symbol
let symbol = Symbol::from_str("AAPL").unwrap();

// Symbols cannot be empty, and must contain fewer than 8 characters
assert!(Symbol::from_str("").is_err());
assert!(Symbol::from_str("12345678").is_err());

// Symbols implement Copy
let symbol_copy = symbol;
assert_eq!(symbol_copy, symbol);

// They can also be compared to strings...
assert_eq!(symbol, "AAPL");

// ...and easily converted into strings
let symbol_str: &str = symbol.as_str();
assert_eq!(symbol_str, "AAPL");

// Symbol also implements Ord and Hash for use in other data structures
let symbol2 = Symbol::from_str("BAC").unwrap();
assert!(symbol < symbol2);

let mut map = std::collections::HashMap::new();
map.insert(symbol, 123.0f64);

Features

The serde feature enables serde support. Symbols are currently serialized as, and deserialized from strings. Other formats are unlikely to be supported in the future. If more direct control is needed, then a custom serializer/deserializer can be made.

The sqlx feature enables support for encoding and decoding Symbols directly from sqlx queries and fetch results. Similar to serde, Symbols are encoded and decoded as &strs.

Dependencies

~0–1.5MB
~31K SLoC