spacejam / sled

the champagne of beta embedded databases
Apache License 2.0
8.09k stars 383 forks source link

Lossy signature for Tree::set_merge_operator #1492

Closed RuofengX closed 9 months ago

RuofengX commented 9 months ago

Motivation

I am writting a double-index ecs component storage, by wrap the sled api.

// Define a merge function trait like MergeOperator, let my lib user to write the function
pub trait MergeFn: Fn(EID, Option<Value>, Delta) -> Option<Value> + Send + Sync{} 

I found that it is not possible to insert a trait-object(Rc<dyn MergeFn> in my case) into a Tree as its merge_operator. Because the set_merge_operator function's signature is strict to a static implement.

I design a trait to let my user write there own closure, an wrap them into a Rc<>. Function pointer is also a workround but use triat object is encouraged by the Book, see more in here.

PR content

1 more thing

(Not include in this pr)

In my scanery, merge function is considered as stateless, so it's a Fn trait, instead FnMut, also the merge function is init at the beginning of the program, and never changed after that. So I think the mutex to protect the function isn't needed. The merge function should be passed at the init pharse of the Tree when the program begin, and not protected by a Mutex.

Pros

Cons

RuofengX commented 9 months ago

It change the public api.