partial_function

[Done] Partial Function library for Rust.
git clone https://git.jojolepro.com/partial_function.git
Log | Files | Refs | README | LICENSE

commit d2cd8700a40b7e24d80138c5eacc35ac7cdc291d
parent 837efa1d3d072e3fa62f34b7a8e404b395f71131
Author: Joël Lupien (Jojolepro) <jojolepromain@gmail.com>
Date:   Wed,  8 Aug 2018 21:47:26 -0400

Replaced fn by Box<Fn>

Diffstat:
M.idea/workspace.xml | 112++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Msrc/lib.rs | 10+++++-----
2 files changed, 85 insertions(+), 37 deletions(-)

diff --git a/.idea/workspace.xml b/.idea/workspace.xml @@ -2,10 +2,8 @@ <project version="4"> <component name="ChangeListManager"> <list default="true" id="c61cb26e-cd20-4478-b386-32735fce1844" name="Default" comment=""> - <change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> - <change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/lib.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/lib.rs" afterDir="false" /> - <change beforePath="$PROJECT_DIR$/tests/tests.rs" beforeDir="false" afterPath="$PROJECT_DIR$/tests/tests.rs" afterDir="false" /> </list> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="TRACKING_ENABLED" value="true" /> @@ -15,15 +13,19 @@ <option name="LAST_RESOLUTION" value="IGNORE" /> </component> <component name="FileEditorManager"> - <leaf> + <leaf SIDE_TABS_SIZE_LIMIT_KEY="300"> <file leaf-file-name="lib.rs" pinned="false" current-in-tab="true"> <entry file="file://$PROJECT_DIR$/src/lib.rs"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="348"> - <caret line="153" column="13" lean-forward="true" selection-start-line="153" selection-start-column="13" selection-end-line="153" selection-end-column="13" /> + <state relative-caret-position="444"> + <caret line="177" column="61" selection-start-line="177" selection-start-column="61" selection-end-line="177" selection-end-column="61" /> <folding> - <element signature="e#4017#4018#0" expanded="true" /> - <element signature="e#4066#4067#0" expanded="true" /> + <element signature="e#4155#4156#0" expanded="true" /> + <element signature="e#4204#4205#0" expanded="true" /> + <element signature="e#5097#5098#0" expanded="true" /> + <element signature="e#5157#5158#0" expanded="true" /> + <element signature="e#5630#5631#0" expanded="true" /> + <element signature="e#5690#5691#0" expanded="true" /> </folding> </state> </provider> @@ -32,8 +34,8 @@ <file leaf-file-name="tests.rs" pinned="false" current-in-tab="false"> <entry file="file://$PROJECT_DIR$/tests/tests.rs"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="1068"> - <caret line="99" column="29" selection-start-line="99" selection-start-column="29" selection-end-line="99" selection-end-column="29" /> + <state relative-caret-position="412"> + <caret line="71" column="33" selection-start-line="71" selection-start-column="33" selection-end-line="71" selection-end-column="33" /> </state> </provider> </entry> @@ -180,55 +182,101 @@ <frame x="12" y="15" width="1896" height="1053" extended-state="0" /> <editor active="true" /> <layout> - <window_info anchor="right" id="Palette" /> + <window_info anchor="right" id="Palette" order="3" /> <window_info anchor="bottom" id="TODO" order="6" /> - <window_info anchor="right" id="Cargo" /> - <window_info anchor="right" id="Palette&#9;" /> - <window_info id="Image Layers" /> - <window_info anchor="right" id="Capture Analysis" /> - <window_info anchor="bottom" id="Event Log" side_tool="true" /> - <window_info anchor="right" id="Maven Projects" /> - <window_info anchor="bottom" id="Version Control" /> + <window_info anchor="right" id="Cargo" order="3" /> + <window_info anchor="right" id="Palette&#9;" order="3" /> + <window_info id="Image Layers" order="2" /> + <window_info anchor="right" id="Capture Analysis" order="3" /> + <window_info anchor="bottom" id="Event Log" order="7" side_tool="true" /> + <window_info anchor="right" id="Maven Projects" order="3" /> <window_info anchor="bottom" id="Run" order="2" /> - <window_info anchor="bottom" id="Terminal" /> - <window_info id="Capture Tool" /> - <window_info id="Designer" /> - <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.121081084" /> + <window_info anchor="bottom" id="Version Control" order="7" /> + <window_info anchor="bottom" id="Terminal" order="7" /> + <window_info id="Capture Tool" order="2" /> + <window_info id="Designer" order="2" /> + <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.06108108" /> <window_info id="Structure" order="1" side_tool="true" weight="0.25" /> <window_info anchor="right" id="Ant Build" order="1" weight="0.25" /> - <window_info id="UI Designer" /> - <window_info anchor="right" id="Theme Preview" /> - <window_info id="Favorites" side_tool="true" /> + <window_info id="UI Designer" order="2" /> + <window_info anchor="right" id="Theme Preview" order="3" /> <window_info anchor="bottom" id="Debug" order="3" weight="0.4" /> - <window_info anchor="bottom" id="Find" order="1" /> - <window_info anchor="right" id="Commander" order="0" weight="0.4" /> + <window_info id="Favorites" order="2" side_tool="true" /> <window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" /> <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> + <window_info anchor="right" id="Commander" order="0" weight="0.4" /> <window_info anchor="bottom" id="Message" order="0" /> <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> + <window_info anchor="bottom" id="Find" order="1" /> </layout> </component> <component name="VcsContentAnnotationSettings"> <option name="myLimit" value="2678400000" /> </component> <component name="editorHistoryManager"> + <entry file="file://$PROJECT_DIR$/src/lib.rs"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="2240"> + <caret line="146" column="33" lean-forward="true" selection-start-line="146" selection-start-column="33" selection-end-line="146" selection-end-column="33" /> + <folding> + <element signature="e#4155#4156#0" expanded="true" /> + <element signature="e#4204#4205#0" expanded="true" /> + <element signature="e#5097#5098#0" expanded="true" /> + <element signature="e#5157#5158#0" expanded="true" /> + <element signature="e#5630#5631#0" expanded="true" /> + <element signature="e#5690#5691#0" expanded="true" /> + </folding> + </state> + </provider> + </entry> + <entry file="file://$PROJECT_DIR$/tests/tests.rs"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="1584"> + <caret line="99" column="29" selection-start-line="99" selection-start-column="29" selection-end-line="99" selection-end-column="29" /> + </state> + </provider> + </entry> <entry file="file://$PROJECT_DIR$/.gitignore"> <provider selected="true" editor-type-id="text-editor" /> </entry> + <entry file="file://$PROJECT_DIR$/src/lib.rs"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="2368"> + <caret line="154" column="9" lean-forward="true" selection-start-line="154" selection-start-column="9" selection-end-line="154" selection-end-column="9" /> + </state> + </provider> + </entry> <entry file="file://$PROJECT_DIR$/tests/tests.rs"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="1068"> + <state relative-caret-position="1584"> <caret line="99" column="29" selection-start-line="99" selection-start-column="29" selection-end-line="99" selection-end-column="29" /> </state> </provider> </entry> + <entry file="file://$PROJECT_DIR$/.gitignore"> + <provider selected="true" editor-type-id="text-editor" /> + </entry> + <entry file="file://$PROJECT_DIR$/.gitignore"> + <provider selected="true" editor-type-id="text-editor" /> + </entry> + <entry file="file://$PROJECT_DIR$/tests/tests.rs"> + <provider selected="true" editor-type-id="text-editor"> + <state relative-caret-position="412"> + <caret line="71" column="33" selection-start-line="71" selection-start-column="33" selection-end-line="71" selection-end-column="33" /> + </state> + </provider> + </entry> <entry file="file://$PROJECT_DIR$/src/lib.rs"> <provider selected="true" editor-type-id="text-editor"> - <state relative-caret-position="348"> - <caret line="153" column="13" lean-forward="true" selection-start-line="153" selection-start-column="13" selection-end-line="153" selection-end-column="13" /> + <state relative-caret-position="444"> + <caret line="177" column="61" selection-start-line="177" selection-start-column="61" selection-end-line="177" selection-end-column="61" /> <folding> - <element signature="e#4017#4018#0" expanded="true" /> - <element signature="e#4066#4067#0" expanded="true" /> + <element signature="e#4155#4156#0" expanded="true" /> + <element signature="e#4204#4205#0" expanded="true" /> + <element signature="e#5097#5098#0" expanded="true" /> + <element signature="e#5157#5158#0" expanded="true" /> + <element signature="e#5630#5631#0" expanded="true" /> + <element signature="e#5690#5691#0" expanded="true" /> </folding> </state> </provider> diff --git a/src/lib.rs b/src/lib.rs @@ -110,7 +110,7 @@ where B: PartialOrd, { /// The stored function f(x) = ??? - pub func: fn(B) -> O, + pub func: Box<Fn(B) -> O>, /// The lower bound of the function. pub lower: B, } @@ -135,7 +135,7 @@ where impl<B, O> LowerPartialFunction<B, O> where - B: PartialOrd, + B: PartialOrd { /// Creates a new LowerPartialFunctionBuilder. pub fn new() -> LowerPartialFunctionBuilder<B, O> { @@ -149,7 +149,7 @@ where for (i, bounded) in iter { let next = self.funcs.get(i + 1); if x >= bounded.lower && ((next.is_some() && next.unwrap().lower > x) || next.is_none()){ - let f = bounded.func; + let f = &bounded.func; return Some(f(x)); } } @@ -175,10 +175,10 @@ where } /// Adds a bounded function bounded between [lower,higher[ of function func. - pub fn with(mut self, lower: B, func: fn(B) -> O) -> Self { + pub fn with<F: Fn(B)->O + 'static>(mut self, lower: B, func: F) -> Self { debug_assert!(self.can_insert(&lower)); let f = LowerBoundedFunction { - func, + func: Box::new(func), lower, }; self.funcs.push(f);