Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage: Borrow instead of own #262

Closed
kkloberdanz opened this issue Apr 19, 2021 · 1 comment
Closed

Storage: Borrow instead of own #262

kkloberdanz opened this issue Apr 19, 2021 · 1 comment

Comments

@kkloberdanz
Copy link
Contributor

Hi gang,

If we take a look at the following line: https://github.com/tf-encrypted/runtime/blob/7254b6230fdf0af1061e3848e419b2514a5875cc/rust/moose/src/storage.rs#L13

The save and load methods on the storage traits take exclusive ownership of the data passed to them. I propose that instead of taking ownership, we instead borrow the values in these methods.

What do you think?

Problem

Currently, if you write something like this, you get a compiler error because the variable key is owned by the first function that it gets passed to, storage.save.

        let expected = Float64Tensor::from(
            array![[1.0, 2.0], [3.0, 4.0]]
                .into_dimensionality::<IxDyn>()
                .unwrap(),
        );
        let key = "my-object".to_string();
        let storage = S3SyncStorage::default();
        storage.save(key, Value::from(expected));
        let loaded = storage.load(key);

Compiler error message:

error[E0382]: use of moved value: `key`
   --> src/cape/storage.rs:205:35
    |
202 |         let key = "my-object".to_string();
    |             --- move occurs because `key` has type `std::string::String`, which does not implement the `Copy` trait
203 |         let storage = S3SyncStorage::default();
204 |         storage.save(key, Value::from(expected));
    |                      --- value moved here
205 |         let loaded = storage.load(key);
    |                                   ^^^ value used here after move

Solution

pub trait SyncStorage {
    fn save(&self, key: &String, val: &Value) -> Result<()>;
    fn load(&self, key: &String) -> Result<Value>;
}

#[async_trait]
pub trait AsyncStorage {
    async fn save(&self, key: &String, val: &Value) -> Result<()>;

    async fn load(&self, key: &String) -> Result<Value>;
}
@mortendahl
Copy link
Member

FYI this is a larger pending issue in the current runtime, where we currently often move Values instead of borrowing. Makes sense to start optimizing this though so I'd say go for it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants