Skip to content

Commit

Permalink
Add BTreeSet impls to Witty. (#3158)
Browse files Browse the repository at this point in the history
## Motivation

The Witty traits are implemented for `BTreeMap`, but not `BTreeSet`.

## Proposal

Implement them for `BTreeSet`, too.

## Test Plan

The implementation is trivial. It will effectively be tested when I use
it in a feature soon.

## Release Plan

- Nothing to do / These changes follow the usual release cycle.

## Links

- [reviewer
checklist](https://github.com/linera-io/linera-protocol/blob/main/CONTRIBUTING.md#reviewer-checklist)
  • Loading branch information
afck authored Jan 22, 2025
1 parent 8ed5ddb commit 618dd16
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion linera-witty/src/type_traits/implementations/std/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

//! Implementations of the custom traits for the Rust collection types.
use std::{borrow::Cow, collections::BTreeMap};
use std::{
borrow::Cow,
collections::{BTreeMap, BTreeSet},
};

use frunk::HList;

Expand Down Expand Up @@ -95,3 +98,81 @@ where
entries.lower(memory)
}
}

impl<T> WitType for BTreeSet<T>
where
T: WitType,
{
const SIZE: u32 = <Vec<T> as WitType>::SIZE;

type Layout = <Vec<T> as WitType>::Layout;
type Dependencies = HList![T];

fn wit_type_name() -> Cow<'static, str> {
<Vec<T> as WitType>::wit_type_name()
}

fn wit_type_declaration() -> Cow<'static, str> {
<Vec<T> as WitType>::wit_type_declaration()
}
}

impl<T> WitLoad for BTreeSet<T>
where
T: WitType + Ord + WitLoad,
{
fn load<Instance>(
memory: &Memory<'_, Instance>,
location: GuestPointer,
) -> Result<Self, RuntimeError>
where
Instance: InstanceWithMemory,
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
{
let entries = <Vec<T> as WitLoad>::load(memory, location)?;
Ok(entries.into_iter().collect())
}

fn lift_from<Instance>(
flat_layout: <Self::Layout as Layout>::Flat,
memory: &Memory<'_, Instance>,
) -> Result<Self, RuntimeError>
where
Instance: InstanceWithMemory,
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
{
let entries = <Vec<T> as WitLoad>::lift_from(flat_layout, memory)?;
Ok(entries.into_iter().collect())
}
}

impl<T> WitStore for BTreeSet<T>
where
T: WitType + WitStore,
for<'a> &'a T: WitStore,
{
fn store<Instance>(
&self,
memory: &mut Memory<'_, Instance>,
location: GuestPointer,
) -> Result<(), RuntimeError>
where
Instance: InstanceWithMemory,
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
{
let entries = self.iter().collect::<Vec<&T>>();
entries.store(memory, location)
}

fn lower<Instance>(
&self,
memory: &mut Memory<'_, Instance>,
) -> Result<Self::Layout, RuntimeError>
where
Instance: InstanceWithMemory,
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
{
let entries = self.iter().collect::<Vec<&T>>();
entries.lower(memory)
}
}

1 comment on commit 618dd16

@Epo00023

This comment was marked as spam.

Please sign in to comment.