Skip to content

Commit

Permalink
Add rc constructor variants; remove Share struct
Browse files Browse the repository at this point in the history
  • Loading branch information
appcypher committed Aug 30, 2023
1 parent d90353a commit 2dfa2da
Show file tree
Hide file tree
Showing 20 changed files with 432 additions and 480 deletions.
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,15 @@ Let's see an example of working with a public filesystem. We will use the in-mem
```rust
use anyhow::Result;
use chrono::Utc;
use std::rc::Rc;
use wnfs::public::PublicDirectory;
use wnfs_common::MemoryBlockStore;
use wnfs::{
common::MemoryBlockStore,
public::PublicDirectory
};
#[async_std::main]
async fn main() -> Result<()> {
// Create a new public directory.
let dir = &mut Rc::new(PublicDirectory::new(Utc::now()));
let dir = &mut PublicDirectory::rc(Utc::now());
// Create an in-memory block store.
let store = &MemoryBlockStore::default();
Expand Down Expand Up @@ -215,12 +216,13 @@ That's the public filesystem, the private filesystem, on the other hand, is a bi
use anyhow::Result;
use chrono::Utc;
use rand::thread_rng;
use std::rc::Rc;
use wnfs::private::{
PrivateDirectory,
forest::{hamt::HamtForest, traits::PrivateForest},
use wnfs::{
common::MemoryBlockStore,
private::{
PrivateDirectory,
forest::{hamt::HamtForest, traits::PrivateForest},
}
};
use wnfs_common::MemoryBlockStore;
#[async_std::main]
async fn main() -> Result<()> {
Expand All @@ -231,14 +233,10 @@ async fn main() -> Result<()> {
let rng = &mut thread_rng();
// Create a private forest.
let forest = &mut Rc::new(HamtForest::new_trusted(rng));
let forest = &mut HamtForest::rc_trusted(rng);
// Create a new private directory.
let dir = &mut Rc::new(PrivateDirectory::new(
&forest.empty_name(),
Utc::now(),
rng,
));
let dir = &mut PrivateDirectory::rc(&forest.empty_name(), Utc::now(), rng);
// Add a file to /pictures/cats directory.
dir.mkdir(
Expand Down
29 changes: 13 additions & 16 deletions wnfs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ Let's see an example of working with a public filesystem. We will use the in-mem
```rust
use anyhow::Result;
use chrono::Utc;
use std::rc::Rc;
use wnfs::public::PublicDirectory;
use wnfs_common::MemoryBlockStore;
use wnfs::{
common::MemoryBlockStore,
public::PublicDirectory
};

