Skip to content

Instantly share code, notes, and snippets.

:octocat:
Working from home

Guilherme Kunigami kunigami

View GitHub Profile
View closure.rs
fn find_node_with_visitor<F>(
...
k: &K,
mut visitor: F
) -> where F: FnMut(&K) {
...
visitor(&key);
...
}
View node_ptr.rs
struct NodePtr<K: Ord, V>(*mut RBTreeNode<K, V>);
...
pub struct RBTree<K: Ord, V> {
root: NodePtr<K, V>,
...
}
...
// in find_node_with_visitor()
let mut temp = &self.root;
let key = &(*temp.0).key;
View unsafe.rs
let mut num = 5;
let r1 = &num as *const i32;
unsafe {
println!("r1 is: {}", *r1);
}
View wrap_search.rs
// Every container key is smaller than the target_key. In this case we 'wrap around' the
// table and select the first element.
if (closest_key == std::u32::MAX) {
let result = self.containers.get_first();
match result {
None => {
return Err("Did not find first entry.");
}
Some((_, entry)) => {
let container_id = &entry.id;
View find_node.rs
fn find_node_with_visitor<F>(
&self,
k: &K,
mut visitor: F
) -> NodePtr<K, V> where F: FnMut(&K) {
if self.root.is_null() {
return NodePtr::null();
}
let mut temp = &self.root;
unsafe {
View closest_key.rs
let mut closest_key: u32 = std::u32::MAX;
self.containers.get_with_visitor(
&target_key,
|node_key| {
if (
distance(closest_key, target_key) >
distance(*node_key, target_key) &&
*node_key > target_key
) {
View consistent_hash_impl_new.rs
impl ConsistentHashTable {
pub fn new() -> ConsistentHashTable {
return ConsistentHashTable {
containers: rbtree::RBTree::new(),
entries: HashSet::new(),
hash_function: hash_function,
};
}
...
}
View consistent_hash_struct.rs
pub struct ConsistentHashTable {
containers: rbtree::RBTree<u32, Entry>,
entries: HashSet<u32>,
// The hash function must have the property of mapping strings to
// the space of u32 numbers with uniform probability.
hash_function: fn (&String) -> u32
}
View get_largest_with_lifetime.rs
fn get_largest_with_lifetime<'a>(
v1: &'a Vec<i32>,
v2: &'a Vec<i32>
) -> &'a Vec<i32> {
if (v1.len() > v2.len()) {
return v1;
}
return v2;
}
View lifetime.rs
{
let a = 1;
{
let b = 2;
let c = 3;
}
let d = 4;
}
You can’t perform that action at this time.