pub struct GunCore {
pub graph: Arc<Graph>,
pub state: Arc<State>,
pub events: Arc<EventEmitter>,
pub storage: Option<Arc<dyn Storage>>,
pub id_counter: Arc<AtomicU64>,
pub dup: Arc<RwLock<Dup>>,
}Expand description
Core Gun instance structure
This is the central engine that powers all Gun operations. It manages:
- Graph: In-memory storage of all nodes
- State: Timestamp generation for conflict resolution
- Events: Event system for reactive updates
- Storage: Optional persistent storage backend
- Dedup: Message deduplication for network operations
Based on Gun.js root.js and core.js. This is an internal structure
that is wrapped by the public Gun type.
§Example
use gun::core::GunCore;
let core = GunCore::new();
let soul = core.uuid(None);
println!("Generated soul: {}", soul);Fields§
§graph: Arc<Graph>§state: Arc<State>§events: Arc<EventEmitter>§storage: Option<Arc<dyn Storage>>§id_counter: Arc<AtomicU64>§dup: Arc<RwLock<Dup>>Implementations§
Source§impl GunCore
impl GunCore
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new GunCore instance without persistent storage
This creates an in-memory only instance. Use with_storage
to enable persistent storage.
§Returns
A new GunCore instance with no persistent storage.
Sourcepub fn with_storage(storage: Arc<dyn Storage>) -> Self
pub fn with_storage(storage: Arc<dyn Storage>) -> Self
Create a new GunCore instance with persistent storage
This enables data persistence across application restarts. The storage
backend can be any implementation of the Storage trait,
such as LocalStorage or SledStorage.
§Arguments
storage- Storage backend implementing theStoragetrait
§Returns
A new GunCore instance with persistent storage enabled.
§Example
use gun::core::GunCore;
use gun::storage::LocalStorage;
use std::sync::Arc;
let storage = Arc::new(LocalStorage::new("./gun_data")?);
let core = GunCore::with_storage(storage);Sourcepub fn uuid(&self, length: Option<usize>) -> String
pub fn uuid(&self, length: Option<usize>) -> String
Generate a new soul (UUID) for a node
Souls are unique identifiers for nodes in the Gun graph. They combine:
- A state-based component derived from the current timestamp
- A random component for uniqueness
Based on Gun.js uuid generation: Gun.state().toString(36).replace('.','') + String.random(12)
§Arguments
length- Optional length of the random component (default: 12)
§Returns
A unique soul string suitable for use as a node identifier.
§Example
use gun::core::GunCore;
let core = GunCore::new();
let soul = core.uuid(None); // Default 12-character random suffix
let short_soul = core.uuid(Some(6)); // 6-character random suffixSourcepub fn random_id(&self, length: usize) -> String
pub fn random_id(&self, length: usize) -> String
Generate a simple random ID (for message IDs, etc.)
This generates a pure random alphanumeric string without the state component. Useful for message IDs and other temporary identifiers.
§Arguments
length- Length of the random ID to generate
§Returns
A random alphanumeric string of the specified length.
§Example
use gun::core::GunCore;
let core = GunCore::new();
let message_id = core.random_id(16);Sourcepub fn next_chain_id(&self) -> u64
pub fn next_chain_id(&self) -> u64
Get the next unique chain ID
Chain IDs are used internally to track chain instances for listener management. This increments atomically and returns a unique identifier for each chain.
§Returns
A unique chain ID (monotonically increasing counter).