Calling functions in the implementation of a trait function












-1












$begingroup$


Consider the following example: I have a trait Foo with a function foo() and an associated type F, and a struct Bar which implements Foo.



Bar's implementation of foo() is so complicated that I would like to have it call a smaller function func() to help structure the code. As I understand it, I can't add functions to the impl Foo for Bar block, because func() isn't part of the Foo trait. So they have to be in their own impl Bar block. Here is what I finally came up with:



trait Foo {
type F;
fn foo(&self) -> Self::F;
}

struct Bar {}

impl Foo for Bar {
type F = i32;
fn foo(&self) -> Self::F {
(self as &Bar).func(3)
}
}

impl Bar {
fn func(&self, f: <Self as Foo>::F) -> <Self as Foo>::F {
f+1
}
}

fn main() {
let b = Bar {};
println!("{}",b.foo());
}


But is this the idiomatic way of solving my issue? Of somewhat lesser importance, if I need more helper functions like func(), is there a way to shorten <Self as Foo>::F? (Inserting a type declaration in the top level of the impl Bar block certainly doesn't work the way it would inside a function scope.) Would I want to?










share|improve this question









$endgroup$












  • $begingroup$
    As per the rules in the the help center, Code Review requires real code. Hypothetical foo/bar examples are off-topic for Code Review.
    $endgroup$
    – 200_success
    51 mins ago
















-1












$begingroup$


Consider the following example: I have a trait Foo with a function foo() and an associated type F, and a struct Bar which implements Foo.



Bar's implementation of foo() is so complicated that I would like to have it call a smaller function func() to help structure the code. As I understand it, I can't add functions to the impl Foo for Bar block, because func() isn't part of the Foo trait. So they have to be in their own impl Bar block. Here is what I finally came up with:



trait Foo {
type F;
fn foo(&self) -> Self::F;
}

struct Bar {}

impl Foo for Bar {
type F = i32;
fn foo(&self) -> Self::F {
(self as &Bar).func(3)
}
}

impl Bar {
fn func(&self, f: <Self as Foo>::F) -> <Self as Foo>::F {
f+1
}
}

fn main() {
let b = Bar {};
println!("{}",b.foo());
}


But is this the idiomatic way of solving my issue? Of somewhat lesser importance, if I need more helper functions like func(), is there a way to shorten <Self as Foo>::F? (Inserting a type declaration in the top level of the impl Bar block certainly doesn't work the way it would inside a function scope.) Would I want to?










share|improve this question









$endgroup$












  • $begingroup$
    As per the rules in the the help center, Code Review requires real code. Hypothetical foo/bar examples are off-topic for Code Review.
    $endgroup$
    – 200_success
    51 mins ago














-1












-1








-1





$begingroup$


Consider the following example: I have a trait Foo with a function foo() and an associated type F, and a struct Bar which implements Foo.



Bar's implementation of foo() is so complicated that I would like to have it call a smaller function func() to help structure the code. As I understand it, I can't add functions to the impl Foo for Bar block, because func() isn't part of the Foo trait. So they have to be in their own impl Bar block. Here is what I finally came up with:



trait Foo {
type F;
fn foo(&self) -> Self::F;
}

struct Bar {}

impl Foo for Bar {
type F = i32;
fn foo(&self) -> Self::F {
(self as &Bar).func(3)
}
}

impl Bar {
fn func(&self, f: <Self as Foo>::F) -> <Self as Foo>::F {
f+1
}
}

fn main() {
let b = Bar {};
println!("{}",b.foo());
}


But is this the idiomatic way of solving my issue? Of somewhat lesser importance, if I need more helper functions like func(), is there a way to shorten <Self as Foo>::F? (Inserting a type declaration in the top level of the impl Bar block certainly doesn't work the way it would inside a function scope.) Would I want to?










share|improve this question









$endgroup$




Consider the following example: I have a trait Foo with a function foo() and an associated type F, and a struct Bar which implements Foo.



Bar's implementation of foo() is so complicated that I would like to have it call a smaller function func() to help structure the code. As I understand it, I can't add functions to the impl Foo for Bar block, because func() isn't part of the Foo trait. So they have to be in their own impl Bar block. Here is what I finally came up with:



trait Foo {
type F;
fn foo(&self) -> Self::F;
}

struct Bar {}

impl Foo for Bar {
type F = i32;
fn foo(&self) -> Self::F {
(self as &Bar).func(3)
}
}

impl Bar {
fn func(&self, f: <Self as Foo>::F) -> <Self as Foo>::F {
f+1
}
}

fn main() {
let b = Bar {};
println!("{}",b.foo());
}


But is this the idiomatic way of solving my issue? Of somewhat lesser importance, if I need more helper functions like func(), is there a way to shorten <Self as Foo>::F? (Inserting a type declaration in the top level of the impl Bar block certainly doesn't work the way it would inside a function scope.) Would I want to?







rust trait






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 14 hours ago









ArthurArthur

1165




1165












  • $begingroup$
    As per the rules in the the help center, Code Review requires real code. Hypothetical foo/bar examples are off-topic for Code Review.
    $endgroup$
    – 200_success
    51 mins ago


















  • $begingroup$
    As per the rules in the the help center, Code Review requires real code. Hypothetical foo/bar examples are off-topic for Code Review.
    $endgroup$
    – 200_success
    51 mins ago
















$begingroup$
As per the rules in the the help center, Code Review requires real code. Hypothetical foo/bar examples are off-topic for Code Review.
$endgroup$
– 200_success
51 mins ago




$begingroup$
As per the rules in the the help center, Code Review requires real code. Hypothetical foo/bar examples are off-topic for Code Review.
$endgroup$
– 200_success
51 mins ago










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211618%2fcalling-functions-in-the-implementation-of-a-trait-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211618%2fcalling-functions-in-the-implementation-of-a-trait-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Список кардиналов, возведённых папой римским Каликстом III

Deduzione

Mysql.sock missing - “Can't connect to local MySQL server through socket”