jaemk / cached

Rust cache structures and easy function memoization
MIT License
1.58k stars 95 forks source link

error[E0282]: type annotations needed -> this method call resolves to `std::option::Option<&V>` #79

Closed doivosevic closed 3 years ago

doivosevic commented 3 years ago

I'm trying to implement a file based cacher. This is my cache type MyCache. I'm trying to follow readme.


pub struct MyCache {
    pub dir: String,
}

type Key = (String, String);

impl MyCache {
    pub fn new() -> MyCache {
        MyCache { dir: "./my_cache".to_string() }
    }

    fn get_key_hash(&self, key: &Key) -> String {
        format!("{}__{}", key.0, key.1)
    }
}

impl<V: DeserializeOwned + Serialize> Cached<Key, V> for MyCache {
    fn cache_get(&mut self, k: &(String, String)) -> Option<&V> {
        let output = cacache::read_sync(self.dir, self.get_key_hash(k)).ok()?;

        let as_str = String::from_utf8(output).unwrap();
        let o: V = serde_json::from_str(&as_str).unwrap();

        return Some(&o);
    }

    fn cache_set(&mut self, k: Key, v: V) -> Option<V> {
        let vec = serde_json::to_vec(&v).unwrap();
        cacache::write_sync(self.dir, self.get_key_hash(&k), vec).unwrap();

        Some(v)
    }

    fn cache_get_mut(&mut self, k: &Key) -> Option<&mut V> {
        self.cache_get(k).map(|x: &V| (*x).borrow_mut())
    }

    fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, k: Key, f: F) -> &mut V {
        let gotten: Option<&V> = self.cache_get(&k);
        match gotten {
            Some(res) => res.borrow_mut(),
            None => {
                let new = f();
                self.cache_set(k, new).unwrap();
                new.borrow_mut()
            }
        }
    }

    fn cache_remove(&mut self, k: &Key) -> Option<V> {
        let v: Option<&V> = self.cache_get(k);
        cacache::remove_sync(self.dir, self.get_key_hash(k));
        let v = *v.unwrap();
        Some(v)
    }

    fn cache_clear(&mut self) {
        cacache::clear_sync(self.dir);
    }

    fn cache_reset(&mut self) {
        cacache::clear_sync(self.dir);
    }

    fn cache_size(&self) -> usize {
        todo!()
    }
}

When trying to create a cached function like this:


#[cached(
    type = "MyCache",
    create = "{ MyCache::new() }",
    convert = r#"{("ymdh".to_string(), bucket_name.to_string())}"#
)]
pub fn cached_get_folder_names_from_bucket_containing_ymdh_subfolders(
    g_cloud: Option<GoogleCloudInterface>,
    a_cloud: Option<AwsCloudInterface>,
    bucket_name: &str,
) -> Vec<String> {
    vec![]
}

I get


error[E0282]: type annotations needed
   --> qwe.rs:100:1
    |
100 | / #[cached(
101 | |     type = "MyCache",
102 | |     create = "{ MyCache::new() }",
103 | |     convert = r#"{("ymdh".to_string(), bucket_name.to_string())}"#
104 | | )]
    | |__^ this method call resolves to `std::option::Option<&V>`
    |
    = note: type must be known at this point
    = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
doivosevic commented 3 years ago

I think this error was hiding an underlying type error. The correct code which seems to work is this:


pub struct MyCache<V> {
    pub dir: String,
    my_hash: HashMap<Key, V>,
}

impl<V> MyCache<V> {
    pub fn new() -> MyCache<V> {
        MyCache {
            dir: "./my_cache".to_string(),
            my_hash: HashMap::new(),
        }
    }

    fn get_key_hash(&self, key: &Key) -> String {
        format!("{}__{}", key.0, key.1)
    }
}

// This can be more generalized if we want to
type Key = (String, String);

impl<V: DeserializeOwned + Serialize> Cached<Key, V> for MyCache<V> {
    fn cache_get(&mut self, k: &Key) -> Option<&V> {
        let output = cacache::read_sync(&self.dir, self.get_key_hash(k)).ok()?;

        let as_str = String::from_utf8(output).unwrap();
        let o: V = serde_json::from_str(&as_str).unwrap();

        self.my_hash.insert(k.to_owned(), o);
        self.my_hash.get(k)
    }

    fn cache_set(&mut self, k: Key, v: V) -> Option<V> {
        let vec = serde_json::to_vec(&v).unwrap();
        cacache::write_sync(&self.dir, self.get_key_hash(&k), vec).unwrap();

        Some(v)
    }

    fn cache_get_mut(&mut self, k: &Key) -> Option<&mut V> {
        let output = cacache::read_sync(&self.dir, self.get_key_hash(k)).ok()?;

        let as_str = String::from_utf8(output).unwrap();
        let o: V = serde_json::from_str(&as_str).unwrap();

        self.my_hash.insert(k.to_owned(), o);
        self.my_hash.get_mut(k)
    }

    fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, k: Key, f: F) -> &mut V {
        let gotten = self.cache_get(&k);

        if gotten.is_none() {
            let new = f();
            self.cache_set(k.clone(), new).unwrap();
        }

        self.cache_get_mut(&k).unwrap()
    }

    fn cache_remove(&mut self, k: &Key) -> Option<V> {
        let key = self.get_key_hash(k);
        cacache::remove_sync(&self.dir, key).unwrap();

        self.my_hash.remove(k)
    }

    fn cache_clear(&mut self) {
        cacache::clear_sync(&self.dir).unwrap();
        self.my_hash.clear();
    }

    fn cache_reset(&mut self) {
        cacache::clear_sync(&self.dir).unwrap();
        self.my_hash = HashMap::new();
    }

    fn cache_size(&self) -> usize {
        self.my_hash.len()
    }
}