#[async_std::main]
async fn main() -> Result<()> {
// Create a new public directory.
let dir = &mut Rc::new(PublicDirectory::new(Utc::now()));
let dir = &mut PublicDirectory::rc(Utc::now());

// Create an in-memory block store.
let store = &MemoryBlockStore::default();
Expand Down Expand Up @@ -84,12 +85,13 @@ That's the public filesystem, the private filesystem, on the other hand, is a bi
use anyhow::Result;
use chrono::Utc;
use rand::thread_rng;
use std::rc::Rc;
use wnfs::private::{
PrivateDirectory,
forest::{hamt::HamtForest, traits::PrivateForest},
use wnfs::{
common::MemoryBlockStore,
private::{
PrivateDirectory,
forest::{hamt::HamtForest, traits::PrivateForest},
}
};
use wnfs_common::MemoryBlockStore;

#[async_std::main]
async fn main() -> Result<()> {
Expand All @@ -100,14 +102,10 @@ async fn main() -> Result<()> {
let rng = &mut thread_rng();

// Create a private forest.
let forest = &mut Rc::new(HamtForest::new_trusted(rng));
let forest = &mut HamtForest::rc_trusted(rng);

// Create a new private directory.
let dir = &mut Rc::new(PrivateDirectory::new(
&forest.empty_name(),
Utc::now(),
rng,
));
let dir = &mut PrivateDirectory::rc(&forest.empty_name(), Utc::now(), rng);

// Add a file to /pictures/cats directory.
dir.mkdir(
Expand Down Expand Up @@ -149,7 +147,6 @@ Finally, we have the random number generator, `rng`, that the library uses for g

Check the [`wnfs/examples/`][wnfs-examples] folder for more examples.


[blockstore-trait]: https://github.com/wnfs-wg/rs-wnfs/blob/main/wnfs-common/src/blockstore.rs
[hamt-wiki]: https://en.wikipedia.org/wiki/Hash_array_mapped_trie
[ipld-spec]: https://ipld.io/
Expand Down
6 changes: 3 additions & 3 deletions wnfs/examples/mnemonic_based.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn main() -> Result<()> {
async fn root_dir_setup(store: &impl BlockStore) -> Result<(Rc<HamtForest>, AccessKey)> {
// We generate a new simple example file system:
let rng = &mut rand::thread_rng();
let forest = &mut Rc::new(HamtForest::new_trusted(rng));
let forest = &mut HamtForest::rc_trusted(rng);
let root_dir =
&mut PrivateDirectory::new_and_store(&forest.empty_name(), Utc::now(), forest, store, rng)
.await?;
Expand Down Expand Up @@ -93,7 +93,7 @@ async fn setup_seeded_keypair_access(
// Store the public key inside some public WNFS.
// Building from scratch in this case. Would actually be stored next to the private forest usually.
let public_key_cid = exchange_keypair.store_public_key(store).await?;
let mut exchange_root = Rc::new(PublicDirectory::new(Utc::now()));
let mut exchange_root = PublicDirectory::rc(Utc::now());
exchange_root
.write(
&["main".into(), "v1.exchange_key".into()],
Expand Down Expand Up @@ -125,8 +125,8 @@ async fn setup_seeded_keypair_access(
&access_key,
counter,
root_did,
forest,
exchange_root,
forest,
store,
)
.await?;
Expand Down
5 changes: 2 additions & 3 deletions wnfs/examples/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use chrono::Utc;
use libipld_core::cid::Cid;
use rand::thread_rng;
use rand_core::CryptoRngCore;
use std::rc::Rc;
use wnfs::{
common::{BlockStore, MemoryBlockStore},
nameaccumulator::AccumulatorSetup,
Expand Down Expand Up @@ -47,10 +46,10 @@ async fn create_forest_and_add_directory(
let setup = AccumulatorSetup::trusted(rng);

// Create the private forest (a HAMT), a map-like structure where file and directory ciphertexts are stored.
let forest = &mut Rc::new(HamtForest::new(setup));
let forest = &mut HamtForest::rc(setup);

// Create a new directory.
let dir = &mut Rc::new(PrivateDirectory::new(&forest.empty_name(), Utc::now(), rng));
let dir = &mut PrivateDirectory::rc(&forest.empty_name(), Utc::now(), rng);

// Add a /pictures/cats subdirectory.
dir.mkdir(
Expand Down
3 changes: 1 addition & 2 deletions wnfs/examples/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use anyhow::Result;
use chrono::Utc;
use libipld_core::cid::Cid;
use std::rc::Rc;
use wnfs::{common::MemoryBlockStore, public::PublicDirectory};

#[async_std::main]
Expand All @@ -13,7 +12,7 @@ async fn main() -> Result<()> {
let store = MemoryBlockStore::default();

// Create a new directory.
let root_dir = &mut Rc::new(PublicDirectory::new(Utc::now()));
let root_dir = &mut PublicDirectory::rc(Utc::now());

// Add a /pictures/cats subdirectory.
root_dir
Expand Down
5 changes: 2 additions & 3 deletions wnfs/examples/tiered_blockstores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use bytes::Bytes;
use chrono::Utc;
use libipld_core::cid::Cid;
use rand::thread_rng;
use std::rc::Rc;
use wnfs::{
common::{BlockStore, MemoryBlockStore},
private::{
Expand All @@ -33,10 +32,10 @@ async fn main() -> Result<()> {

// Create a new private forest.
// This represents your whole private file system, but hides any internal structure.
let forest = &mut Rc::new(HamtForest::new_rsa_2048(rng));
let forest = &mut HamtForest::rc_rsa_2048(rng);

// Create a new private directory
let mut directory = Rc::new(PrivateDirectory::new(&forest.empty_name(), Utc::now(), rng));
let mut directory = PrivateDirectory::rc(&forest.empty_name(), Utc::now(), rng);

let file_path = ["datasets".into(), "recordings".into(), "monday.mp4".into()];
let video = b"This isn't actually a video. But it could be!";
Expand Down
4 changes: 2 additions & 2 deletions wnfs/examples/write_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ async fn main() -> Result<()> {
/// supposed to be publicly signed for verifyable write access.
async fn alice_actions(store: &impl BlockStore) -> Result<(Cid, AccessKey, NameAccumulator)> {
let rng = &mut thread_rng();
let forest = &mut Rc::new(HamtForest::new_rsa_2048(rng));
let root_dir = &mut Rc::new(PrivateDirectory::new(&forest.empty_name(), Utc::now(), rng));
let forest = &mut HamtForest::rc_rsa_2048(rng);
let root_dir = &mut PrivateDirectory::rc(&forest.empty_name(), Utc::now(), rng);

let access_key = root_dir.as_node().store(forest, store, rng).await?;
let cid = forest.store(store).await?;
Expand Down
25 changes: 12 additions & 13 deletions wnfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
//! ```rust
//! use anyhow::Result;
//! use chrono::Utc;
//! use std::rc::Rc;
//! use wnfs::{common::MemoryBlockStore, public::PublicDirectory};
//! use wnfs::{
//! common::MemoryBlockStore,
//! public::PublicDirectory
//! };
//!
//! #[async_std::main]
//! async fn main() -> Result<()> {
//! // Create a new public directory.
//! let dir = &mut Rc::new(PublicDirectory::new(Utc::now()));
//! let dir = &mut PublicDirectory::rc(Utc::now());
//!
//! // Create an in-memory block store.
//! let store = &MemoryBlockStore::default();
Expand Down Expand Up @@ -48,11 +50,12 @@
//! use anyhow::Result;
//! use chrono::Utc;
//! use rand::thread_rng;
//! use std::rc::Rc;
//! use wnfs::private::{
//! PrivateDirectory,
//! use wnfs::{
//! common::MemoryBlockStore,
//! forest::{hamt::HamtForest, traits::PrivateForest},
//! private::{
//! PrivateDirectory,
//! forest::{hamt::HamtForest, traits::PrivateForest},
//! }
//! };
//!
//! #[async_std::main]
Expand All @@ -64,14 +67,10 @@
//! let rng = &mut thread_rng();
//!
//! // Create a private forest.
//! let forest = &mut Rc::new(HamtForest::new_trusted(rng));
//! let forest = &mut HamtForest::rc_trusted(rng);
//!
//! // Create a new private directory.
//! let dir = &mut Rc::new(PrivateDirectory::new(
//! &forest.empty_name(),
//! Utc::now(),
//! rng,
//! ));
//! let dir = &mut PrivateDirectory::rc(&forest.empty_name(), Utc::now(), rng);
//!
//! // Add a file to /pictures/cats directory.
//! dir.mkdir(
Expand Down
Loading

0 comments on commit 2dfa2da

Please sign in to comment.