Skip to content

Commit 618dd16

Browse files
authored
Add BTreeSet impls to Witty. (#3158)
## 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)
1 parent 8ed5ddb commit 618dd16

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

linera-witty/src/type_traits/implementations/std/collections.rs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
//! Implementations of the custom traits for the Rust collection types.
55
6-
use std::{borrow::Cow, collections::BTreeMap};
6+
use std::{
7+
borrow::Cow,
8+
collections::{BTreeMap, BTreeSet},
9+
};
710

811
use frunk::HList;
912

@@ -95,3 +98,81 @@ where
9598
entries.lower(memory)
9699
}
97100
}
101+
102+
impl<T> WitType for BTreeSet<T>
103+
where
104+
T: WitType,
105+
{
106+
const SIZE: u32 = <Vec<T> as WitType>::SIZE;
107+
108+
type Layout = <Vec<T> as WitType>::Layout;
109+
type Dependencies = HList![T];
110+
111+
fn wit_type_name() -> Cow<'static, str> {
112+
<Vec<T> as WitType>::wit_type_name()
113+
}
114+
115+
fn wit_type_declaration() -> Cow<'static, str> {
116+
<Vec<T> as WitType>::wit_type_declaration()
117+
}
118+
}
119+
120+
impl<T> WitLoad for BTreeSet<T>
121+
where
122+
T: WitType + Ord + WitLoad,
123+
{
124+
fn load<Instance>(
125+
memory: &Memory<'_, Instance>,
126+
location: GuestPointer,
127+
) -> Result<Self, RuntimeError>
128+
where
129+
Instance: InstanceWithMemory,
130+
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
131+
{
132+
let entries = <Vec<T> as WitLoad>::load(memory, location)?;
133+
Ok(entries.into_iter().collect())
134+
}
135+
136+
fn lift_from<Instance>(
137+
flat_layout: <Self::Layout as Layout>::Flat,
138+
memory: &Memory<'_, Instance>,
139+
) -> Result<Self, RuntimeError>
140+
where
141+
Instance: InstanceWithMemory,
142+
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
143+
{
144+
let entries = <Vec<T> as WitLoad>::lift_from(flat_layout, memory)?;
145+
Ok(entries.into_iter().collect())
146+
}
147+
}
148+
149+
impl<T> WitStore for BTreeSet<T>
150+
where
151+
T: WitType + WitStore,
152+
for<'a> &'a T: WitStore,
153+
{
154+
fn store<Instance>(
155+
&self,
156+
memory: &mut Memory<'_, Instance>,
157+
location: GuestPointer,
158+
) -> Result<(), RuntimeError>
159+
where
160+
Instance: InstanceWithMemory,
161+
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
162+
{
163+
let entries = self.iter().collect::<Vec<&T>>();
164+
entries.store(memory, location)
165+
}
166+
167+
fn lower<Instance>(
168+
&self,
169+
memory: &mut Memory<'_, Instance>,
170+
) -> Result<Self::Layout, RuntimeError>
171+
where
172+
Instance: InstanceWithMemory,
173+
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>,
174+
{
175+
let entries = self.iter().collect::<Vec<&T>>();
176+
entries.lower(memory)
177+
}
178+
}

0 commit comments

Comments
 (0)